Git
Stash Commands
Version Control
Software Development
Git Stash Pop vs Apply

Difference between git stash pop and git stash apply

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

git stash pop and git stash apply both restore previously stashed changes to your working directory. The key difference is what happens to the stash entry afterward: pop removes it from the stash stack, while apply keeps it. This distinction matters when you need to apply the same stash to multiple branches or want a safety net before applying changes.

Quick Comparison

CommandApplies ChangesRemoves Stash EntrySafe for Multi-Branch
git stash popYesYes (on success)No
git stash applyYesNoYes

git stash pop

Applies the stash and immediately removes it from the stash list:

bash
1# Save current changes
2git stash
3
4# Apply and remove from stash stack
5git stash pop
6
7# The stash entry is gone
8git stash list
9# (empty — the stash was removed)

If there is a merge conflict during pop, the stash is not removed. You must resolve the conflict and then manually drop the stash:

bash
1git stash pop
2# CONFLICT: Automatic merge failed
3
4# Resolve conflicts, then:
5git add .
6git stash drop

git stash apply

Applies the stash but keeps it in the stash list:

bash
1# Save current changes
2git stash
3
4# Apply but keep the stash
5git stash apply
6
7# Stash is still available
8git stash list
9# stash@{0}: WIP on main: abc1234 Some commit message

This is useful when you want to apply the same changes to multiple branches:

bash
1# Apply to feature-a
2git checkout feature-a
3git stash apply
4
5# Apply the same stash to feature-b
6git checkout feature-b
7git stash apply
8
9# Clean up when done
10git stash drop

Specifying Which Stash

Both commands default to the most recent stash (stash@{0}). You can specify a different one:

bash
1# List all stashes
2git stash list
3# stash@{0}: WIP on main: Fix header
4# stash@{1}: WIP on develop: Add logging
5# stash@{2}: WIP on main: Update config
6
7# Apply a specific stash
8git stash apply stash@{1}
9git stash pop stash@{2}

Stash Workflow Patterns

Quick Context Switch (pop)

bash
1# Working on feature, need to fix a bug
2git stash
3
4git checkout main
5# ... fix the bug, commit ...
6
7git checkout feature-branch
8git stash pop  # restore and clean up

Experimental Changes (apply)

bash
1# Trying a risky change
2git stash
3
4# Apply to test
5git stash apply
6# ... test the changes ...
7
8# If it breaks, reset and the stash is still safe
9git checkout -- .
10git stash apply  # try again differently

Named Stashes

bash
1# Save with a descriptive message
2git stash push -m "WIP: new login form styles"
3
4git stash list
5# stash@{0}: On feature: WIP: new login form styles

Stash with Specific Files

bash
1# Stash only specific files
2git stash push -m "partial work" -- src/login.js src/auth.js
3
4# Stash only staged changes
5git stash push --staged
6
7# Stash including untracked files
8git stash push --include-untracked

Viewing Stash Contents

bash
1# Show what's in the latest stash
2git stash show
3
4# Show as a diff
5git stash show -p
6
7# Show a specific stash
8git stash show -p stash@{2}

Common Pitfalls

  • Conflict handling: git stash pop does not remove the stash if there are conflicts. After resolving, you must git stash drop manually. Many developers forget this, leaving stale stash entries.
  • Wrong branch: Popping a stash on the wrong branch applies those changes there. Since pop removes the stash, you cannot easily undo this. Use apply when unsure, then drop after confirming.
  • Stash index drift: Stash indices shift when you add or drop stashes. stash@{1} after a pop becomes a different stash than before. Always check git stash list before applying by index.
  • Untracked files: By default, git stash does not include untracked files. Use git stash push --include-untracked or -u to include them.
  • Stash is not a substitute for commits: Stashes are local and temporary. For work you want to preserve long-term, create a WIP commit on a branch instead.

Summary

  • git stash pop applies changes and removes the stash entry (unless there are conflicts)
  • git stash apply applies changes and keeps the stash entry for reuse
  • Use apply when you need the same stash on multiple branches or want a safety net
  • Use pop for quick context switches where you just need to restore and move on
  • Always check git stash list to verify which stash you are targeting

Course illustration
Course illustration

All Rights Reserved.