You are halfway through implementing a complex feature when an urgent production bug alert arrives. You must switch to main immediately to fix it. But you don't want to create an embarrassing "half-done unfinished code" commit. Git Stash takes your dirty working state, saves it to an internal clipboard stack, and restores a pristine working directory.
# Quick stash with default message:
$ git stash
# Recommended: Stash with a descriptive note:
$ git stash push -m "WIP: unfinished refactor of auth middleware"
Saved working directory and index state On feature/auth: WIP: unfinished refactor of auth middleware
Your working directory is now completely clean (matching HEAD), allowing you to switch branches safely!
-uBy default, git stash only saves tracked files. If you created brand-new files that haven't been committed before, pass -u (or --include-untracked):
$ git stash -u -m "WIP: include new component templates"
Git maintains your stashes in a Last-In, First-Out (LIFO) stack:
# List all saved stashes:
$ git stash list
stash@{0}: On feature/auth: WIP: unfinished refactor of auth middleware
stash@{1}: On main: WIP: temporary database connection experiment
# Inspect the diff of the top stash without applying it:
$ git stash show -p stash@{0}
git stash pop (Apply AND Remove)Re-applies the changes from stash@{0} to your current working directory and immediately deletes it from the stash stack:
$ git stash pop
git stash apply (Apply and KEEP on Stack)Applies the changes to your working directory but leaves the stash saved in your stash stack (ideal if you want to apply the same changes to multiple branches):
$ git stash apply
# Apply a specific stash from history:
$ git stash apply stash@{1}
# Delete a specific stash:
$ git stash drop stash@{0}
# Delete ALL stashes across your entire repository:
$ git stash clear
git stash branch)If you stashed code, switched branches, and upon trying to pop your stash you encounter massive merge conflicts, turn the stash into its own dedicated branch:
$ git stash branch feature/resumed-auth stash@{0}
Git checks out the exact commit you were on when you created the stash, creates a new branch named feature/resumed-auth, and cleanly pops your stash with zero conflicts!
Strengthen your version control mastery by studying these connected classes:
Choose between permanently discarding edits or saving them to the stash stack.
Prevent checkout errors caused by dirty uncommitted working tree modifications.
Turn complex stashes into dedicated feature branches with git stash branch.
Practice stashing untracked files, applying specific stash indices, and popping.
Advance your software engineering skills with the next topic in our synchronized curriculum.
Continue to Managing Remotes → Previous: ← Interactive Rebase (-i) | Full Syllabus