For example, I create file a in the repo(suppose I’m on the master branch),
then I git add a and git commit.
After that I git branch copy and git checkout copy.
Finaly I create file b in the word directory then git add b.
Git seems to be smart when I checkout back to the master branch, and git ls-files, file b is not listed.
So I’m confused, since we only have one index file in a repo, how can git maintain different staging area for branches at the same time?
EDIT:
How to explain the files which are staged but not commited, is still remembered per branch?
I’ve not dived into the implementation in detail but when you switch a branch, the index file is manually updated to reflect the content of the new
HEAD.For example, I have to branches here
master(with one file) andtest(with two files).It’s changed the index when the branch switching happened.
Also, if you “manually” switching branches, git doesn’t update the index and gets confused. Continuing from above.
In other words, the index has a missing file which is there in the current repository so it’s “staged for delete”.
As for switching branches after staging, the index is a separate area which doesn’t change.