我没有详细研究实现,但是当您切换分支时,索引文件会手动更新以反映新的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
当分支切换发生时,它改变了索引。
此外,如果您“手动”切换分支,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
#
换句话说,索引有一个丢失的文件,该文件位于当前存储库中,因此它“暂存于删除”。
至于分期后切换分支,索引是一个单独的区域,不会改变。
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%