0
0
GitHow-ToBeginner · 3 min read

How to Apply a Specific Stash in Git Quickly

Use git stash list to find the stash you want, then apply it with git stash apply <stash@{index}>. This applies the chosen stash without removing it from the stash list.
📐

Syntax

The command to apply a specific stash in Git uses the stash reference from the stash list.

  • git stash apply <stash@{index}>: Applies the stash at the given index without deleting it.
  • git stash pop <stash@{index}>: Applies the stash and removes it from the stash list.

The <stash@{index}> is the identifier shown in git stash list.

bash
git stash apply stash@{2}
💻

Example

This example shows how to list all stashes, then apply the second stash in the list.

bash
git stash list
git stash apply stash@{1}
Output
stash@{0}: WIP on main: 123abc Fix header stash@{1}: WIP on main: 456def Add footer stash@{2}: WIP on main: 789ghi Update styles # After applying stash@{1}, your working directory will have the changes from that stash applied.
⚠️

Common Pitfalls

Common mistakes include:

  • Not specifying the stash index, which applies the latest stash by default.
  • Confusing git stash apply with git stash pop. The latter removes the stash after applying.
  • Applying a stash that conflicts with current changes, causing merge conflicts.

Always check your stash list and current changes before applying.

bash
git stash apply
# applies latest stash

git stash pop stash@{3}
# applies and removes stash@{3}

# Wrong: git stash apply 3
# Correct: git stash apply stash@{3}
📊

Quick Reference

CommandDescription
git stash listShow all saved stashes with their indexes
git stash apply stash@{n}Apply stash at index n without removing it
git stash pop stash@{n}Apply stash at index n and remove it from stash list
git stash drop stash@{n}Delete stash at index n without applying

Key Takeaways

Use git stash list to find the stash index before applying.
Apply a specific stash with git stash apply stash@{index} to keep it in the stash list.
Use git stash pop stash@{index} to apply and remove the stash.
Avoid conflicts by ensuring your working directory is clean or ready for merge.
Always specify the full stash reference like stash@{2}, not just the number.