How to Apply Stash in Git: Simple Commands and Examples
To apply a stash in Git, use the command
git stash apply which restores the most recent saved changes without removing them from the stash list. You can also apply a specific stash by adding its name, like git stash apply stash@{1}.Syntax
The basic syntax to apply a stash in Git is:
git stash apply: Applies the latest stash.git stash apply <stash-name>: Applies a specific stash by its identifier.
Applying a stash restores your saved changes to your working directory but keeps the stash in the list for reuse or later removal.
bash
git stash apply [<stash-name>]
Example
This example shows how to save changes to a stash and then apply them back to your working directory.
bash
git stash push -m "work in progress" git stash list git stash apply
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
Your branch is up to date with 'origin/main'.
Changes applied from stash@{0}.
Common Pitfalls
Common mistakes when applying stash include:
- Not specifying the stash name when multiple stashes exist, which applies only the latest stash.
- Expecting
git stash applyto remove the stash; it does not remove it, so the stash remains unless dropped manually. - Conflicts can occur if the stash changes clash with current files; you must resolve conflicts manually.
bash
git stash apply # Applies latest stash but does not remove it
git stash drop # Use this to remove a stash after applying
# Wrong: expecting stash to be removed automatically
# Right:
git stash apply
# then
git stash dropQuick Reference
| Command | Description |
|---|---|
| git stash push -m "message" | Save current changes to a new stash with a message |
| git stash list | Show all saved stashes |
| git stash apply | Apply the latest stash without removing it |
| git stash apply stash@{n} | Apply a specific stash by index |
| git stash drop stash@{n} | Remove a specific stash from the list |
| git stash pop | Apply the latest stash and remove it from the list |
Key Takeaways
Use
git stash apply to restore saved changes without deleting the stash.Specify the stash name like
stash@{1} to apply a particular stash.Conflicts may happen when applying stash; resolve them manually.
Stashes remain after applying unless you remove them with
git stash drop.Use
git stash pop to apply and remove the stash in one step.