pop vs apply
- Git course
- Intermediate
- Lesson 19/82
- 4 min
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 applyIf 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 clearCleanup commands are irreversible, so ensure you have applied important changes beforehand.
Practice Restore stash with pop
Restore the stashed changes using git stash pop -q and print the updated file contents with cat metin.txt.
Starter code
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"
# Stash'i pop ile uygula ve dosya icerigini yazdirSample 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.txtExpected output
orijinal eklenen satir
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 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 Correct answer
- apply directly commits changes, pop does not
- apply removes stash entries from all branches simultaneously
- apply only restores untracked new files
Explanation: git stash apply restores changes while preserving the entry in the stack; pop removes it from the stack after applying.
2 Delete stash entry from stack
git stash ____ stash@{0}
git stash drop stash@{0}
Explanation: The git stash drop command permanently removes the specified stash entry from the stack.
3 Restoring and deleting
| Term | Meaning |
|---|---|
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 |