问题
I am new to git and am learning to branch and merge.
When I perform the following operation:
git checkout -b test
a new branch is created, I am switched to that branch. When I:
vim testfile
and enter some text and save, testfile is created as expected. However when I then change to another branch via the following command:
git checkout master
or
git checkout unrelatedBranch
I find that the testfile also exists in these other branches with the same contents.
This behavior is inconsistent with my understanding of how branching works. Several internet resources have indicated that the commands I used here were what was needed.
Can someone please tell me what I am doing wrong or why this is desired behavior?
回答1:
Looks like the testfile
is not under version control.
Try running git status
first to see if it lists testfile
under Untracked files
.
Example:
git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# testfile
no changes added to commit (use "git add" and/or "git commit -a")
If you see testfile
listed under untracked files
like in the above example, you need to add it to version control using the git add
command.
git add testfile
Finally, do not forget to commit the change using git commit
回答2:
testfile
is untracked, and your change isn't committed.
As a result, testfile
isn't on all branches. It's on no branches. Git doesn't know it's even there, so it can't clean it out of the working directory when it switches branches. This is helpful (if unexpected) behavior, because you'd hate to have git messing with untracked files.
Do:
git add testfile
git commit -m "Adding testfile to branch test"
git checkout master
and you'll see testfile go away.
回答3:
It might be because the file is not being tracked.
Try doing: git add testfile
to add it to the new branch.
来源:https://stackoverflow.com/questions/15209217/git-branches-are-showing-the-same-contents