How to use Git reset HEAD

Kenny DuMez
Kenny DuMez
Graphite software engineer
Try Graphite


Note

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


Table of contents

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. For more specific scenarios, you might also want to check out our guides on how to remove files from staging and how to remove files from commits.

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. For a more detailed guide on managing staged files, see our comprehensive guide on removing files from staging.

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

For a more detailed explanation of hard resets and their implications, check out our dedicated guide on git reset --hard HEAD.

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.

git reset moves the HEAD pointer to a previous commit, effectively "erasing" commits from history, while git revert creates a new commit that undoes the changes from a previous commit, preserving the history. Use git reset for local changes you haven't pushed, and git revert for changes that have been shared with others.

If you haven't run garbage collection yet, you might be able to recover files using git reflog to find the commit hash before the reset, then use git reset --hard <commit-hash> to restore. However, this is not guaranteed, so always be careful with hard resets.

When you use git reset HEAD (which is the same as git reset --mixed HEAD), your staged changes are moved back to the working directory as unstaged changes. Your files remain unchanged, but they're no longer staged for commit.

You can reset to any commit by using its hash: git reset --hard <commit-hash>. You can find commit hashes using git log --oneline to see a compact history.

HEAD~1 refers to the commit before the current HEAD (one commit back), while HEAD~2 refers to two commits back. You can use HEAD~n to go back n commits from the current HEAD.

Yes, you can use git stash to temporarily save your local changes, then reset to the remote branch, and finally use git stash pop to restore your changes:

Terminal
git stash
git reset --hard origin/main
git stash pop

A "detached HEAD" occurs when HEAD points directly to a commit instead of a branch. This can happen when you checkout a specific commit. To avoid this, always checkout branches or create a new branch when checking out a specific commit.

  • Use --soft when you want to undo a commit but keep changes staged
  • Use --mixed (default) when you want to undo a commit and unstage changes
  • Use --hard when you want to completely discard all changes (use with caution)

Built for the world's fastest engineering teams, now available for everyone