How to Choose Between git stash apply and git stash pop
In this tutorial, you'll learn about How to Choose Between git stash apply and git stash pop. We cover key concepts, practical examples, and best practices.
The Problem
You are unsure whether to use git stash apply or git stash pop when restoring stashed changes. Using pop can lose the stash if the changes conflict, while apply leaves the stash behind for later use.
Quick Fix
Use git stash apply When You Want to Keep the Stash
git stash
# Saved working directory and index state WIP on main: abc1234
git stash apply
# On branch main
# Changes not staged for commit:
echo "Stash still present:"
git stash list
# stash@{0}: WIP on main: abc1234
git stash apply restores the changes but keeps the stash entry, allowing you to apply the same stash to multiple branches or keep it as a backup.
Use git stash pop to Restore and Remove
git stash
# Saved working directory and index state WIP on main: abc1234
git stash pop
# On branch main
# Changes not staged for commit:
echo "Stash list after pop:"
git stash list
# (empty)
git stash pop restores the changes and removes the stash entry. It is equivalent to git stash apply && git stash drop.
Use git stash apply to Handle Conflicts Safely
git stash
# Saved working directory and index state WIP on main: abc1234
git stash apply
# Auto-merging file.txt
# CONFLICT (content): Merge conflict in file.txt
echo "Stash is preserved after conflict:"
git stash list
# stash@{0}: WIP on main: abc1234
If a conflict occurs with git stash pop, the stash is dropped regardless. Using apply first lets you resolve conflicts while keeping the stash as a fallback.
Use git stash drop to Clean Up Manually
git stash
git stash apply
# ... changes restored ...
git stash drop
# Dropped refs/stash@{0} (abc1234)
Apply the stash, verify everything works, then manually drop it. This two-step approach gives you a safety net if the restored changes are broken.
Use git stash show to Preview Changes
git stash show -p stash@{0}
# diff --git a/file.txt b/file.txt
# +new content
Preview what is inside a stash before applying it. This helps you decide whether to use apply or pop based on how complex the changes are and whether you might need to apply them multiple times.
Practice with a Test Repository
cd /tmp
mkdir git-practice && cd git-practice
git init --initial-branch=main
# Initialized empty Git repository in /tmp/git-practice/.git/
echo "test" > file.txt && git add . && git commit -m "init"
# [main (root-commit) abc1234] init
Before running destructive commands on your real repository, practice on a throwaway test repository. This builds confidence and prevents costly mistakes. The reflog is your safety net, but practice makes it less needed.
Prevention
- Use
git stash poponly when you are certain the stash will apply cleanly - Use
git stash applywhen working with complex conflicts or multiple stashes - Keep the stash until you have tested the restored changes in your working tree
- If a pop conflict loses your stash, recover it from
git fsck --lost-foundor the reflog
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro