my-git

Stash

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.

Suitable Scenarios

Basic Usage

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}

Choosing Between apply and pop

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}

Common Misconceptions

1. Treating Stash as Long-Term Storage

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.

2. Forgetting to Save Untracked Files

By default, git stash might not save untracked files. Use:

git stash push -u

3. Not Cleaning Up When Stashes Accumulate

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.

Further Reading