Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undo git stash pop that results in merge conflict

Tags:

git

git-stash

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 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. This resulted in a bunch of merge conflicts and loss of a clean stash of my changes (since I used pop).

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

like image 791
acjay Avatar asked Mar 05 '14 19:03

acjay


People also ask

Can you revert git stash pop?

After that just fire git stash pop again and you get the same stash, that conflicted before. Keep in mind: The stash is safe, however, uncommitted changes in the working directory are of course not. They can get messed up. What I understand is that you can simply cleanup and pop again, but you can't undo it.

How do I resolve conflicts after git stash pop?

The stash entry is kept in case you need it again. There's no magic remedy for such merge conflicts. The only option for developers is to edit the file by hand and keep what they want and dispose of what they don't want. Once they merge and save the file, they will have effectively resolved the git stash conflict.

How do I abort a git stash merge conflict?

When popping out commits using the git stash pop command, you will have some merge conflicts sometimes, you can move to resolve those conflicts or abort the whole process. command. This has worked for me.

How do you undo a merge conflict?

You can use the git reset --merge command. You can also use the git merge --abort command. As always, make sure you have no uncommitted changes before you start a merge.


1 Answers

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:

  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}
like image 69
acjay Avatar answered Oct 01 '22 03:10

acjay