Undo Git Stash Pop Conflicts

  1. Git Stash Pop Merge Conflicts - The Problem
  2. Undo Git Stash Pop With Conflicts - Abort the Bad Merges to Return to a Clean State
  3. Undo Git Stash Pop by Resolving The Bad Conflicts

You can undo git stash pop with merge conflicts with the solutions in this article. We show you how to abort the bad stash pop operation and return to a clean state.

But we also demonstrate a method to resolve the conflicts and undo git stash pop with new good merges.

Git Stash Pop Merge Conflicts - The Problem

It is common in a fast-paced developer’s workflow to stash the current state and jump to other features as new ideas come up.

When we finish our work on the new feature, we apply the stashed changes with the git stash pop command. But sometimes, this back and forth workflow results in merge conflicts.

You may want to do one of two things in such a situation.

  1. Either abort the bad merges and return to the previous clean state.
  2. You may want to edit the files/directories locally or pull from a remote repository to resolve the merge conflicts. You can then merge the correct changes with fresh commits.

Let us see both the solutions.

Undo Git Stash Pop With Conflicts - Abort the Bad Merges to Return to a Clean State

You should use any of the following commands if you want to remove the changes in the bad stash pop operation. These methods will abort all the changes that caused the merge conflict and return to the previous healthy state.

git reset --merge

First, let’s look at the setup. We have a master branch with a few files, like so:

master branch initial setup

We then fork a new_branch local branch from it. We modify a few files in this branch and commit those changes.

new branch initial setup

We now make a few changes in file1.txt and file2.txt. We then stash these changes.

git stash -u -m "Modify file1 and file2 in new_branch"

The git stash takes a few options here. The -u flag lets us stash the untracked changes.

The -m flag carries the same meaning - it lets us add a semantic message to our stash.

stash in new branch

We now make some changes in the same file1.txt and file2.txt in our master branch. These files have different versions in our master and new_branch.

modify files master after fork

If we want to apply the stash in the master branch, these different versions of file1 and file2 will cause conflicts.

git stash pop conflicts

If we now check our repository, we find weird files in it because of the bad merge attempt.

git stash pop conflicts weird behavior

We can use the reset command with some options to git undo failed stash pop.

git reset --merge

The git reset has recently learned the --merge option. The --merge option is like the default --mixed option, but it only applies to the files affected by the merge operation.

We see the result of this command is to git undo bad stash pop in our case.

git reset merge

git checkout -f

We can see the same result above using the git checkout command by passing the -f flag.

git checkout -f

Without any argument to it, the git checkout command takes the default HEAD argument. Our last commit was good without the bad stash pop merge conflicts, and so this command wipes the slate clean.

The -f flag is for the --force option. It helps by ignoring unmerged commits and untracked files to ensure a healthy repository state.

git checkout force option

The above two methods are best to undo git stash pop with conflicts if you only want to wipe them clean.

But if you want to resolve the commits with fresh good merges, you need to use the methods below.

Undo Git Stash Pop by Resolving The Bad Conflicts

You need to edit your files and directories nicely in sync in all the branches to resolve conflicts.

You could do this locally with a few commands or pull it in from your remote repository. We will look at both solutions.

Resolve Conflicts to Git Undo Failed Stash Pop - For Local Branches

We have the same setup as above. But this time, we will resolve the conflicts between our main and new_branch and reapply our stash to get the desired result.

We first remove the changes in our files that cause the stash pop conflicts. The change we made in file1 and file2 in our master after the fork caused the problem.

We reset our master to the commit before the commit with the bad changes to our file1 and file2.

git reset <good_commit_hash>

git reset bad commit

git checkout HEAD .

We also roll back our working area with the checkout command. Note the trailing dot ., which selects all the files to ensure all conflicts resolve.

You could also use:

git reset HEAD file1.txt file2.txt

Because in our case, we know the two relevant files are file1.txt and file2.txt.

In this form, the git checkout command sets the working area to align with HEAD, the last good commit without conflicts.

git checkout head

We now see the commit is gone from our master branch.

bad commit gone local setup

We now apply our stashed changes successfully.

git stash pop

git good stash pop

successful resolve apply stash

Git Undo Bad Stash Pop - Resolve Conflicts By Pulling From Remote Repository

If your workflow is set up such that the remote repository has the least healthy state, you can resolve the bad stash conflicts by pulling in from the remote.

Let’s first set up a concrete use case for this solution.

healthy remote

local master behind remote

We see our local remote is behind our remote main by 1 commit. Suppose we now create a new local branch tracking the remote master and stash some changes.

git fetch --all

git switch -c new_branch_tracking_remote_master origin/master

local branch tracking remote master

We stash some changes on this branch.

git stash -m "Change file5.txt in local branch"

stash local tracking remote master

Now we realize we want these changes on a new local branch. We fork a new local branch from our local master.

git checkout master

git branch local_branch_2

new fork from local master

We see this branch is behind by 1 commit, and it does not have the file5.txt file. If we now pop our stash, it leads to bad merge conflicts.

bad git pop remote workflow

To resolve conflicts, in this case, we pull the remote master into our local master. We first need to fetch the remote repository.

git fetch --all

We then merge our local master with the remote master.

git merge origin/master

pull local master from remote

We now see our local master is in sync with the remote master by pulling the latest changes. You can also see it has the crucial file5.txt in it.

updated local master

We will now rebase our local branch, local_branch_2, on the updated local master.

First, checkout the local branch.

git checkout local_branch_2

Now, rebase it on the local master.

git rebase master

This makes our local_branch_2 healthy and ready to receive the stashed changes.

rebase local on master

We now apply our stashed changes.

git stash pop

We see this time the stashed changes apply successfully.

git stash pop success remote setup

We have resolved the conflicts and successfully merged the stashed changes.

success git stash pop remote setup

Resources:

  1. https://melvingeorge.me/blog/abort-git-stash-process-if-merge-conflicts-git
  2. https://www.theserverside.com/video/How-to-easily-merge-and-resolve-git-stash-pop-conflicts
  3. https://melvingeorge.me/blog/abort-git-stash-process-if-merge-conflicts-git
  4. https://newbedev.com/git-stash-blunder-git-stash-pop-and-ended-up-with-merge-conflicts
  5. https://stackoverflow.com/questions/4778882/how-to-launch-and-edit-a-file-from-git-using-notepad
Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Git Stash

  • Recover Stash in Git
  • Apply Stashes in Git
  • Clear Stash in Git