Interruptions happen; stash lets you switch branches without committing half-done work.
`git stash` takes your uncommitted changes (staged and unstaged edits to tracked files), saves them on a stack, and resets your working tree to HEAD. You get a clean tree to switch branches, pull or test something, then bring the changes back.
Commands:
git stash push -m "wip: search": stash with a description (plaingit stashworks too).git stash -u: also stash untracked files, which are left behind by default.git stash list: showsstash@{0},stash@{1}... newest first.git stash show -p stash@{1}: see what's in one.git stash pop: apply the newest stash and drop it if it applied cleanly.git stash apply stash@{1}: apply without dropping, so you can apply it elsewhere too.git stash drop stash@{1}/git stash clear: delete.git stash branch new-branch: create a branch at the commit you stashed from and apply it there, which avoids conflicts when the original branch has moved on.
A stash can be applied on any branch, which makes it a quick way to move uncommitted edits you started on the wrong branch. Applying can conflict; if pop conflicts, the stash is kept, so after resolving you drop it yourself.
Stashes are easy to forget, and an old stash is hard to understand. For anything longer than a quick interruption, prefer a WIP commit on a branch, or a second worktree.
Example
# Mid-feature, an urgent bug report arrives
git stash push -u -m "wip: search filters"
git switch main && git switch -c hotfix/login
# ... fix, commit, push ...
git switch feature/search
git stash list
# stash@{0}: On feature/search: wip: search filters
git stash pop # changes are back, stash dropped
# Started on the wrong branch? Move edits over
git stash && git switch correct-branch && git stash popRemember
- stash saves tracked changes and cleans the tree; -u includes untracked files
- pop = apply + drop; apply keeps the stash
- A conflicting pop keeps the stash so nothing is lost
Watch out for
- Forgetting -u, switching branches, and finding new untracked files have followed you.