git stash
Temporarily save uncommitted changes and restore them later, keeping your working directory clean.
You're in the middle of working on a feature when an urgent bug report comes in.
You need to switch branches immediately, but you're not ready to commit your
half-finished work. git stash saves your uncommitted changes temporarily,
giving you a clean working directory so you can switch branches, then lets you
restore those changes later.
Running git stash takes all your modified tracked files and staged changes,
saves them to a stack, and reverts your working directory to match the last
commit. Now you can switch branches, pull updates, or do whatever you need with
a clean slate. When you're ready to continue your original work, git stash pop
restores the most recent stash and removes it from the stack.
You can stash multiple times, building up a stack of saved changes. Running
git stash list shows all your stashes with identifiers like stash@{0} (most
recent), stash@{1}, etc. To restore a specific stash, use
git stash apply stash@{1}. The difference between pop and apply is that pop
removes the stash after applying, while apply keeps it for potential reuse.
Adding a message makes stashes easier to identify:
git stash save "WIP: user authentication" or
git stash push -m "half-done API changes". Later, git stash list shows your
message alongside each stash, helping you remember what work each one contains.
Stashing Untracked and Ignored Files
By default, stash only saves tracked files (files Git already knows about). New
files you created but haven't added yet stay in your working directory. To stash
those too, use git stash -u (or --include-untracked). To stash everything
including ignored files, use git stash -a (or --all), though this is rare
since you usually don't want temporary build artifacts in stashes.
If you realize you don't need a stash anymore, git stash drop deletes the most
recent one, or git stash drop stash@{2} deletes a specific stash. To clear all
stashes, use git stash clear, though be careful—deleted stashes are hard to
recover.
Pull Request Context
In pull request workflows, stashing is common when switching context. You're working on your feature branch when you need to quickly review someone's PR. You stash your changes, check out their branch, test it, leave review comments, then switch back to your branch and pop your stash to continue where you left off.
Stashing also helps when pulling causes conflicts you're not ready to handle. If
you pull and get conflicts, you can git merge --abort, stash your changes,
pull cleanly, then apply your stash and resolve conflicts in a more controlled
way.
Understanding stash means having a temporary parking spot for work in progress. It's not a replacement for commits—stashed changes aren't part of your repository history and can be lost if you're not careful. Use stash for short-term storage when switching contexts, then commit properly when you're ready to save work permanently.
