好吧,假设有一天我们碰巧做了一堆修改,当我们提交它们时,我们注意到我们在错误的分支上工作。

我们如何在不丢弃本地更改的情况下强制git切换分支?

在等待回复的过程中,我可能会以一种天真的方式谈论这个问题,但我想知道是否有正确的程序,因为如果我说我以前没有遇到过这种情况,那我就是在撒谎……

备份更改的回购 Git重置——很难 Git签出 恢复的变化 Git commit -m "changes"


当前回答

Git保存保存未提交的更改 Git存储列表列出您保存的未提交的存储 Git应用stash@{x},其中x可以是0,1,2..没有你藏过的东西

其他回答

有很多不同的方法,这取决于你走了多远,以及你想要它们在哪个分支上。

让我们来看一个典型的错误:

$ git checkout master
... pause for coffee, etc ...
... return, edit a bunch of stuff, then: oops, wanted to be on develop

因此,现在您希望这些您尚未承诺掌握的更改处于开发状态。

If you don't have a develop yet, the method is trivial: $ git checkout -b develop This creates a new develop branch starting from wherever you are now. Now you can commit and the new stuff is all on develop. You do have a develop. See if Git will let you switch without doing anything: $ git checkout develop This will either succeed, or complain. If it succeeds, great! Just commit. If not (error: Your local changes to the following files would be overwritten ...), you still have lots of options. The easiest is probably git stash (as all the other answer-ers that beat me to clicking post said). Run git stash save or git stash push,1 or just plain git stash which is short for save / push: $ git stash This commits your code (yes, it really does make some commits) using a weird non-branch-y method. The commits it makes are not "on" any branch but are now safely stored in the repository, so you can now switch branches, then "apply" the stash: $ git checkout develop Switched to branch 'develop' $ git stash apply If all goes well, and you like the results, you should then git stash drop the stash. This deletes the reference to the weird non-branch-y commits. (They're still in the repository, and can sometimes be retrieved in an emergency, but for most purposes, you should consider them gone at that point.)

apply步骤使用Git强大的底层合并机制对存储的更改进行合并,这与执行分支合并时使用的东西是一样的。这意味着如果您错误地处理的分支与您打算处理的分支有足够大的不同,那么您可能会得到“合并冲突”。因此,即使Git本身没有检测到任何合并冲突,在您假定存储被干净地应用之前,仔细检查结果也是一个好主意。

Many people use git stash pop, which is short-hand for git stash apply && git stash drop. That's fine as far as it goes, but it means that if the application results in a mess, and you decide you don't want to proceed down this path, you can't get the stash back easily. That's why I recommend separate apply, inspect results, drop only if/when satisfied. (This does of course introduce another point where you can take another coffee break and forget what you were doing, come back, and do the wrong thing, so it's not a perfect cure.)


1 git stash save中的save是创建新stash的旧动词。Git版本2.13引入了新的动词,使其与pop更加一致,并为创建命令添加了更多选项。Git版本2.16正式弃用了旧的动词(尽管它在Git 2.23中仍然有效,这是我编辑本文时的最新版本)。

你可以:

使用git隐藏来搁置您的更改或, 创建另一个分支并在那里提交更改,然后将该分支合并到工作目录中

你可以使用:

Git保存你的工作 Git checkout <your-branch> Git stash apply或Git stash pop加载你的最后一个工作


当您想要暂时保存未完成或凌乱的工作,而又想在另一个分支上做一些事情时,Git stash非常有用。

Git -stash文档

使用git stash

git stash

它将更改推入堆栈。当你想把它们拉回来的时候使用

git stash apply

你甚至可以把单独的项目拉出来。

彻底吹走藏匿物:

git stash clear

你可以使用——merge/-m git checkout选项: Git checkout -m <another branch>

- m -合并 切换分支时,如果您对一个或多个文件的本地修改在当前分支和 要切换到的分支,该命令拒绝切换 分支,以便在上下文中保存您的修改。然而, 使用此选项,将在当前分支和您的分支之间进行三向合并 工作树的内容,新分支完成后,您将进入 新分支。

来源:https://git-scm.com/docs/git-checkout