CodeBlanks Logo Icon CodeBlanks

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.

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

terminal
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 yazdir

Sample solution

terminal
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

Expected 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

Question
git stash ____ stash@{0}
Answer
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

All lessons in Intermediate

20 Intermediate final

  1. Branch management
  2. Rebase
  3. Conflict
  4. PR flow