本文翻译自:Undo git stash pop that results in merge conflict

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. 为了转移它们,我想存放它们,然后将它们应用到master的新分支中。 I used git stash pop to transfer work-in-progress changes to this new branch, forgetting that I hadn't pulled new changes into master before creating the new branch. 我使用git stash pop将正在进行的更改转移到这个新分支,忘记了我在创建新分支之前并没有将新更改引入master。 This resulted in a bunch of merge conflicts and loss of a clean stash of my changes (since I used pop). 这导致了一堆合并冲突,并且丢失了我所做更改的干净存储(因为我使用了pop)。

Once I recreate the new branch correctly, how I can I recover my stashed changes to apply them properly? 正确重新创建新分支后,如何恢复已隐藏的更改以正确应用它们?


#1楼

参考:https://stackoom.com/question/1VB7Y/撤消git-stash-pop导致合并冲突


#2楼

As it turns out, Git is smart enough not to drop a stash if it doesn't apply cleanly. 事实证明,如果Git不能很好地应用,它足够聪明以至于不会丢下任何东西。 I was able to get to the desired state with the following steps: 我可以通过以下步骤达到所需的状态:

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

#3楼

Luckily git stash pop does not change 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 假设您的代码库之前是干净的,则可以使用以下命令返回该状态: git checkout -f
Then do the stuff you forgot, eg git merge missing-branch 然后做您忘记的事情,例如git merge missing-branch
After that just fire git stash pop again and you get the same stash, that conflicted before. 之后,再次启动git stash pop ,您将获得相同的 stash,之前有所冲突。

Attention: The stash is safe, however, uncommitted changes in the working directory are not. 注意:存储是安全的,但是,工作目录中未提交的更改不是。 They can get messed up. 他们可能会搞砸。


#4楼

Instructions here are a little complicated so I'm going to offer something more straightforward: 这里的说明有点复杂,所以我将提供一些更简单的方法:

  1. git reset HEAD --hard Abandon all changes to the current branch git reset HEAD --hard放弃对当前分支的所有更改

  2. ... Perform intermediary work as necessary ...根据需要执行中介工作

  3. git stash pop Re-pop the stash again at a later date when you're ready git stash pop准备好以后再重新弹出存储

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐