Lesson Completion
Back to course

Applying Stashes

Beginner
8 minutes4.8Git

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

CommandRestores 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

flowchart LR STASH["📦 Stash Stack"] -->|"apply"| WT["💻 Working Tree"] STASH -->|"pop"| WT STASH -.->|"apply: stash kept"| STASH STASH -.->|"pop: stash removed"| GONE["🗑️ Removed"] style STASH fill:#0f3460,stroke:#ffd700,color:#ffd700 style WT fill:#1a1a2e,stroke:#53d8fb,color:#53d8fb style GONE fill:#2d1b1b,stroke:#e94560,color:#e94560

Why It Matters

  • Choosing correctly: apply when you might need the stash again; pop when you're done with it.
  • Conflict handling: Know that pop doesn'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

bash
# ─── 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

  • apply keeps the stash; pop removes it after successful apply.
  • If pop hits 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 --index to preserve the staged/unstaged distinction when reapplying.

Interview Prep

  • Q: What is the difference between git stash apply and git stash pop? A: Both restore stashed changes to the working tree. apply keeps the stash entry in the stack for future use. pop removes the stash entry after a successful apply. If pop encounters 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 to main, and run git stash apply — the changes will be applied to your current branch.

  • Q: What does git stash apply --index do? 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.

Topics Covered

Git StashingGit Fundamentals

Tags

#git#stash#apply#pop#beginner-friendly

Last Updated

2026-02-12