Reputation: 159
Variation of this question have been asked and answered but I am still perplexed as I work my way thru the Chacon book.
I have cloned my test repository from github. I created a file in this called afile.txt that I stage and commit I created a 2nd file in this called asecondfile.txt that I don't stage or commit
Now I execute
$ git checkout -b testing
Switched to a new branch 'testing'
When I list the files, I see only asecondfile.txt.
I make a change to this file, don't stage or commit and execute
$ git checkout master
If I cat asecondfile.txt I can see the changes I made in testing branch.
$ git checkout testing
I stage and commit the changes to asecondfile.txt.
$ git checkout master
I list files I don't see aseconfile.txt.
Really puzzled by this behavior and would be glad of an explanation.
Upvotes: 1
Views: 99
Reputation: 489748
When a file is neither staged nor committed, git doesn't know anything about it. Such a path is called "untracked". Git leaves these alone, but gripes about them being "untracked" when you run git status
. (You can shut up the griping, but ignore that until you get to .gitignore
.)
Once a file is staged, though, it's no longer "untracked". It's now a tracked file (and adding it to .gitignore
won't help, it's already tracked). Once that tracked file is committed git knows to remove it when switching from a commit that has it—such as the commit you just made on testing
—to a commit that doesn't.
This is what you saw: git checkout master
removed the file, because it's committed (and therefore also in the index/staging-area) when you're on branch testing
, and the commit that is the tip of branch master
does not have that file.
Once git has removed it (by switching to master
), the path is no longer staged nor committed, so if you create a new asecondfile.txt
after switching to master
, that's an "untracked" file. If you then ask git to git checkout testing
it should complain and fail by default: it would have to remove your untracked file, so as to replace it with a tracked file with (presumably) different contents.
Upvotes: 2