Git 允许使用非暂存更改进行分支更改 [英] Git allows for branch change with unstaged changes

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

问题描述

Git 允许我在更改未暂存以进行提交(修改的文件)时更改分支.

Git is allowing me to change branches when I have changes not staged for commit (modified files).

在某处有这个配置吗?

起初我认为这是一个配置,如果有修改的未暂存文件,我需要设置它以禁止在分支之间进行更改.但是根据 Emily 的评论,如果分支之间的文件不同,似乎会提示您,否则不会提示您.

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.

假设您在分支 dev 上并且您已经修改了 foo.txt.在不提交的情况下,您尝试查看 master.两件事情中的一件会发生.

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. 如果 foo.txtmaster 中的 dev 没有的提交中被修改,你将不会允许在不提交的情况下切换,因为 master 有一个new"与未暂存更改冲突的文件版本.

  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.

结帐"因此,master 将要求 Git 将 foo.txt 更新到 master 拥有的较新版本,从而破坏未暂存的更改.为防止您丢失工作,它不会更改分支.

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.

否则,修改已经因为"master 知道的版本,您将能够更改分支.Git 不必更新文件,因为 master 没有关于文件的新信息.

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 这是我的更改摘要"(摘要将显示在 git stash list 中)
  • git checkout otherbranch
  • git stash pop(它是 git stash applygit stash drop 的组合)
  • 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天全站免登陆