Git:git如何记住每个分支的索引?

Det*_*ant 7 git git-index

例如,我在repo中创建文件a(假设我在主分支上),然后我git add agit commit.在那之后我git branch copygit checkout copy.最后我在word目录中创建文件b git add b.

当我结帐回主分支时,Git似乎很聪明,并且git ls-files没有列出文件b.

所以我很困惑,因为我们index在repo中只有一个文件,git如何同时为分支维护不同的临时区域?

编辑:

如何解释分阶段但未提交的文件,仍然会记住每个分支?

Nou*_*him 4

我没有详细讨论实现,但是当您切换分支时,索引文件会手动更新以反映新HEAD.

例如,我必须在这里分支master(使用一个文件)和test(使用两个文件)。

noufal@sanitarium% git branch
  master
* test
noufal@sanitarium% file .git/index
.git/index: Git index, version 2, 2 entries
noufal@sanitarium% git checkout master
Switched to branch 'master'
noufal@sanitarium% file .git/index
.git/index: Git index, version 2, 1 entries
Run Code Online (Sandbox Code Playgroud)

当分支切换发生时,它改变了索引。

另外,如果您“手动”切换分支,git 不会更新索引并会变得混乱。从上面继续。

noufal@sanitarium% more .git/HEAD
ref: refs/heads/master
noufal@sanitarium% echo "ref: refs/heads/test" > .git/HEAD
noufal@sanitarium% file .git/index
.git/index: Git index, version 2, 1 entries
noufal@sanitarium% git status
# On branch test
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       deleted:    b
#
Run Code Online (Sandbox Code Playgroud)

换句话说,索引有一个丢失的文件,该文件位于当前存储库中,因此它“暂存以进行删除”。

至于暂存后切换分支,索引是一个单独的区域,不会改变。

noufal@sanitarium% git branch
* master
  test
noufal@sanitarium% ls
x
noufal@sanitarium% git status
# On branch master 
nothing to commit (working directory clean)
noufal@sanitarium% git checkout test
Switched to branch 'test'
noufal@sanitarium% ls
x
noufal@sanitarium% echo "Something" > b
noufal@sanitarium% git add b
noufal@sanitarium% git status
# On branch test   
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   b
#
noufal@sanitarium% git checkout master
A       b
Switched to branch 'master'
noufal@sanitarium% git status                    # Also there in index on master branch.
# On branch master 
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   b
#
noufal@sanitarium% git commit -m "Added b in master"
[master 41d0c68] Added b in master
 1 file changed, 1 insertion(+)
 create mode 100644 b
noufal@sanitarium% git status
# On branch master 
nothing to commit (working directory clean)
noufal@sanitarium% git checkout test
Switched to branch 'test'
noufal@sanitarium% ls                           # Missing in the test branch although it was `git add`ed here. 
x
noufal@sanitarium%        
Run Code Online (Sandbox Code Playgroud)