The Hook (The "Byte-Sized" Intro)
You stashed your work. Now you want it back. Two commands, one decision: apply keeps a copy in the stash (safe bet), pop applies and removes it (clean and done). Pick wrong and you either have stale stashes piling up or accidentally lose your only copy when conflicts occur. Here's how to choose right every time.
📖 What is Applying Stashes?
Applying a stash means restoring the saved changes back into your working tree. git stash apply keeps the stash entry, git stash pop removes it after applying.
Conceptual Clarity
| Command | Restores Changes? | Removes from Stash? | Safe? |
|---|---|---|---|
git stash apply | ✅ Yes | ❌ No (stays in stack) | ✅ Very — you keep a backup |
git stash pop | ✅ Yes | ✅ Yes (removed on success) | ⚠️ Stash is gone after |
git stash apply stash@{2} | ✅ Yes (specific stash) | ❌ No | ✅ |
git stash pop stash@{2} | ✅ Yes (specific stash) | ✅ Yes | ⚠️ |
Important edge case: If pop encounters a merge conflict, Git applies the changes but does NOT remove the stash (so you don't lose it). You must resolve the conflict, then manually git stash drop when ready.
Real-Life Analogy
apply= Borrowing a book from the library (it stays on the shelf for others or for you to borrow again)pop= Taking a book off the "free" shelf (once you take it, it's gone)
Visual Architecture
Why It Matters
- Choosing correctly:
applywhen you might need the stash again;popwhen you're done with it. - Conflict handling: Know that
popdoesn't remove the stash if conflicts occur — your work is safe. - Cross-branch: You can apply a stash to a different branch than where you created it.
- Selective: Apply a specific stash by index, not just the most recent one.
Code
# ─── Apply the latest stash (keep it in stack) ───
git stash apply
# Changes restored, stash still exists at stash@{0}
# ─── Pop the latest stash (apply + remove) ───
git stash pop
# Changes restored, stash removed from stack
# ─── Apply a specific stash by index ───
git stash apply stash@{2}
# ─── Pop a specific stash ───
git stash pop stash@{1}
# ─── Apply stash on a different branch ───
git switch main
git stash apply
# Works even if the stash was created on feature/login
# ─── Handle conflict during pop ───
git stash pop
# CONFLICT (content): Merge conflict in app.js
# Stash is NOT removed (safety net)
# Resolve the conflict:
git add app.js
git commit -m "Resolve stash conflict"
# Manually remove the stash:
git stash drop stash@{0}
# ─── Restore staged vs unstaged state ───
git stash apply --index
# Restores staged files to staged area (not just working tree)Key Takeaways
applykeeps the stash;popremoves it after successful apply.- If
pophits a conflict, the stash is preserved — your work isn't lost. - Use
stash@{N}to apply a specific stash, not just the most recent. - Use
--indexto preserve the staged/unstaged distinction when reapplying.
Interview Prep
-
Q: What is the difference between
git stash applyandgit stash pop? A: Both restore stashed changes to the working tree.applykeeps the stash entry in the stack for future use.popremoves the stash entry after a successful apply. Ifpopencounters a conflict, the stash is preserved. -
Q: Can you apply a stash to a different branch? A: Yes. Stashes are not tied to a specific branch. You can create a stash on
feature/login, switch tomain, and rungit stash apply— the changes will be applied to your current branch. -
Q: What does
git stash apply --indexdo? A: It restores both the working tree changes AND the staging area state. Without--index, all stashed changes are applied as unstaged modifications, losing the distinction between what was staged and what wasn't.