How to use git reset --hard 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 --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 reset --hard HEAD: Discards all changes in both the working directory and staging area, making your working directory exactly match the HEAD commit.
  • git reset --soft HEAD: Moves the HEAD pointer but keeps your changes staged in the index. Your working directory remains unchanged, and all changes are preserved in the staging area.

Unfortunately, git reset --hard permanently deletes uncommitted changes. However, you might be able to recover some data using:

  • Git reflog: git reflog shows a history of all HEAD movements, and you might find your lost changes there
  • File system recovery tools: If the changes were recently deleted, file recovery software might help
  • IDE/editor history: Some editors keep local history of file changes
  • git reset --hard HEAD~1: Use when you want to completely remove the last commit from history (before pushing to remote). This rewrites history.
  • git revert: Use when you want to undo a commit that's already been pushed to a shared repository. This creates a new commit that undoes the changes, preserving history.

Yes, they are functionally equivalent. When you don't specify a commit reference, Git defaults to HEAD. So git reset --hard and git reset --hard HEAD do exactly the same thing.

git reset --hard HEAD only affects tracked files. Untracked files (files that have never been added to Git) will remain untouched in your working directory. To remove untracked files, you would need to use git clean -fd.

No, git reset --hard operates on the entire repository, not individual files. If you want to reset only a specific file, use:

Terminal
git checkout HEAD -- <filename>

or

Terminal
git restore <filename>
  • git reset --hard HEAD: Resets to the current local HEAD commit
  • git reset --hard origin/HEAD: Resets to the remote branch's HEAD commit (requires a recent git fetch to be accurate)
  • Use git reset --hard: When you want to permanently discard changes and don't need them later
  • Use git stash: When you want to temporarily save changes to apply them later or on a different branch

Before running git reset --hard, you can:

  1. Create a backup branch: git branch backup-branch
  2. Use git stash to save changes: git stash push -m "backup before reset"
  3. Test the reset, and if needed, restore with git stash pop or git checkout backup-branch
Git inspired
Graphite's CLI and VS Code extension make working with Git effortless.
Learn more

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