W3docs

How to Undo Git Stash Pop Resulting in Merge Conflict

This tutorial will give the answer to the question of undoing git stash which results merge conflict in detail. Read about stashing and merge conflicts.

If merge conflicts occur and your stash becomes dirty while running git stash pop, this snippet explains how to undo the operation.

Steps to undo a git stash pop

Follow these steps to resolve the issue:

Unstaging merge conflicts

Unstage the conflicting files by running git reset with a trailing dot:

Unstage the merge conflicts

git reset HEAD .

Stashing

Save the conflicted state with git stash:

Save the conflicted state

git stash

This step will fail if there are unmerged paths. Check for unmerged paths by executing:

Check for unmerged paths

git status

If unmerged paths exist, remove the unwanted stash created in this step:

Remove the unwanted stash

git stash drop

Switching to master

Switch to the master branch with git checkout:

Switch to master

git checkout master

Pulling changes

Pull the latest changes from the remote repository:

Pull the latest changes

git pull upstream master

Switching to the new branch

Switch to your new branch and update it by running:

Switch to the new branch

git checkout <new-branch>

Update the branch

git rebase master

Applying stashes

Apply the previously stashed changes with git stash apply:

Apply the stashed changes

git stash apply stash@{1}

If conflicts arise, check for unmerged paths by executing:

Check for unmerged paths

git status

Stashing and Cleaning

When your working directory is in a dirty state, you can switch branches to work on something else and return later to re-apply your changes. This is handled by the git stash command. Stashing saves the current state of the working directory onto a stack of unfinished changes that you can return to later. git stash pop removes the changes from your stash and re-applies them to your working copy. Alternatively, use git stash apply if you want to re-apply the changes while keeping them in your stash.

Merge Conflicts

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

A merge conflict arises because Git cannot automatically determine which code to keep and which to discard.