在 git 中切换分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1475037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Switching branches in git
提问by Bill
Sometimes I'm in a feature branch, but I've made an unrelated change that I want to see in master. Often I can just do:
有时我在一个功能分支中,但我做了一个不相关的更改,我想在 master 中看到。通常我可以这样做:
git checkout master
git commit -m "..." filename
But sometimes when I do the checkout I get a warning that there are local changes and thus I can't switch the branch.
但有时当我结账时,我会收到一条警告,说有本地更改,因此我无法切换分支。
Why does this only happen sometimes? Is there a workaround when I see this message? Maybe stash?
为什么这只是有时发生?当我看到此消息时是否有解决方法?也许藏起来?
回答by Phil
As Devin Ceartas mentioned, this happens when switching branches would change some file that you've already changed locally. (Git won't complain when you have local changes on a file that would not be changed, or add new files that exist on neither the branch nor master.)
正如 Devin Ceartas 所提到的,当切换分支会更改您已在本地更改的某些文件时,就会发生这种情况。(当您对不会更改的文件进行本地更改时,Git 不会抱怨,或者添加既不存在于分支上也不存在于 master 上的新文件。)
Two ways around this:
解决这个问题的两种方法:
"git stash" your changes, change to master, and "git stash apply". Then commit the change.
Commit the changes you want on the branch, then "git stash" any other changes (if there are any), change to master, and cherry-pick the change on master.
“git stash”您的更改,更改为 master,然后“git stash apply”。然后提交更改。
在分支上提交您想要的更改,然后“git stash”任何其他更改(如果有),更改为 master,并在 master 上挑选更改。
回答by Devin Ceartas
I've seen this also. I think the issue is when your local changes would change with something in the other branch (as opposed to a new file not in the other branch). You can always check the other branch out in a different directory.
我也见过这个。我认为问题是当您的本地更改会随着另一个分支中的某些内容发生变化时(而不是不在另一个分支中的新文件)。您始终可以在不同的目录中检出另一个分支。