git stash
Temporarily shelve changes so you can work on something else
Practical Example
git stash # save current changes git checkout other-branch # work on something else git checkout main git stash pop # restore changes and remove from stack
Command Overview & How It Works
Saves your modified tracked files onto a stack of temporary snapshots, reverting your working directory to the last committed state. This is useful when you need to switch branches urgently but are in the middle of unfinished work. Use git stash pop to reapply the most recent stash and remove it from the stack, or git stash apply to reapply it without removing it (useful for applying the same stash to multiple branches). Use git stash list to view all stashes, git stash show -p to see the diff of a stash, and git stash drop to remove a specific stash. Use git stash -u (or --include-untracked) to also stash untracked files, and git stash -a (or --all) to stash everything including ignored files. Use git stash branch <name> to create a new branch from a stash — helpful when you realize you are on the wrong branch.
Practical Tips
- •Stash unfinished work before switching branches: git stash, switch, do the fix, switch back, git stash pop.
- •Stash untracked files too with git stash -u, or everything including ignored files with git stash -a.
- •If you pop a stash on the wrong branch, apply it where it belongs instead — the stash is not lost until you drop it.