⇦ Back

WDX-180

Web Development X

The 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 revert command is recommended.

Syntax

git reset <mode-option> <commit-reference>

This is run in the terminal. The <mode-options> and <commit-reference> are discussed in more detail below.

Mode Options

The <mode-options> refer to how far reset will go when rolling back changes to a previous commit, including:

More specifically, these modes include:

If, for example, an error was made in a text file,example.txt, and the changes were accidentally added and committed, git reset can be used to go back to the state before that commit was made.

Referencing Commits

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: 05df67f9066c8ddd95c8d7bb2137acfb8b18e167 -> 05df67f

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.

Example

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 main
nothing to commit, working tree clean

The text above indicates the following:

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~1
git reset --mixed HEAD~1

Since the --mixed mode runs by default, both of the commands are identical in function. This will do the following:

If git status is run once more, this should appear on the terminal:

On branch main
Changes 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.txt

no changes added to commit (use "git add" and/or "git commit -a")

Project maintained by in-tech-gration Hosted on GitHub Pages — Theme by mattgraham