| 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.
git stash push -u saves and removes all tracked staged and unstaged content plus untracked files from the current worktree. Check its scope first:
git status --porcelain
Use it only when all of that content belongs to you and you intend to restore it later. If status includes edits owned by someone else or files with an uncertain source, stop in this worktree and use a separate worktree.
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 diff --cached
git stash drop stash@{0}
Run git stash drop only after confirming that staged, unstaged, and untracked content has all been restored as expected; a successful apply alone is not enough to delete the saved state.
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.