Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Devops
  3. Question
Devops

Undo git stash pop that results in merge conflict

Asked by Carl Paige Apr 13, 2021 7.1K views 2 answers
Share

About this question

 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?

Your answer

2 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on Jul 22, 2024

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.








Was this helpful?

More Devops discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest DevOps Blogs

Guides, tips and career advice on DevOps from JanBask experts.