Git Reference ยท Merging and Rebasing
git reset command
Reference for git reset: move HEAD and optionally the index and working tree with --soft, --mixed, --hard, --keep and --merge, unstage paths, and recover with ORIG_HEAD.
git reset does one of two quite different things, depending on whether you give it a path. Without a path it moves the current branch (and HEAD) to another commit, and optionally makes the index and working tree match. With a path it copies the file from a commit into the index without touching the branch, which is how you unstage.
Git has three "places" that hold a version of your files: the commit HEAD points to, the index (staging area), and the working tree. The mode of reset decides how far the new state is carried: --soft moves only HEAD, --mixed (the default) also resets the index, and --hard resets all three. Only --hard can destroy uncommitted work.
git reset --soft HEAD~1 # undo the commit, keep everything staged git reset HEAD~1 # undo the commit, keep changes unstaged git reset --hard HEAD~1 # undo the commit and throw the changes away git reset -- file.txt # unstage a file
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
git reset [--soft | --mixed | --hard | --merge | --keep] [-q] [<commit>] git reset [-q] [<tree-ish>] [--] <pathspec>... git reset [-q] [--pathspec-from-file=<file> [--pathspec-file-nul]] [<tree-ish>] git reset (--patch | -p) [<tree-ish>] [--] [<pathspec>...]
| Mode | HEAD / branch | Index | Working tree |
|---|---|---|---|
--soft |
moved | kept | kept |
--mixed (default) |
moved | reset to <commit> |
kept |
--hard |
moved | reset | reset (local changes lost) |
--merge |
moved | reset | reset files that differ between HEAD and <commit>; keeps unstaged changes; aborts if the index has unmerged or changed files |
--keep |
moved | reset | reset files that differ between HEAD and <commit>; aborts if those files have local changes |
| Name | Description |
|---|---|
<commit> |
The target. Default: HEAD. Any revision, such as HEAD~2, a branch, a tag or a reflog entry like HEAD@{3}. |
<pathspec>... |
Limits the reset to these paths; it then only updates the index and never moves HEAD. Cannot be combined with --soft, --hard and the like. |
-p, --patch |
Choose hunks interactively to unstage. |
-q |
Quiet; also stops the "Unstaged changes after reset" list. |
--recurse-submodules |
Also update submodule working trees. |
--refresh, --no-refresh |
Refresh the index after a mixed reset (default on). |
Errors and edge cases: fatal: Cannot do hard reset with paths; fatal: ambiguous argument when the name could be a file and a revision (separate them with --). git reset on a repository without commits unstages everything (resets to the empty tree).
Usage notes
- Hard reset destroys uncommitted work in the working tree and index, and Git cannot bring it back. Committed work is recoverable from the reflog; uncommitted changes are not. Stash or commit first if in doubt.
- Recovering a reset. Before the move Git saves the old tip in
ORIG_HEAD, and the reflog records every move:git reset --hard ORIG_HEADorgit reset --hard HEAD@{1}undoes the reset. Seegit reflog. - Do not reset published history. If the branch is already pushed, resetting it and pushing again rewrites history for everyone; prefer
git revert. - Reset versus restore. Since Git 2.23,
git restore --staged <file>is the clearer way to unstage a file, andgit restore <file>throws away working tree edits.resetkeeps its path form for compatibility. - Reset versus checkout and switch.
resetmoves the branch you are on;git switchchanges which branch you are on and leaves branches where they are. - Squashing the last N commits.
git reset --soft HEAD~N && git commitreplaces them with one commit, keeping the combined changes. - Untracked files are never touched by
reset, even with--hard. Usegit cleanfor them. - Merge in progress.
git reset --mergeis the safe way to leave a failed merge (git merge --abortuses it) because it keeps your unrelated local changes. - Files that differ in mode only are reset like any other change under
--hard. - Large trees.
--hardrewrites every file that differs, which can be slow in huge repositories; sparse checkouts limit this.