0
0
GitHow-ToBeginner · 3 min read

How to Use git stash apply: Syntax and Examples

Use git stash apply to reapply the most recent saved changes from your stash to your current working directory without removing them from the stash list. You can specify a particular stash by adding its name, like git stash apply stash@{2}.
📐

Syntax

The basic syntax of git stash apply is simple:

  • git stash apply: Applies the latest stash.
  • git stash apply <stash-name>: Applies a specific stash by name, e.g., stash@{1}.
  • The command reapplies changes but keeps the stash entry intact.
bash
git stash apply [<stash>]
💻

Example

This example shows how to save changes, list stashes, and apply a stash back to your working directory.

bash
git stash push -m "work in progress"
git stash list
git stash apply stash@{0}
Output
Saved working directory and index state WIP on main: 123abc4 work in progress stash@{0}: WIP on main: 123abc4 work in progress On branch main Changes applied from stash@{0}
⚠️

Common Pitfalls

Common mistakes when using git stash apply include:

  • Expecting the stash to be removed after applying. It stays until you run git stash drop or git stash pop.
  • Applying a stash that conflicts with current changes, causing merge conflicts.
  • Not specifying the stash name when multiple stashes exist, which applies the latest by default.
bash
git stash apply
# If conflicts occur, resolve them manually

# Wrong: expecting stash to be removed
# Right: use git stash pop to apply and remove stash
📊

Quick Reference

CommandDescription
git stash applyApply the latest stash without removing it
git stash apply stash@{n}Apply a specific stash by index
git stash popApply the latest stash and remove it from stash list
git stash listShow all saved stashes
git stash drop stash@{n}Remove a specific stash

Key Takeaways

Use git stash apply to reapply saved changes without deleting the stash.
Specify the stash name like stash@{1} to apply a particular stash.
Conflicts can happen if current changes clash with stashed changes; resolve them manually.
To apply and remove a stash in one step, use git stash pop instead.
Always check your stash list with git stash list before applying.