Read Anthropic’s case study about Graphite Reviewer

How to use git reset --hard 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 --hard HEAD command enables developers to revert changes in their working directory and index (staging area) to match a specific commit. This guide will delve into how to use git reset --hard HEAD and its variants, such as git reset --hard HEAD~1 and git reset --hard origin/HEAD.

The command git reset --hard HEAD is typically used when you need to discard all changes in your working directory and staging area, resetting everything to the last commit. The HEAD in this command refers to the current commit you're checked out to, which is usually the latest commit on your current branch.

  • git reset: This command is used for moving the current HEAD to a specified state.
  • --hard: This option tells Git to change all the tracked files in your working directory to match the specified commit. Any changes to tracked files in the working directory and index are discarded.
  • HEAD: A pointer to the current branch's latest commit.

If you need to reset all of the changes currently in your working directory, you can run:

Terminal
git reset --hard HEAD

This resets the state of your working directory and index to match the HEAD commit, discarding any uncommitted changes.

  1. git reset --hard HEAD~1: This moves the current branch back by one commit, effectively undoing the last commit.

    Terminal
    git reset --hard HEAD~1
  2. git reset --hard origin/HEAD: This resets your current branch's HEAD to the last commit fetched from the remote repository, discarding any local changes.

    Terminal
    git reset --hard origin/HEAD
  3. git reset --hard fetch_head: Typically used after a fetch to reset to what was fetched.

    Terminal
    git reset --hard FETCH_HEAD
  1. Undoing recent commits: If you made a mistake in your last commit or the last few commits, you can use git reset --hard to revert to a previous state. This is often used in situations where the commits have not been pushed to a remote repository.

  2. Discarding local changes: If your working directory is cluttered with changes you wish to discard completely, git reset --hard HEAD will clean your project by removing all uncommitted changes.

  3. Reverting to a remote state: To align your local branch with the state of a remote branch, particularly after observing that local changes are either wrong or no longer needed, git reset --hard origin/HEAD or a similar command can be utilized.

  • Data loss: Using git reset --hard can lead to permanent loss of uncommitted changes. Always ensure that you do not need these changes before executing this command.
  • Remote repositories: If you have already pushed commits to a remote repository, resetting locally will not affect the remote. Additional steps, like force pushing, are required to synchronize changes, which can affect other collaborators.

For further reading see this guide on how to use the Git command git reset.

Git inspired
Graphite's CLI and VS Code extension make working with Git effortless.
Learn more

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2