How to Undo Git Stash Pop Resulting in Merge Conflict

If merge conflicts and loss of clean stash of changes are presented while running git stash pop then you can find anything related to this issue in this snippet. Follow this steps accordingly to get to the desired result.

Unstage the merge conflicts by running git reset with trailing dot:

git reset HEAD

Save the conflicted merge with git stash:

git stash

Go back to master with git checkout:

git checkout master

Now pull the latest changes with git fetch or git merge:

git fetch upstream

or

git merge upstream/master

Correct your new branch by running either:

git checkout new-branch

or

git rebase master

Apply the correct stashed changes with git stash apply:

git stash apply stash@{1}

The second step won’t work if there are unmerged paths. Check whether you have unmerged paths or not by executing:

git reset --mixed

Add git stash drop to get rid of the unwanted stash from the second step:

git stash drop

Stashing and Cleaning

Very often, when you are working on the part of your project, and things are in a dirty state, you can switch branches to work on something else and then get back to and re-apply them. This is possible with the git stash command. Stashing takes the messy state of the working directory and saves it on a stack of unfinished changes that you can return to them later. The git stash pop removes the changes from your stash and re-applies them to your working copy. The alternate way is git stash apply in case you want to re-apply the changes and keep them in your stash.

Merge Conflicts

Git allows users to merge commits from two branches through merging action. Files are merged automatically unless there are conflicting parts. A merge conflict occurs when Git cannot automatically resolve differences between two commits. Git will successfully merge conflict if all the changes in the code occur on different lines or in different files.

Merge conflict arises because Git recognizes which code to keep and which to discard.


Do you find this helpful?

Related articles