Live streamJoin us for "State of code review 2024" on July 18thRegister today

How to use Git reset HEAD

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


The git reset command is a powerful tool used to undo changes in a Git repository. It can be used to unstage files, alter commits, and even delete changes altogether. This guide explains the different modes of using git reset, particularly focusing on resetting the HEAD pointer to a previous state in the project's history.

HEAD in Git refers to the current pointer to the branch you are working on, which is essentially the last commit on that branch. When you use the git reset command, you change where the HEAD and potentially the current branch pointer points to in the history of the project.

There are three main types of resets in Git: soft, mixed, and hard. Each affects the repository in different ways:

  • Soft reset (--soft): This mode does not change the working directory or the staging area (index), but only the HEAD itself. Changes from reset commits move to the staging area.
  • Mixed reset (default): This resets the index but not the working directory. It undoes commits and leaves the changes as uncommitted in your local working directory.
  • Hard reset (--hard): This resets the index and working directory. Any changes to tracked files in the working directory since the commit to which HEAD is reset are discarded.

Always check your current status with git status before performing a reset to understand what changes are staged, committed, or uncommitted.

Terminal
git status

If you've staged files that you no longer wish to commit, you can unstage them with:

Terminal
git reset HEAD

This command will unstage all currently staged files.

To undo your last commit but keep the changes available in your staging area, use:

Terminal
git reset --soft HEAD~1

This command moves the HEAD back by one commit and keeps the changes in the staging area.

To undo your last commit and move the changes to your working directory, you can run:

Terminal
git reset HEAD~1

This will not touch your files; it only removes changes from the staging area.

Use this command with caution. If you want to discard changes in your working directory and staging area, resetting the HEAD to the previous commit, use:

Terminal
git reset --hard HEAD~1

If you need to reset your local branch to reflect a remote branch, first fetch the latest changes:

Terminal
git fetch origin

Then reset:

Terminal
git reset --hard origin/main

This will reset your local branch to match the main branch on the remote named origin.

  • Be cautious with git reset --hard: This command can permanently delete your work if not used carefully.
  • Regularly commit your work: More frequent commits mean you can afford to lose less work if you need to reset to an earlier point.
  • Use soft and mixed resets for most tasks: These are safer and allow more flexibility.
  • git reset HEAD: Unstages files.
  • git reset --soft HEAD~1: Moves HEAD back by one commit, changes are left staged.
  • git reset HEAD~1: Moves HEAD back by one commit, changes are left in working directory.
  • git reset --hard HEAD~1: Resets index and working directory to previous commit, discards all changes.
  • git reset --hard origin/main: Resets local branch to match remote main branch.

For further reading see the official Git documentation.

Stay unblocked. Ship faster.
Experience the new developer workflow - create, review, and merge code continuously. Get started with one command.
Get started

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2