Git 允许使用非暂存更改进行分支更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8526279/
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
Git allows for branch change with unstaged changes
提问by Dave
Git is allowing me to change branches when I have changes not staged for commit (modified files).
Git 允许我在更改未暂存以进行提交(修改的文件)时更改分支。
Is there a configuration for this somewhere?
某处有这个配置吗?
Edit: At first I thought this was a configuration that I needed to set to disallow changing between branches if there are modified unstaged files. But by Emily's comment, it appears that you're prompted if the files differ between branches, and not prompted otherwise.
编辑:起初我认为这是一个配置,如果有修改的未暂存文件,我需要设置它以禁止在分支之间进行更改。但是根据艾米丽的评论,如果分支之间的文件不同,似乎会提示您,而不会提示您。
回答by Nathan Long
How it decides
它如何决定
A quick experiment shows the following.
快速实验显示以下内容。
Suppose you're on branch dev
and you've modified foo.txt
. Without committing, you try to check out master
. One of two things will happen.
假设你在分支上dev
并且你已经修改了foo.txt
. 没有提交,您尝试检查master
. 将发生两件事之一。
If
foo.txt
was modified inmaster
in a commit thatdev
doesn't have, you won't be allowed to switch without committing, becausemaster
has a "new" version of the file that conflicts with the unstaged changes.To "check out"
master
, therefore, would require Git to updatefoo.txt
to the newer version thatmaster
has, destroying your unstaged changes. To prevent your losing work, it won't change branches.- Otherwise, the modification has been done "since" the version
master
knows about, and you'll be able to change branches. Git doesn't have to update the file becausemaster
has no new information about the file.
如果在没有提交的提交
foo.txt
中master
进行了修改,dev
则不允许您在不提交的情况下进行切换,因为master
该文件的“新”版本与未暂存的更改冲突。master
因此,要“签出” ,将需要 Git 更新foo.txt
到具有的较新版本,master
从而破坏您未暂存的更改。为了防止您丢失工作,它不会更改分支。- 否则,修改已经“因为”版本
master
知道而完成,您将能够更改分支。Git 不必更新文件,因为master
没有关于文件的新信息。
For the "whoops" changes
对于“哎呀”的变化
Because of the above, if you have unstaged changes in files on one branch and realize you actually want to commit the changes on another, you may or may not be able to check out the other branch.
由于上述原因,如果您对一个分支上的文件进行了未暂存的更改,并且意识到您实际上想要在另一个分支上提交更改,则您可能无法签出另一个分支。
You can, however, do the following:
但是,您可以执行以下操作:
git stash save "here's a summary of my changes"
(summary will show up ingit stash list
)git checkout otherbranch
git stash pop
(which is a combination ofgit stash apply
andgit stash drop
)
git stash save "here's a summary of my changes"
(摘要将出现在git stash list
)git checkout otherbranch
git stash pop
(这是的组合git stash apply
和git stash drop
)
回答by Lily Ballard
As Emily pointed out, this is a feature. Git will only disallow the branch change if performing the change would require modifying any of the files that have unstaged changes. If all of the modified files will be untouched by the branch checkout, git will not complain at all. This means that regardless of what branch you check out, you can always check out the previous branch and your working tree will be identical to how you left it.
正如艾米丽指出的那样,这是一个功能。如果执行更改需要修改任何具有未暂存更改的文件,Git 只会禁止分支更改。如果所有修改过的文件都不会被分支检出,git 根本不会抱怨。这意味着无论您检出哪个分支,您始终可以检出前一个分支,并且您的工作树将与您离开它的方式相同。