- Git Stash Pop Merge Conflicts - The Problem
- Undo Git Stash Pop With Conflicts - Abort the Bad Merges to Return to a Clean State
- 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.
- Either abort the bad merges and return to the previous clean state.
- 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:
We then fork a
new_branch local branch from it. We modify a few files in this branch and commit those changes.
We now make a few changes in
file2.txt. We then
stash these changes.
git stash -u -m "Modify file1 and file2 in new_branch"
git stash takes a few options here. The
-u flag lets us stash the untracked changes.
-m flag carries the same meaning - it lets us add a semantic message to our stash.
We now make some changes in the same
file2.txt in our
master branch. These files have different versions in our
If we want to apply the stash in the
master branch, these different versions of
file2 will cause conflicts.
If we now check our repository, we find weird files in it because of the bad merge attempt.
We can use the
reset command with some options to git undo failed stash pop.
git reset --merge
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 checkout -f
We can see the same result above using the
git checkout command by passing the
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.
-f flag is for the
--force option. It helps by ignoring unmerged commits and untracked files to ensure a healthy repository state.
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
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
file2 in our
master after the fork caused the problem.
master to the commit before the commit with the bad changes to our
git reset <good_commit_hash>
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
In this form, the
git checkout command sets the working area to align with
HEAD, the last good commit without conflicts.
We now see the commit is gone from our
We now apply our stashed changes successfully.
git stash pop
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.
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
We stash some changes on this branch.
git stash -m "Change file5.txt in local branch"
Now we realize we want these changes on a new local branch. We fork a new
local branch from our
git checkout master git branch local_branch_2
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.
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
git merge origin/master
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.
We will now
rebase our local branch,
local_branch_2, on the updated
checkout the local branch.
git checkout local_branch_2
rebase it on the local master.
git rebase master
This makes our
local_branch_2 healthy and ready to receive the stashed changes.
We now apply our stashed changes.
git stash pop
We see this time the stashed changes apply successfully.
We have resolved the conflicts and successfully merged the stashed changes.