recovery
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 reset [<mode>] [<commit>] [-- <paths>] Common flags
| Flag | Purpose |
|---|---|
| --soft | Move HEAD only; keep index and working tree |
| --mixed | Default: move HEAD and reset index, keep working tree |
| --hard | Move HEAD, reset index AND working tree — discards changes |
| --keep | Reset like --hard but abort if local changes would be lost |
| -- <path> | Reset the index for specific paths without moving HEAD |
Examples
git reset HEAD~1 --soft Undo the last commit but keep changes staged
git reset --hard origin/main Force local branch to match remote (DESTRUCTIVE)
git reset -- src/app.js Unstage a file without altering its contents
Gotcha
'git reset --hard' permanently discards uncommitted work and can drop committed work if you rewind past it; recover via 'git reflog' if you moved fast.
Related commands
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.
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 stash
Shelves uncommitted changes onto a stack so you can switch context with a clean working tree. Later, reapply them with pop or apply.