混帐隐藏 – >合并隐藏的变化与当前的变化

我对分支做了一些修改,意识到我忘记了对分支进行了一些必要的修改。 我想要的是一种合并我的隐藏的变化与当前的变化。

有没有办法做到这一点?

为了方便起见,我最终放弃了,首先承诺了我现在的变化,然后我的变化,但我宁愿一举把它们弄进去。

运行git stash popgit stash apply基本上是一个合并。 您不应该需要提交当前的更改,除非在工作副本中更改的文件也发生了更改,在这种情况下,您将看到以下错误消息:

 error: Your local changes to the following files would be overwritten by merge: file.txt Please, commit your changes or stash them before you can merge. Aborting 

在这种情况下,您无法一次性将存储应用于当前的更改。 您可以提交更改,应用隐藏,再次提交,并使用git rebase这两个提交,如果您确实不需要两次提交,但这可能会更麻烦,这是值得的。

我刚刚发现,如果你的未提交的更改被添加到索引(即“上演”,使用“git add …”),那么“git stash apply”(和,大概是“git stash pop”)实际上会做一个适当的合并。 如果没有冲突,你是金。 如果没有,像往常一样用“git mergetool”解决它们,或者用编辑器手动解决它们。

要清楚的是,这是我正在谈论的过程:

 mkdir test-repo && cd test-repo && git init echo test > test.txt git add test.txt && git commit -m "Initial version" # here's the interesting part: # make a local change and stash it: echo test2 > test.txt git stash # make a different local change: echo test3 > test.txt # try to apply the previous changes: git stash apply # git complains "Cannot apply to a dirty working tree, please stage your changes" # add "test3" changes to the index, then re-try the stash: git add test.txt git stash apply # git says: "Auto-merging test.txt" # git says: "CONFLICT (content): Merge conflict in test.txt" 

…这可能是你在找什么。

我想要的是一种合并我的隐藏的变化与当前的变化

这是另一个select:

 git stash show -p|git apply git stash drop 

git stash show -p将显示上次保存的隐藏的补丁。 git apply会应用它。 合并完成后,可以使用git stash drop来删除合并的stash。

正如@Brandan所build议的,这是我需要做的

 error: Your local changes to the following files would be overwritten by merge: file.txt Please, commit your changes or stash them before you can merge. Aborting 

按照这个过程:

 git status # local changes to `file` git stash list # further changes to `file` we want to merge git commit -m "WIP" file git stash pop git commit -m "WIP2" file git rebase -i HEAD^^ # I always use interactive rebase -- I'm sure you could do this in a single command with the simplicity of this process -- basically squash HEAD into HEAD^ # mark the second commit to squash into the first using your EDITOR git reset HEAD^ 

而且你将剩下完全合并的本地 file修改,准备做进一步的工作/清理或做出一个好的提交。 或者,如果你知道file的合并内容是正确的,你可以写一个拟合消息并跳过git reset HEAD^

另一种select是做另一个“git stash”本地未提交的更改,然后结合这两个git stashes。 不幸的是,git似乎没有办法轻松地组合两个窗口。 所以一个选项是创build两个.diff文件并将它们应用 – 以免它们不是额外的提交,也不涉及十步处理:

如何: https : //stackoverflow.com/a/9658688/32453