Git
pop vs apply
Learn the difference between stash pop and stash apply.
Restoring stash: pop and apply
There are two primary commands to restore stashed changes: git stash pop and git stash apply. git stash pop restores the top stash to your working directory and removes it from the stack on success. Meanwhile, git stash apply applies the changes but keeps the entry in the stack.
# Apply and remove from stack:
git stash pop
# Apply but keep in stack:
git stash apply
If you need to apply the same change across multiple branches, apply is the ideal choice.
Cleaning entries: drop and clear
To delete a stash after using apply or when it is no longer needed, use the git stash drop command. You can pass an index to delete a specific entry: git stash drop stash@{1}. If you want to delete all entries in the stack at once, run git stash clear.
git stash drop stash@{0}
git stash clear
Cleanup commands are irreversible, so ensure you have applied important changes beforehand.
Exercise Restore stash with pop
Restore the stashed changes using git stash pop -q and print the updated file contents with cat metin.txt.
Show example solution
git init -q -b main
git config user.name "Leyla"
git config user.email "leyla@example.com"
echo "orijinal" > metin.txt
git add metin.txt
git commit -qm "Ilk metin"
echo "eklenen satir" >> metin.txt
git stash push -qm "metin guncelleme"
git stash pop -q
cat metin.txt
Test yourself
-
What is the key difference between git stash apply and git stash pop?
- apply does not remove entry from stack, pop removes it after applying
- apply directly commits changes, pop does not
- apply removes stash entries from all branches simultaneously
- apply only restores untracked new files
Show answer
apply does not remove entry from stack, pop removes it after applying — git stash apply restores changes while preserving the entry in the stack; pop removes it from the stack after applying.
-
Delete stash entry from stack
git stash ____ stash@{0}
Show answer
git stash drop stash@{0}
-
Restoring and deleting
- git stash pop
- Applies and removes from stack
- git stash apply
- Applies but keeps in stack
- git stash drop
- Deletes specified stash entry
- git stash clear
- Clears the entire stash stack
Next lesson: Summary - Stash