Undo git stash pop that results in merge conflict

924    Asked by CarlPaige in Devops , Asked on Apr 13, 2021

 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?

Answered by Carl Paige

First step to undo git stash pop, unstage the merge conflicts using the following command:

  $ git reset HEAD .

Then save the conflicted merge

  $ git stash

Now return to master

  $ git checkout master

In order to pull latest changes:

  $ git fetch upstream; git merge upstream/master

In order to correct my new branch:

  $ git checkout new-branch; git rebase master

In order to apply the correct stashed changes (now 2nd on the stack):

  $ git stash apply stash@{1}


Your Answer

Answer (1)

If you have run git stash pop and encountered merge conflicts, you can undo this operation to revert to the state before the stash was popped. Here’s a step-by-step guide to handle this situation:

1. Check the Current State

First, ensure you are aware of the current state of your working directory and index.

  git status

2. Resolve Conflicts (Optional)

If you prefer to resolve conflicts manually, you can proceed with resolving the conflicts in the files indicated by Git and then commit the changes. If you want to undo the stash pop, follow the steps below.

3. Undo the Stash Pop

Since git stash pop is essentially git stash apply followed by git stash drop, you can follow these steps to undo it:

Step 1: Reset the Working Directory

Use git reset to revert the changes introduced by the stash pop. This will discard all changes made to your working directory and index.

  git reset --hard HEAD

Step 2: Reapply the Stash

Reapply the stash without dropping it. This helps ensure that the stash changes are not lost and you can attempt to reapply them later.

  git stash apply stash@{0}

4. Alternative Approach: Using the Reflog

If the above method does not work or if you want to revert to a previous state more accurately, you can use the Git reflog to find the commit before the stash pop and reset to it.

Step 1: Check the Reflog

The reflog keeps a record of all recent changes to the tip of branches. Use it to find the commit before you applied the stash.

git reflogLook for the commit hash right before the stash pop. It will usually have a message like stash: ....

Step 2: Reset to the Previous Commit

Once you have identified the correct commit hash, reset to that commit. This will revert your repository to the state before the stash pop.

  git reset --hard 

5. Restash the Changes (Optional)

If you still want to keep the changes in the stash, you can create a new stash from the current state.

  git stash

By following these steps, you can effectively undo a git stash pop that resulted in merge conflicts and revert your working directory to a clean state.








3 Months

Interviews

Parent Categories