Git允许使用未分类的更改进行分支更改 [英] Git allows for branch change with unstaged changes
问题描述
有没有配置这个地方?
编辑:起初,我认为这是一个配置,我需要设置为禁止在分支之间进行更改,如果存在修改的非分页文件。但通过Emily的评论,似乎会提示您分支之间的文件是否有所不同,而不是以其他方式提示。
解决方案
它决定
一个快速实验显示如下。
假设你在分支 dev
,并且修改了 foo.txt
。如果不提交,请尝试检出 master
。两件事情之一会发生。
-
如果在
foo.txt
> master 中的dev
没有提交,因此不允许切换,因为master
拥有与未分离的更改相冲突的文件的新版本。
要检出
因此,要求Git将
foo.txt
更新为新版本master
>有,摧毁你未分离的变化。为防止您丢失工作,它不会更改分支。 - 否则,修改已从版本
master
知道,你就可以改变分支。 Git不必更新文件,因为master
没有关于该文件的新信息。
对于whoops更改
由于上述原因,如果您在一个分支上的文件中进行非暂存更改并意识到您确实想提交更改另一方面,您可能会也可能不会查看其他分支。
但是,您可以执行以下操作:
git stash save这里是我的更改摘要
(摘要将显示在 git存储列表
)
git checkout otherbranch
git stash drop $ c的组合$ c>)
Git is allowing me to change branches when I have changes not staged for commit (modified files).
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.
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.
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.
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允许使用未分类的更改进行分支更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!