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

How to use the Git command git reset

Greg Foster
Greg Foster
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 in Git's suite, allowing developers to undo changes to their project's history and staging area. This command can be intimidating at first as it is one of the few commands that is destructive and can cause you to lose work.

However, understanding how to use git reset effectively can get you out of tricky situations and undo other mistakes. This guide will explain the various git reset modes (hard, soft, and mixed), what they do, and when to use them.

  • git reset: At its core, git reset changes the current HEAD to the specified state. It can modify the index (staging area), the working directory, and which commit the tip of the current branch is pointing to, depending on the mode used.
  1. Hard reset (-hard): This mode resets the index and working directory.
    • Usage:

      git reset --hard <commit>

    Before resetting to a specific commit it’s helpful to look at the git log to see the current state of your Git history. Here you can see where you are in the timeline, grab specific commit hashes, and more.

    • Git hard reset examples:

      • Reset to a specific commit: git reset --hard <commit-hash>

      • Reset to the previous commit: git reset --hard HEAD^

      • Reset to the 3rd commit before the current HEAD: git reset --hard HEAD~3

      • Reset the current branch to match another branch: git reset --hard origin/master

  1. Soft reset (-soft): This mode only resets the current branch's tip to the specified commit but does not touch the index or working directory. It's as if you undid your last commit(s) but left your files staged.

    • Usage:

      git reset --soft <commit-hash>

  2. Mixed reset (default): This is the default mode when no option is specified. It resets the index but not the working directory. It un-stages changes but does not discard them. The changes will still be present in local memory.

    • Usage:

      git reset <commit-hash>

  • git reset HEAD: This command is often used to unstage files that have been added to the staging area by mistake.
  • Reverting changes: If you want to discard all changes in the working directory since the last commit, use:

    git reset --hard

  • Undoing a git commit: To undo the last commit and keep the changes available in the local working directory run:

    git reset --soft HEAD^

  • Cleaning the working directory: To remove all changes in the working directory:

Terminal
git reset --hard
git clean -fdx

By using git clean you will remove all untracked files from the working tree.

  • Reset to a remote branch: If you need to reset your local branch to exactly match a remote branch:

    git fetch origin git reset --hard origin/<branch-name>

  • Resetting a file to HEAD: To reset a specific file to its state at HEAD:

    git checkout HEAD -- <file-path>

Conclusion

The git reset command is versatile, offering different modes for various scenarios. Whether you need to discard changes, undo commits, or clean your working directory, understanding how to use git reset effectively is crucial for managing your Git repository. Always proceed with caution when using git reset --hard, as it can permanently delete your work.

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