🌐 Detecting your location…
📢 Advertisement — Configure AdSense in Appearance → Customize → AdSense Settings

Git Stash: Complete Guide — Save, Apply, Pop, Drop, and Branch from Stash

⏱️5 min read  ·  1,021 words

git stash saves your working directory changes temporarily so you can switch context without committing incomplete work. It’s one of the most useful but underutilized Git commands. This guide covers every subcommand with practical examples.

🔑 Key Takeaway

git stash saves your working directory changes temporarily so you can switch context without committing incomplete work. It’s one of the most useful but underutilized Git commands.

Core Concept

# You're in the middle of feature work and need to switch branches
git status
# M  src/auth.ts    ← modified
# M  src/utils.ts   ← modified
# ?? src/new-file.ts  ← untracked

git checkout main  # ERROR: would overwrite changes

# git stash saves and cleans your working directory
git stash
# Saved working directory and index state WIP on feature: abc123 Add auth

git checkout main  # ✅ now clean working tree

# ... do what you needed on main ...

git checkout feature
git stash pop      # restore your changes

git stash Commands

# Save stash with a message (recommended — harder to forget what it was)
git stash push -m "WIP: auth middleware refactor"

# Short form (saves tracked changes only)
git stash

# Include untracked files
git stash push --include-untracked   # or -u

# Include untracked AND ignored files (rare)
git stash push --all                 # or -a

# Partial stash — choose specific files
git stash push -m "just auth changes" -- src/auth.ts src/auth.test.ts

Listing and Inspecting Stashes

# List all stashes
git stash list
# stash@{0}: WIP on feature: abc123 Add auth
# stash@{1}: WIP on main: def456 Fix bug
# stash@{2}: On feature: Manual save before experiment

# Show what's in a stash (summary)
git stash show            # most recent
git stash show stash@{1} # specific stash

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

Restoring: pop vs apply

# git stash pop — apply AND remove from stash list
git stash pop              # applies stash@{0} and removes it
git stash pop stash@{2}   # apply a specific stash

# git stash apply — apply but KEEP in stash list
git stash apply            # applies stash@{0}, keeps it in list
git stash apply stash@{1}

# When to use apply:
# - When you want to apply the same stash to multiple branches
# - When you're not sure the apply will be clean and want to retry
# - As a safety measure — you can always drop manually after

Conflicts When Applying Stash

# If the stash conflicts with current branch state:
git stash pop
# CONFLICT (content): Merge conflict in src/auth.ts
# The stash entry is NOT removed when conflicts occur

# Fix conflicts normally
git add src/auth.ts
git stash drop    # manually remove the stash after resolving
# (pop would have removed it automatically if no conflicts)

Branching from a Stash

# Create a new branch FROM the commit where the stash was saved
# and apply the stash to it — useful when stash won't apply cleanly to current branch
git stash branch my-new-branch stash@{0}

# This is equivalent to:
# git checkout -b my-new-branch stash-parent-commit
# git stash apply stash@{0}
# git stash drop stash@{0}

Removing Stashes

# Remove specific stash
git stash drop stash@{0}

# Remove most recent stash
git stash drop

# Remove ALL stashes (dangerous — no undo)
git stash clear

Partial Stash with Interactive Mode

# Interactively choose which hunks to stash (like git add -p)
git stash push --patch    # or -p

# Shows each changed hunk and asks: stash this hunk? (y/n/d/a/e/?)
# Useful when a file has changes you want to stash and changes you want to keep

Common Developer Workflows

# Workflow 1: Quick context switch
git stash push -m "WIP: new feature"
git checkout main
git pull origin main
# ... fix urgent bug ...
git checkout feature
git stash pop

# Workflow 2: Stash before risky rebase
git stash push -u -m "Before rebase backup"
git rebase origin/main
# If rebase goes wrong: git stash pop restores your state
# (though git reflog is better for full recovery)

# Workflow 3: Test clean build
git stash push -u          # stash everything including untracked
npm run build              # verify clean build works
git stash pop              # restore your work

# Workflow 4: Apply stash to different branch
git stash push -m "shared config changes"
git checkout feature-a
git stash apply stash@{0}  # apply (keep in list)
git checkout feature-b
git stash apply stash@{0}  # apply to second branch too
git stash drop stash@{0}   # now drop it

git stash vs git commit –amend vs git worktree

Scenario Best Tool
Quick context switch (same machine, 5-30 min) git stash
Work on two branches simultaneously git worktree add
Save WIP with ability to push to remote git commit -m "WIP" (fixup later)
Undo last commit but keep changes git reset HEAD~1

Frequently Asked Questions

Q: Does git stash work across branches?
A: Yes. Stash is a global stack — not branch-specific. You can stash on branch A, switch to branch B, and pop the stash there. If the files conflict with branch B, you’ll get a merge conflict to resolve.

Q: Will git stash save untracked files?
A: Not by default. Use git stash push -u (or --include-untracked) to include new untracked files. Use git stash push -a (or --all) to include both untracked and gitignored files.

Q: I accidentally dropped my stash — can I recover it?
A: Possibly. Run git fsck --no-reflog | awk '/dangling commit/ {print $3}' to find dangling commits. Then git show <hash> to identify your stash content, and git stash apply <hash> to restore it. Works within ~30 days before Git garbage collection.

Q: How many stashes can I have?
A: No practical limit. But if you have 10+ stashes, consider committing WIP or using git worktree instead — a long stash list is a sign of deferred decisions.

Q: Can I stash only staged changes?
A: Yes: git stash push --staged stashes only what’s in the index (staged), leaving unstaged changes in the working directory. Useful for separating changes into smaller logical units.

Conclusion

git stash is essential for clean context switching without committing incomplete work. The key commands: git stash push -u -m "description" to save (including untracked), git stash pop to restore and remove, git stash apply to restore and keep. Use git stash branch when the stash won’t apply cleanly to your current branch. The stash is a LIFO stack — maintain it actively, drop stashes you no longer need, and don’t let it accumulate beyond 3-4 entries.

✍️ Leave a Comment

Your email address will not be published. Required fields are marked *