0
0
Gitdevops~5 mins

git stash apply vs pop - CLI Comparison

Choose your learning style9 modes available
Introduction
Sometimes you need to save your work temporarily without committing. Git stash lets you do this. You can bring back your saved work with either git stash apply or git stash pop, but they behave a little differently.
When you want to save unfinished changes quickly to switch to another task without committing.
When you want to test something else but keep your current work safe.
When you want to bring back saved changes but keep a copy in stash for later use.
When you want to bring back saved changes and remove them from stash because you no longer need the backup.
Commands
This command saves your current changes to a stash and cleans your working directory so you can work on something else.
Terminal
git stash
Expected OutputExpected
Saved working directory and index state WIP on main: abc1234 Commit message
Shows all the stashes you have saved so far.
Terminal
git stash list
Expected OutputExpected
stash@{0}: WIP on main: abc1234 Commit message
Brings back the latest stash changes into your working directory but keeps the stash saved for later use.
Terminal
git stash apply
Expected OutputExpected
On branch main Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: example.txt no changes added to commit (use "git add" and/or "git commit -a")
--index - Try to reinstate the staged changes as well
Brings back the latest stash changes and removes that stash from the list because you no longer need it saved.
Terminal
git stash pop
Expected OutputExpected
On branch main Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: example.txt no changes added to commit (use "git add" and/or "git commit -a") Dropped refs/stash@{0} (abc1234 Commit message)
Key Concept

If you want to keep your saved changes after bringing them back, use git stash apply; if you want to bring them back and remove them from stash, use git stash pop.

Common Mistakes
Using git stash pop without checking for conflicts or errors.
If conflicts happen, the stash is removed but your working directory may be messy, making recovery harder.
Use git stash apply first to test changes, resolve conflicts, then use git stash drop to remove stash manually.
Assuming git stash apply removes the stash automatically.
The stash remains saved, which can cause confusion or clutter if not cleaned up.
After applying, run git stash drop to remove the stash if you no longer need it.
Summary
git stash saves your current work temporarily without committing.
git stash apply restores the saved changes but keeps the stash for reuse.
git stash pop restores the saved changes and deletes the stash entry.