reset command is used to change the state of the Git repository or undo commits.
Note: This command is ideal for undoing uncommitted changes made in a private, local repository. For undoing changes in a public, remote repository, the
revertcommand is recommended.
git reset <mode-option> <commit-reference>
This is run in the terminal. The
<commit-reference> are discussed in more detail below.
<mode-options> refer to how far
reset will go when rolling back changes to a previous commit, including:
- Where the
HEADis pointing towards (usually done with just
- Whether the staging area or index, reflects the commit the
HEADis now pointing towards.
- Whether the working tree is rolled back to reflect the changes reset in the
HEADand staging area.
More specifically, these modes include:
--soft: This rolls back to an earlier commit by moving the
HEADpointer towards it but leaving the staging area and working tree untouched, allowing for new commits to be made.
--mixed: In addition to moving the
HEADpointer to an earlier commit, the staging area is cleared to reflect the changes made in that commit (this runs by default).
--hard: This goes one step further and resets the working tree to reflect the previous commit reflected in the staging area and the
commit-reference refers to a commit’s unique hash, or save point, that was generated after creation. This hash is a long string that is a mix of characters and numbers that is usually represented by a shorter version:
git reset can be used with either the commit hash or with the
HEAD keyword, which refers to the commit being viewed on the currently checked-out branch.
Alternatively, a filename can be used in place of the
commit-reference to undo a
git add for a file that wasn’t meant to be staged for commit.
This is what the terminal would look like after creating a commit by accident on the
main branch and running a
git status check:
On branch mainnothing to commit, working tree clean
The text above indicates the following:
mainbranch is up to date, with the
HEADpointing towards the most recent commit.
- There is nothing to commit in the staging area.
- New changes haven’t been made yet in the working tree, hence why it is “clean”.
To set the
HEAD back by one commit as well as clear the staging area, one of the following commands can be run:
git reset HEAD~1git reset --mixed HEAD~1
--mixed mode runs by default, both of the commands are identical in function. This will do the following:
- It will move the
HEADpointer back by one (
- The staging area will be cleared of changes.
- The overall state of the
mainbranch is set to before changes in
example.txtwere added to the staging area for the previous commit.
git status is run once more, this should appear on the terminal:
On branch mainChanges not staged for commit:(use "git add <file>..." to update what will be committed)(use "git restore <file>..." to discard changes in working directory)modified: example.txtno changes added to commit (use "git add" and/or "git commit -a")