撤消导致合并冲突的 git stash pop

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22207256/
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 17:50:57  来源:igfitidea点击:

Undo git stash pop that results in merge conflict

git

提问by acjay

I began making changes to my codebase, not realizing I was on an old topic branch. To transfer them, I wanted to stash them and then apply them to a new branch off of master. I used git stash popto transfer work-in-progress changes to this new branch, forgetting that I hadn't pulled new changes into master before creating the new branch. This resulted in a bunch of merge conflicts and loss of a clean stash of my changes (since I used pop).

我开始更改我的代码库,没有意识到我在一个旧的主题分支上。为了转移它们,我想把它们藏起来,然后将它们应用到 master 的一个新分支。我曾经git stash pop将正在进行的更改转移到这个新分支,忘记了在创建新分支之前我没有将新更改拉入 master。这导致了一堆合并冲突并丢失了我的更改的干净存储(因为我使用了 pop)。

Once I recreate the new branch correctly, how I can I recover my stashed changes to apply them properly?

正确重新创建新分支后,如何恢复隐藏的更改以正确应用它们?

回答by acjay

As it turns out, Git is smart enough not to drop a stash if it doesn't apply cleanly. I was able to get to the desired state with the following steps:

事实证明,Git 足够聪明,不会在应用不干净的情况下丢弃 stash。我能够通过以下步骤达到所需的状态:

  1. To unstage the merge conflicts: git reset HEAD .(note the trailing dot)
  2. To save the conflicted merge (just in case): git stash
  3. To return to master: git checkout master
  4. To pull latest changes: git fetch upstream; git merge upstream/master
  5. To correct my new branch: git checkout new-branch; git rebase master
  6. To apply the correct stashed changes (now 2nd on the stack): git stash apply stash@{1}
  1. 取消合并冲突:(git reset HEAD .注意尾随点)
  2. 保存冲突的合并(以防万一): git stash
  3. 回到主人: git checkout master
  4. 拉取最新更改: git fetch upstream; git merge upstream/master
  5. 要更正我的新分支: git checkout new-branch; git rebase master
  6. 要应用正确的隐藏更改(现在堆栈中的第二个): git stash apply stash@{1}

回答by flori

Luckily git stash popdoes notchange the stash in the case of a conflict!

幸运的是git stash pop没有改变藏匿在冲突的情况下!

So nothing, to worry about, just clean up your code and try it again.

所以没什么可担心的,只需清理您的代码并重试即可。

Say your codebase was clean before, you could go back to that state with: git checkout -f
Then do the stuff you forgot, e.g. git merge missing-branch
After that just fire git stash popagain and you get the samestash, that conflicted before.

假设你的代码库之前是干净的,你可以回到那个状态:git checkout -f
然后做你忘记的事情,例如,git merge missing-branch
在这之后git stash pop再次启动,你得到同样的存储,之前冲突。

Attention:The stash is safe, however, uncommittedchanges in the working directory are not. They can get messed up.

注意:存储是安全的,但是,工作目录中未提交的更改不是。他们可能会搞砸。

回答by anon58192932

Instructions here are a little complicated so I'm going to offer something more straightforward:

这里的说明有点复杂,所以我将提供一些更直接的内容:

  1. git reset HEAD --hardAbandon all changes to the current branch

  2. ...Perform intermediary work as necessary

  3. git stash popRe-pop the stash again at a later date when you're ready

  1. git reset HEAD --hard放弃对当前分支的所有更改

  2. ...必要时进行中介工作

  3. git stash pop准备好以后再重新弹出存储

回答by Allahbakash.G

git checkout -f

must work, if your previous state is clean.

必须工作,如果你以前的状态是干净的。