How to Reset to a Commit in Git

How to Reset to a Specific Commit in Git, and a Guide to the Git reset Command

Published by Carlo van Wyk on June 17, 2025 in Git

How to Reset to a Commit in Git
How to Reset to a Commit in Git

Understanding how to reset to a commit in Git allows you to undo changes and return your changes to a previous state. Let me show you how to use the different git reset commands. Before diving into the commands, let's understand the key concepts that make git reset work.

Understanding Git's Key Components

HEAD is Git's pointer to your current branch's latest commit. Think of it as a bookmark showing where you are in your project's history. When you make a new commit, HEAD automatically moves to point to that new commit.

Working Directory is your project folder containing the actual files you can see and edit. This is where you make changes to your code before committing them.

Staging Area (also called the index) is Git's preparation area where you place changes before committing them. When you run git add, files move from your working directory to the staging area.

The three reset modes works like this:

  • Soft Reset only moves HEAD to a previous commit, leaving your staging area and working directory untouched
  • Mixed Reset moves HEAD and clears the staging area, but keeps your working directory files as they are
  • Hard Reset moves HEAD, clears the staging area, and overwrites your working directory files to match the target commit

Git Reset Commands Explained

Git provides three main reset options: --soft, --mixed, and --hard. Each option allows you to reset your repository in different ways.

Soft Reset

A soft reset moves your HEAD pointer to a previous commit while keeping your staged changes and working directory intact. This is useful when you want to recommit your changes differently.

git reset --soft HEAD~1

Mixed Reset

The mixed reset (default option) moves HEAD and unstages changes, but preserves modifications in your working directory. Use this when you want to restructure your commits.

git reset --mixed HEAD~1
# or simply
git reset HEAD~1

Hard Reset

A hard reset completely discards all changes and reverts to the specified commit. This is the most destructive option and should be used carefully.

git reset --hard HEAD~1

Resetting to a Specific Commit

To reset to a specific commit, use its hash instead of HEAD~1:

git reset --soft abc123
git reset --mixed abc123
git reset --hard abc123

Conclusion

Understanding the git reset command gives you powerful control over your project's history. Use --soft when you want to recommit changes with better commit messages or combine multiple commits. Use --mixed when you need to unstage files and reorganize your changes before committing. Use --hard for situations where you want to discard all changes and return to a clean state. Just keep in mind that --hard is irreversible for uncommitted changes.

Now that you know how to reset to a commit in git, you can manage your repository's history and recover from mistakes.