问题
I thought a file created on one branch will not appear in any other branches until I merge or rebase the branch?
Example:
I have two branches:
- master
- new_contact_page
I checkout the new_contact_page branch.
$ git checkout new_contact_page
Then I create a new file.
$ vi contact_page.html
Without doing any Git commands, I switch back to my Master branch.
$ git checkout master
Then I see that this contact_page.html file is also in my Master branch.
$ ls (contact_page.html shows up in the list!)
Shouldn't the file only exist in new_contact_page branch?
回答1:
Git will never touch any files that aren't actually in your repository. (untracked files)
You need to git add
and git commit
the file (into one branch) first.
回答2:
This is where is is important to understand the index (or staging area).
As long as you don't stage (git add) files, they remain "untracked" (or "private"), and won't be modified by a "git checkout".
That is different from "unstaged" which references tracked file (previously committed in the local Git repo) with local modifications not yet added to the index.
You can read more in "You could have invented git (and maybe you already have!)"
来源:https://stackoverflow.com/questions/12341665/git-new-file-appears-in-all-branches