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

← All Git commands · Git cheat sheet · Git rebase vs merge