Drill: Advanced Stash Usage¶
Goal¶
Use git stash to save work-in-progress changes with messages, stash specific files, and understand pop vs apply.
Setup¶
- A git repository with uncommitted changes
Commands¶
Set up a practice scenario:
mkdir /tmp/stash-drill && cd /tmp/stash-drill && git init
echo "base" > file1.txt && echo "base" > file2.txt && git add . && git commit -m "initial"
echo "wip change 1" > file1.txt
echo "wip change 2" > file2.txt
Stash with a descriptive message:
Stash only specific files:
echo "change1" > file1.txt && echo "change2" > file2.txt
git stash push -m "only file1 changes" -- file1.txt
Stash including untracked files:
List all stashes:
Show what a stash contains:
Apply a stash (keeps it in the stash list):
Pop a stash (applies and removes from list):
Drop a specific stash:
Create a branch from a stash:
Clear all stashes:
What to Look For¶
applykeeps the stash entry;popremoves it on successful apply- Stash messages make it easy to identify stashes in a long list
stash push -- <file>lets you stash only part of your working directorystash branchis useful when the stash conflicts with current HEAD
Common Mistakes¶
- Using
popinstead ofapplyand losing the stash reference if conflicts occur - Forgetting
-uto include untracked files in the stash - Not giving stashes descriptive messages and losing track of what each contains
- Using
git stash clearaccidentally and losing all stashed work