Git Aliases for Stash Operations
Create git aliases for stash operations including named stashes, listing, and applying. Manage your stash stack efficiently with shortcuts.
Detailed Explanation
Stash Aliases
Git stash is a powerful tool for temporarily shelving changes, but its commands are verbose. These aliases make stash operations quick and intuitive:
[alias]
save = stash push -m
pop = stash pop
sl = stash list
sa = stash apply
sd = stash drop
sshow = stash show -p
Named Stashes with save
Always name your stashes. Anonymous stashes quickly become mysterious:
git save "WIP: refactoring user module"
git save "Experiment: new caching strategy"
Compared to unnamed stashes:
stash@{0}: WIP: refactoring user module # Clear purpose
stash@{1}: Experiment: new caching strategy # Clear purpose
stash@{2}: WIP on main: a1b2c3d Fix typo # What was this?
Stash List (sl)
View your stash stack:
git sl
# stash@{0}: On feature: WIP: refactoring user module
# stash@{1}: On main: Experiment: new caching strategy
Apply vs Pop
| Command | Effect |
|---|---|
git pop |
Apply latest stash and remove it from the stack |
git sa |
Apply latest stash but keep it on the stack |
git sa stash@{2} |
Apply specific stash by index |
Use pop when you are done with the stash. Use apply when you want to apply the same changes to multiple branches.
Show Stash Contents (sshow)
Before applying, preview what a stash contains:
git sshow # Show latest stash diff
git sshow stash@{1} # Show specific stash diff
Including Untracked Files
By default, stash only saves tracked files. To include untracked files:
[alias]
saveu = stash push -u -m
The -u flag includes untracked files, which is essential when you have created new files that are not yet staged.
Use Case
Use these aliases when you frequently context-switch between tasks and need to temporarily save work in progress. Named stashes are especially valuable in complex projects where you might have multiple stashes and need to remember what each one contains.