git reset
git
Usage
git reset [--soft|--mixed|--hard] <commit> | git reset <path>Moves the branch pointer to another commit: --soft keeps everything staged, --mixed (the default) also clears the staging area but leaves your edits in the files, and --hard throws those edits away too, unrecoverably.
Common flags
| Flag | Meaning |
|---|---|
| --soft | move the branch only; index and files keep everything, so the change stays staged |
| --mixed | default: also clear the staging area, leaving edits in your files |
| --hard | also overwrite your files; uncommitted work is destroyed |
| <path> | no commit given: unstage that path |
| --keep | move but refuse if it would overwrite a local change |
| --merge | like --hard but keeps changes that do not collide |
Examples
git reset --soft HEAD~1undoes the last commit and leaves everything staged
git reset HEAD~1undoes the last commit; the changes sit in your files, unstaged
git reset src/app.tsunstages one file without changing it
git reset --hard origin/mainthrows away local commits AND uncommitted edits
The confusion is that undo has several shapes — moving where a branch points is not the same as adding a commit that reverses it.
How to read this
- Square brackets [ ] mark a part you may leave out.
- An ellipsis … means you can list more than one.
- Flags are case-sensitive — in some commands -r and -R do different things.
Questions
Q. What does git reset do?
Moves the branch pointer to another commit: --soft keeps everything staged, --mixed (the default) also clears the staging area but leaves your edits in the files, and --hard throws those edits away too, unrecoverably.
Q. How do I type it?
git reset [--soft|--mixed|--hard] <commit> | git reset <path> — square brackets mark the parts you can leave out.
Q. How many flags are worth knowing?
6 are listed here; the full set is in man git. This command sits under git.
Related commands
man page: man git-reset