本文翻译自: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: 我可以通过以下步骤达到所需的状态:
- To unstage the merge conflicts:
git reset HEAD .要取消合并冲突:git reset HEAD .(note the trailing dot) (注意尾随点) - To save the conflicted merge (just in case):
git stash保存冲突的合并(以防万一):git stash - To return to master:
git checkout master返回主git checkout master:git checkout master - To pull latest changes:
git fetch upstream; git merge upstream/master进行最新更改:git fetch upstream; git merge upstream/mastergit fetch upstream; git merge upstream/master - To correct my new branch:
git checkout new-branch; git rebase master更正我的新分支:git checkout new-branch; git rebase mastergit checkout new-branch; git rebase master - 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: 这里的说明有点复杂,所以我将提供一些更简单的方法:
git reset HEAD --hardAbandon all changes to the current branchgit reset HEAD --hard放弃对当前分支的所有更改...Perform intermediary work as necessary...根据需要执行中介工作git stash popRe-pop the stash again at a later date when you're readygit stash pop准备好以后再重新弹出存储
解决git stash pop后的合并冲突
当尝试将工作进度从stash应用到新分支时,由于未先更新master分支,导致了合并冲突。Git在冲突时不会丢弃stash。可以通过取消暂存冲突、保存冲突、回退到master、拉取最新改动、修正新分支等步骤来恢复和正确应用stash。如果代码库之前是干净的,可以使用'git reset --hard HEAD^'回到先前状态,然后再执行stash应用。
7172

被折叠的 条评论
为什么被折叠?



