| English | 中文 |
git stash is used to temporarily save uncommitted changes.
Use it when you have unfinished code but need to switch branches, pull code, or fix a hotfix right now.
Save current changes, including untracked files:
git stash push -u -m "wip: describe current work"
View the stash list:
git stash list
Apply a specific stash, but keep the stash record:
git stash apply stash@{0}
Apply and delete the stash:
git stash pop
Delete a specific stash:
git stash drop stash@{0}
For important changes, prioritize using apply.
pop deletes the stash after a successful apply. If a conflict occurs during apply, recovering cleanly is harder.
A safer workflow:
git stash apply stash@{0}
git status
git diff
git stash drop stash@{0}
Stash is meant for temporary saving, not for long-term storage of important work.
Important work should be committed to a distinct branch as soon as possible.
By default, git stash might not save untracked files. Use:
git stash push -u
When there are too many stashes, it is hard to know what each save point is for.
Add -m every time to explain the purpose.