The difference between git stash pop
and git stash apply
lies in how they handle the stash after reapplying your changes:
Command | Action | Stash Deletion? | Use Case |
---|---|---|---|
git stash apply | Applies the stashed changes to your working directory. | ❌ Keeps the stash in the stash list. | Use when you want to test or reuse the stash later. |
git stash pop | Applies the stashed changes and deletes the stash (if successful). | ✅ Removes the stash from the list. | Use when you’re done with the stash and want to clean up the stash list. |
Key Details:
- Default Behavior:
- Both commands apply the latest stash (
stash@{0}
) by default. - Example:
bash git stash apply # Applies but keeps stash git stash pop # Applies and deletes stash
- Specifying a Stash:
- You can target a specific stash (e.g.,
stash@{2}
):bash git stash apply stash@{2} # Keeps stash@{2} git stash pop stash@{2} # Deletes stash@{2} after applying
- Conflict Handling:
- If conflicts occur during application:
apply
: The stash remains in the list for troubleshooting.pop
: The stash is only deleted if the application succeeds (no conflicts).
Example Workflow:
- Stash Changes:
git stash save "Work in progress"
- Reapply Stash:
- Keep the stash (for later reuse):
bash git stash apply
- Apply and remove the stash (clean up):
bash git stash pop
When to Use Which:
- Use
apply
if: - You want to test the stashed changes before committing to them.
- You might need to reuse the same stash later.
- Use
pop
if: - You’re confident the stashed changes are ready to be integrated.
- You want to avoid clutter in your stash list.