recovery
git stash
Shelves uncommitted changes onto a stack so you can switch context with a clean working tree. Later, reapply them with pop or apply.
git stash [push | pop | apply | list | drop] [<options>] Common flags
| Flag | Purpose |
|---|---|
| push -m <msg> | Stash with an explanatory message |
| -u / --include-untracked | Also stash new untracked files |
| -a / --all | Include ignored files too |
| pop | Reapply the newest stash and remove it from the stack |
| apply | Reapply the stash but keep it in the list |
| -p / --patch | Interactively pick hunks to stash |
| drop <stash> | Delete a specific stash entry |
Examples
git stash push -m "WIP header refactor" Save labeled work-in-progress
git stash pop Reapply and remove the latest stash
git stash list Show all stash entries with their messages
Gotcha
Untracked files are NOT stashed unless you pass -u; without it, new files stay in your working tree and may block a checkout.
Related commands
git restore
Modern command (Git 2.23+) for restoring working tree files from the index or a commit, or unstaging files. Replaces the confusing file-mode of 'git checkout'.
git reset
Moves the current branch tip to a specified commit, optionally updating the index and working tree. Used to unstage files or rewind local history.
git checkout
Legacy multipurpose command that switches branches, restores files, or detaches HEAD to a commit. In modern Git (2.23+), prefer 'git switch' for branches and 'git restore' for files.
git revert
Creates a new commit that inverts the changes of a previous commit, preserving history. The safe way to undo pushed commits on shared branches.
git reflog
Shows the log of updates to branch tips and HEAD stored locally, even after commits are dropped from history. The rescue rope for recovering 'lost' commits.