可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am new to Git. It seems to be a fantastic non-liner workflow based on the Directed Acyclic Graph.
According to the git checkout doc:
git checkout <branch>
To prepare for working on , switch to it by updating the index and the files in the working tree, and by pointing HEAD at the branch. Local modifications to the files in the working tree are kept, so that they can be committed to the <branch>
.
I have 2 branches:
Both branches are clean and up-to-date.
I did this:
git checkout dev <change a tracked file> git checkout master
Then I got this error:
error: Your local changes to the following files would be overwritten by checkout: readme.txt Please, commit your changes or stash them before you can switch branches. Aborting
So does this conflict with the bold part of the doc?
ADD
Thanks for all the comments and replies. I can understand Git's good intention to avoid data loss. I just want to point out a potential doc ambiguity/error.
回答1:
You made a change to readme.txt
, which is all well and good.
Then you asked Git to change your current branch from dev
to master
, which is also OK, except for one problem.
Git compared the tip commit of dev
to the tip commit of master
and found that readme.txt
is different in these two commits. So, in order to switch commits, it "wants" to remove the current readme.txt
and replace it with the version out of master
.
But you made a change to readme.txt
. If Git removes readme.txt
and replaces it with the version from master
, your changes will be destroyed.
If Git switches to master
without getting the different version of readme.txt
from master
, that might be OK, but it's not programmed to do that. So you must move your changes out of the way first, then you can switch, then you can restore your changes however you like. Using git commit
or git stash
provide two different ways to do this.
(If you had changed some other file, or if readme.txt
were the same in both commits, Git might or definitely would not have run into this problem, and might or definitely would have switched your current branch over.)
回答2:
No, not really. Normally local changes are indeed kept if you switch branch, but the problem in this case is that the branch you are checking out (master) has other changes to the same file. These changes would overwrite your local changes. Hence its recommendation to either commit or stash.
回答3:
The readme.text
is modified in current branch. This file change at both branches. You should commit and push this file to your current branch.
git add readme.text
git commit -m "Your message"
git push <current branch>
and check git status to see files change. Then, checkout to another branch.
git checkout <another branch>
回答4:
Yes, indeed the documentation is a little confusing. Instead of changing a tracked file, if you add a new file to the master branch and then checkout dev branch, you will see the the local change you made in master (new file you added to the working tree) is kept as the documentation suggests.
git checkout dev
touch <some_new_file>
git checkout master
Master checks out without error and ls shows some_new_file in working dir
If you modify a file that is part of the heads of both branches though, git throws the error.
git checkout dev
<change a tracked file>
git checkout master
throws error about committing or stashing local changes before checkout
This is because your local uncommitted changes can be overwritten if git simply checked out the file from the dev branch.
This behavior can be confusing when you have added a new file to master hoping to commit to the master branch but are not ready yet and have to checkout dev branch only to find that your working directory is a mix of dev's files and the file you intended to commit to master.
In order to avoid this confusion, it is recommended to stash, commit or discard all changes before checking out a new branch.