Bash

Fix Git Stash Pop Merge Conflicts and Leftover Stash Entries

You run git stash pop to bring back uncommitted work you set aside two hours ago, and Git dumps merge conflicts across five files. When you check git stash list, the stash entry is still there, your index is half-staged, and your working tree contains conflict markers.

text
Auto-merging src/server.js
CONFLICT (content): Merge conflict in src/server.js
Auto-merging package.json
CONFLICT (content): Merge conflict in package.json
The stash entry is kept in case you need it again.

If you panic and run git reset --hard, you wipe out your current changes without actually clearing the conflicted stash application cleanly. If you try to run git stash pop again, Git blocks you immediately:

text
error: Your local changes to the following files would be overwritten by merge:
  package.json
  src/server.js
Please commit your changes or stash them before you merge.
Aborting

Why Git leaves you in this state

A stash commit is not just one commit. It is a commit object with two, sometimes three parents: one for the base commit you were on, one for the index at the time of stashing, and an optional third parent if you used git stash -u to include untracked files.

When you run git stash pop, Git performs a three-way merge between:

  1. The commit your branch pointed to when you created the stash.
  2. The stash’s working-tree commit.
  3. Your current HEAD.

If the files changed on your current branch overlap with edits in the stash, the merge fails.

Here is the design choice that trips people up: pop means “apply, and if and only if the apply succeeds with zero conflicts, drop the stash entry from the reflog.” Because conflicts occurred, Git leaves stash@{0} intact. It does not drop it because dropping it would destroy your only clean copy of the stashed state if you botch the conflict resolution.

Git also stages some changes and leaves others unstaged. Files that merged cleanly go straight into the index, while files with conflicts are dumped into the working tree with standard conflict markers. You are left straddling a failed merge and a populated stash list.

Step 1: Decide whether to push forward or bail out

Before you touch any code, you need to decide if you want to fix the conflicts right now on your current branch, or abort completely back to where you were thirty seconds ago.

Option A: Aborting cleanly back to pre-pop state

If you popped the stash on the wrong branch, or the conflicts are so extensive that you want to back out and prepare the branch first, run this exact sequence.

First, wipe the failed merge from the working directory and unstage cleanly merged files:

bash
git checkout -f
git clean -df

git checkout -f forces Git to overwrite working tree changes with what is in HEAD, clearing all conflict markers and resetting tracked files. git clean -df removes any untracked files generated during the failed merge.

Now inspect your stash:

bash
git stash list
text
stash@{0}: WIP on feature/auth: 8a4c192 Add token verification logic

Your original stash entry is completely untouched. Your branch is back to its pristine state before you typed git stash pop.

Option B: Resolving the conflicts manually

If you are on the right branch and want to resolve the conflict right now, inspect which files are stuck:

bash
git status --short
text
M  package.json
UU src/server.js

The UU indicator means both you and the stash changed the same lines. Open src/server.js. Git renders the conflict with standard markers:

javascript
<<<<<<< Updated upstream
const PORT = process.env.APP_PORT || 8080;
const HOST = '0.0.0.0';
=======
const PORT = process.env.PORT || 3000;
const TIMEOUT = 5000;
>>>>>>> Stashed changes

Updated upstream represents the code on your branch (HEAD). Stashed changes represents the code from the stash.

Edit the file to the desired state:

javascript
const PORT = process.env.APP_PORT || 8080;
const HOST = '0.0.0.0';
const TIMEOUT = 5000;

Stage the resolved file:

bash
git add src/server.js

Once all files with UU markers are resolved and staged, check the status:

bash
git status
text
On branch feature/auth
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   package.json
	modified:   src/server.js

Because git stash pop does not clear the stash entry when conflicts occur, stash@{0} is still present. Now that you manually resolved the conflicts and staged the result, drop that stash entry yourself:

bash
git stash drop stash@{0}
text
Dropped stash@{0} (a99b4d8ec5a5e3bc9b61d368e539d9f1db71239c)

The safer alternative: Stash into an isolated branch

If your branch has diverged significantly from the branch where you made the stash, manual conflict resolution directly inside a dirty working tree is risky. Git has a built-in command that creates a brand-new branch specifically isolated for this stash: git stash branch.

This command checks out the exact commit your stash was based on, applies the stash there without conflicts, and drops the stash entry from the list.

Run this:

bash
git stash branch recovery-branch stash@{0}
text
Switched to a new branch 'recovery-branch'
On branch recovery-branch
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   package.json

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
	modified:   src/server.js

Dropped refs/stash@{0} (a99b4d8ec5a5e3bc9b61d368e539d9f1db71239c)

Because this branch branches off the original base commit, there are zero merge conflicts. From here, you commit your work:

bash
git add .
git commit -m "WIP changes restored from stash"

Now you have a clean commit on recovery-branch. You can rebase it onto your main working branch or cherry-pick it. Standard rebase conflict tooling is much better than dealing with unstaged stash conflicts.

bash
git checkout feature/auth
git merge recovery-branch

If conflicts occur during this merge, standard commands like git merge --abort work cleanly. That is much safer than stash conflict recovery.

Gotcha 1: Dropping the wrong stash index after a conflict

When you have multiple stashes, running git stash drop without an explicit index or with the wrong index is a common late-night mistake.

If you have:

text
stash@{0}: WIP on feature/payments
stash@{1}: WIP on feature/auth
stash@{2}: WIP on main

If you ran git stash apply stash@{1}, and it failed with conflicts, running git stash drop without arguments drops stash@{0}, not stash@{1}. Git defaults to index 0 unless specified. You just deleted your payments stash while trying to clean up your auth stash.

Always specify the exact stash reference:

bash
git stash drop stash@{1}

If you accidentally drop a stash you needed, you can find the dangling commit using git fsck:

bash
git fsck --unreachable | grep commit | cut -d' ' -f3 | xargs git log --merges --no-walk --grep="WIP on"

This command digs through orphan commits, searches for the automatic merge commit messages Git uses when creating stashes, and outputs the commit hashes. When you find the hash:

bash
git stash apply e3a1f9c4

Gotcha 2: The index state was not preserved

When you run git stash, Git stores your staged index changes and your unstaged working tree changes separately. But when you run git stash pop, Git attempts to merge all changes into the working tree, dropping your staging area separation.

Suppose you had staged package.json and left src/server.js unstaged before you ran git stash. If you pop that stash without flags:

bash
git stash pop

Both files will end up as unstaged changes in your working tree. If there is a conflict in package.json, Git stages the auto-merged parts and leaves conflict markers in the unstaged parts, creating an index that matches neither your pre-stash state nor your post-stash intention.

To preserve the staged state upon popping, use the --index flag:

bash
git stash pop --index

If a conflict occurs while using --index, Git aborts the pop immediately and does not apply anything:

text
error: could not restore untracked files from stash

This prevents the halfway-applied state where half your files are staged, some have conflict markers, and you cannot easily remember what was intended for staging and what was not.

Verifying the resolution

Once you resolve the conflicts and stage them, confirm your Git tree is stable.

First, check that your stash list no longer has the leftover entry:

bash
git stash list

Make sure the entry you worked on is gone. Next, verify that no conflict markers slipped into your codebase:

bash
git diff --check

If any <<<<<<<, =======, or >>>>>>> markers remain in any tracked file, this command prints the file and line number. If the output is empty, your conflict markers are fully cleaned up.

Finally, run your tests or check git status:

bash
git status --short

You should see clean M or A flags, no UU markers, and zero unmerged paths. You can now commit your resolved changes safely.

More in Bash