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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:31:15  来源:igfitidea点击:

Git allows for branch change with unstaged changes

gitversion-control

提问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 devand you've modified foo.txt. Without committing, you try to check out master. One of two things will happen.

假设你在分支上dev并且你已经修改了foo.txt. 没有提交,您尝试检查master. 将发生两件事之一。

  1. If foo.txtwas modified in masterin a commit that devdoesn't have, you won't be allowed to switch without committing, because masterhas a "new" version of the file that conflicts with the unstaged changes.

    To "check out" master, therefore, would require Git to update foo.txtto the newer version that masterhas, destroying your unstaged changes. To prevent your losing work, it won't change branches.

  2. Otherwise, the modification has been done "since" the version masterknows about, and you'll be able to change branches. Git doesn't have to update the file because masterhas no new information about the file.
  1. 如果在没有提交的提交foo.txtmaster进行了修改,dev则不允许您在不提交的情况下进行切换,因为master该文件的“新”版本与未暂存的更改冲突。

    master因此,要“签出” ,将需要 Git 更新foo.txt到具有的较新版本,master从而破坏您未暂存的更改。为了防止您丢失工作,它不会更改分支。

  2. 否则,修改已经“因为”版本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 in git stash list)
  • git checkout otherbranch
  • git stash pop(which is a combination of git stash applyand git stash drop)
  • git stash save "here's a summary of my changes"(摘要将出现在git stash list
  • git checkout otherbranch
  • git stash pop(这是的组合git stash applygit 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 根本不会抱怨。这意味着无论您检出哪个分支,您始终可以检出前一个分支,并且您的工作树将与您离开它的方式相同。