What's Actually Happening

When you stash changes, Git saves your working directory state. Later, when you apply the stash, Git tries to merge those saved changes into your current branch. If the branch changed in ways that conflict with the stashed changes, you get a merge conflict.

The Error You'll See

bash
$ git stash pop
CONFLICT (content): Merge conflict in src/app.js
src/app.js needs merge
The stash entry is kept in case you need it again.

Or:

bash
$ git stash apply stash@{0}
error: Your local changes to the following files would be overwritten by merge:
	src/config.js
Please commit your changes or stash them before you merge.
Aborting

Why This Happens

  1. 1.Branch changed since stash - New commits modified same files as stash
  2. 2.Multiple stashes - Wrong stash order causing conflicts
  3. 3.Partial stash - Stash didn't include all related changes
  4. 4.Working directory not clean - Uncommitted changes conflict with stash

Step 1: Check Stash List

bash
git stash list

Shows all stashes:

bash
stash@{0}: WIP on feature: abc123d Add config changes
stash@{1}: WIP on main: def4567 Bug fix attempt
stash@{2}: On feature: ghi890j Experimental changes

Step 2: Show Stash Content

Before applying, check what's in the stash:

```bash # Show stash changes git stash show stash@{0}

# Show full diff git stash show -p stash@{0}

# Show files changed git stash show --stat stash@{0} ```

Step 3: Handle Working Directory Conflict

If you have uncommitted changes blocking stash apply:

```bash # Check current changes git status

# Option A: Commit current changes first git add . git commit -m "Save current work"

# Option B: Stash current changes git stash push -m "temporary stash"

# Now apply the target stash git stash apply stash@{0} ```

Step 4: Resolve Apply Conflicts

When stash apply shows merge conflicts:

```bash git stash pop # CONFLICT (content): Merge conflict in src/app.js

# Check which files conflict git status

# Open conflicted file cat src/app.js ```

You see conflict markers:

javascript
<<<<<<< Updated upstream
current branch code
=======
stashed code
>>>>>>> Stashed changes

Resolve by editing the file:

```bash # Fix the conflict manually vim src/app.js

# Or use checkout to pick one version git checkout --ours src/app.js # Keep current branch version git checkout --theirs src/app.js # Keep stashed version

# Mark resolved git add src/app.js ```

Step 5: Complete After Conflict Resolution

After resolving all conflicts:

```bash # If using stash pop, stash is still kept after conflict # Drop it manually after resolution git stash drop stash@{0}

# Or if using stash apply, no extra step needed ```

Step 6: Apply Specific Files from Stash

If only some stashed files conflict:

```bash # Apply only specific file git checkout stash@{0} -- path/to/file.txt

# Apply only non-conflicting files first git stash show stash@{0} --stat # Apply file by file, skip conflicting ones git checkout stash@{0} -- clean-file.txt ```

Step 7: Create Branch from Stash

If conflicts are too complex, create a branch:

```bash # Create branch from stash git stash branch stash-work stash@{0}

# This creates new branch with stash applied # Now you can work on conflicts without affecting main branch ```

Step 8: Abort and Try Different Approach

If stash pop went wrong:

```bash # Abort the conflict resolution git reset --hard

# Stash is still saved, try apply instead of pop git stash apply stash@{0}

# Or try on a clean branch git checkout -b test-stash git stash apply stash@{0} ```

Step 9: Use Git Stash Show to Preview

Before applying to avoid conflicts:

```bash # Preview stash changes git stash show -p stash@{0}

# Compare with current branch git diff stash@{0}

# Check if files changed since stash git log --oneline HEAD..stash@{0}^ ```

Verify the Fix

After successful apply:

```bash # Check stash was applied git status git diff

# Stash list still has entry if using apply git stash list

# Verify file content is correct cat src/app.js ```

Prevention Tips

Reduce stash conflicts:

```bash # Stash with message for clarity git stash push -m "WIP: config changes for feature X"

# Apply on clean working directory git status # Ensure clean before apply

# Use stash branch for complex stashes git stash branch work-on-stash stash@{0} ```