git 切换分支
有时我在一个功能分支中,但我做了一个不相关的更改,我希望在 master 中看到它。通常我可以这样做:
git checkout master
git commit -m "..." filename
但有时当我结账时,我会收到一条警告,提示有本地更改,因此我无法切换分支。
为什么这种情况只是有时发生?当我看到这条消息时有解决办法吗?也许藏起来?
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:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 Devin Ceartas 提到的,当切换分支会更改您已在本地更改的某些文件时,就会发生这种情况。 (当您对不会更改的文件进行本地更改,或者添加分支或主控上都不存在的新文件时,Git 不会抱怨。)
解决此问题的两种方法:
“git stash”您的更改,更改掌握,并“git stash apply”。然后提交更改。
在分支上提交您想要的更改,然后“git stash”任何其他更改(如果有),更改为 master,并在 master 上挑选更改。
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.)
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.
我也见过这个。我认为问题是当您的本地更改会随着其他分支中的某些内容而更改时(而不是其他分支中的新文件)。您始终可以在不同的目录中查看其他分支。
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.