Git允许使用未分类的更改进行分支更改 [英] Git allows for branch change with unstaged changes

查看:94
本文介绍了Git允许使用未分类的更改进行分支更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有没有配置这个地方?

编辑:起初,我认为这是一个配置,我需要设置为禁止在分支之间进行更改,如果存在修改的非分页文件。但通过Emily的评论,似乎会提示您分支之间的文件是否有所不同,而不是以其他方式提示。

解决方案

它决定

一个快速实验显示如下。



假设你在分支 dev ,并且修改了 foo.txt 。如果不提交,请尝试检出 master 。两件事情之一会发生。


  1. 如果在 foo.txt > master 中的 dev 没有提交,因此不允许切换,因为 master 拥有与未分离的更改相冲突的文件的新版本。



    要检出因此,要求Git将 foo.txt 更新为新版本 master >有,摧毁你未分离的变化。为防止您丢失工作,它不会更改分支。


  2. 否则,修改已从版本 master 知道,你就可以改变分支。 Git不必更新文件,因为 master 没有关于该文件的新信息。



对于whoops更改



由于上述原因,如果您在一个分支上的文件中进行非暂存更改并意识到您确实想提交更改另一方面,您可能会也可能不会查看其他分支。



但是,您可以执行以下操作:


  • git stash save这里是我的更改摘要(摘要将显示在 git存储列表

  • git checkout otherbranch
  • c $ c> git stash pop
    (这是 git stash apply 和 git stash drop

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.

  1. If foo.txt was modified in master in a commit that dev doesn't have, you won't be allowed to switch without committing, because master has a "new" version of the file that conflicts with the unstaged changes.

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

  2. 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 because master 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 in git stash list)
  • git checkout otherbranch
  • git stash pop (which is a combination of git stash apply and git stash drop)

这篇关于Git允许使用未分类的更改进行分支更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆