Git stash Command
In this page:
What is Git Stash?
git stash takes your uncommitted changes (staged and unstaged) and stores them on a separate stack, resetting your working directory to match the last commit. This is the quickest way to switch tasks — check out another branch, fix an urgent bug — without committing half-finished work just to clear your tree.
Example: What is Git Stash?
git stash
Listing and Viewing Stashes
git stash list shows every stash you've saved, most recent first, and git stash show -p stash@{n} lets you view the actual diff inside a specific one before deciding whether to bring it back. This matters once you have more than one stash sitting around and can't remember which is which.
Example: Listing and Viewing Stashes
git stash list
git stash show -p stash@{0}
Applying and Popping Stash
git stash apply reapplies a stash's changes to your working directory but leaves it in the stash list for later reuse — handy if you want the same changes on multiple branches. git stash pop does the same but also removes it from the list, which is what you want for the common one-time case.
Example: Applying and Popping Stash
git stash apply
git stash pop
Stashing Specific Files
You're not limited to stashing everything at once — git stash push -- <file> stashes only specific files, and git stash push --patch lets you interactively choose individual hunks, useful when only part of your working changes are ready to be set aside.
Example: Stashing Specific Files
git stash push -- file.txt
Cleaning the Stash
Old stashes just sit there taking up space and cluttering the list if you never clean them up. git stash drop stash@{n} removes one specific entry, while git stash clear wipes the entire stash list — the latter is irreversible, so double-check you don't need any of them first.
Example: Cleaning the Stash
git stash drop stash@{0}
git stash clear
Chapter Quiz — Complete all 18 topics to unlock
0/18 topics done
Complete these topics first:
- Git init Command
- Git New Files
- Git Staging Environment
- Git clone Command
- Git status Command
- Git help Command
- Git add Command
- Git commit Command
- Git tags Command
- Git stash Command
- Git log Command
- Git diff Command
- Git show Command
- Git rm Command
- Git mv Command
- Git restore Command
- Git clean Command
- Git ignore (.gitignore)