Articles

What is Git Reset? Explained with Examples

When we work with Git, mistakes are inevitable — we might commit too early, forget to include a file, or simply want to undo a change. Fortunately, Git gives us several tools to fix such errors, and one of the most powerful among them is git reset.

Understanding how to use git reset correctly helps us manage our repository with confidence, clean up our commit history, and maintain a professional workflow. In this tutorial, we’ll explain what git reset is, how it works, when to use it, and more.

Let’s start the discussion with a brief overview of git reset.

  • Learn how to track changes in your code and switch between different versions with Git, an open-source version control system.
    • Beginner Friendly.
      1 hour
  • Become proficient in Git for DevOps, including repository management, Git commands, GUI, distributed workflows, branching, Git server protocols, and Gitflow.
    • Intermediate.
      1 hour

What is Git reset?

The git reset command allows us to move the HEAD of the current branch to a specific commit, effectively resetting our project’s state. It can modify not just the commit history but also the staging area and working directory, depending on the options we choose.

In simpler terms, git reset lets us undo commits or changes locally. Whether we want to unstage files, rework a commit, or discard everything and start fresh, git reset gives us complete control over our repository’s state.

However, it’s important to remember that git reset can rewrite history, which makes it risky to use on shared branches. When used carefully, it becomes a powerful tool for maintaining a clean, accurate project history.

Now that we’ve got an idea of what git reset is, let’s look at its different types and how each of them behaves.

Types of Git reset

Git offers three reset options that differ in how they handle our changes:

  • git reset --soft — Keeps changes staged (safest)
  • git reset --mixed — Keeps changes unstaged (default)
  • git reset --hard — Discards all changes (dangerous)

The main difference between them lies in what happens to our changes after the reset. Let’s compare them in detail, starting with soft reset.

1. git reset –soft

The soft reset (git reset --soft) moves the HEAD to a specific commit but keeps everything else intact. This means both the staging area and the working directory remain unchanged. The files that had been staged before the reset stay staged, and no actual file content is altered.

This mode is ideal when we realize that a recent commit needs to be modified — for example, we forgot to include a file or want to change the commit message. Because it preserves our staged changes, we can easily fix the issue and create a new commit without redoing our work.

In short, a soft reset only affects the commit history, leaving our changes safe and ready for adjustment. It’s the least destructive of all the reset modes and perfect for minor local corrections.

2. git reset –mixed

The mixed reset (git reset --mixed) is the default behavior of git reset. If we run git reset without specifying a flag, Git performs a mixed reset automatically.

This mode updates the HEAD and the staging area to match the target commit, but it leaves our working directory unchanged. That means our actual files remain on disk, but any staged changes are removed from the index — effectively unstaging them.

A mixed reset is especially useful when we’ve staged files that we no longer want to include in our next commit. It gives us the freedom to review, modify, or restage those files as needed before committing again. This approach keeps our working directory intact while allowing us to reorganize our staged content.

Among the three modes, the mixed reset strikes a balance between flexibility and safety. It doesn’t delete our work but gives us a chance to tidy up our staging area.

3. git reset –hard

The hard reset (git reset --hard) is the most powerful — and potentially dangerous — type of git reset. It moves HEAD to the specified commit and resets both the working directory and staging area to match it. In doing so, it permanently discards any uncommitted changes, whether staged or unstaged.

When we perform a hard reset, Git essentially rewinds our project to a clean snapshot of the chosen commit. It erases everything that came after it locally, leaving no trace of uncommitted edits. This can be very useful when we want to completely abandon experimental changes or restore our project to a known stable state.

However, this mode should be used with caution. Because it permanently removes uncommitted changes, recovery can be difficult — or even impossible — without backups or Git’s reflog. Before using a hard reset, it’s always a good idea to ensure that no important work will be lost.

With the types covered, let’s explore some practical examples to see how each mode works in real-world situations.

How to use Git reset

The basic syntax for git reset looks like this:

git reset [mode] [commit] 

In the syntax:

  • [mode]: Represents one of the reset options (--soft, --mixed, --hard).
  • [commit]: The target commit to which we want to reset our branch (it could be a commit hash, branch name, or relative reference like HEAD~1).

Let’s now go through some examples that demonstrate how to perform each type of git reset.

Using git reset –soft

Let’s say we made a commit too early — for example, we committed a file without updating the version number. We don’t want to lose any changes, but we do want to rewrite the last commit to include the missing update.

In this case, we can perform a soft reset:

git reset --soft HEAD~1

After running this command, Git moves the HEAD pointer one commit back. The last commit is undone, but all the changes from that commit remain staged and ready to be recommitted.

We can now make the missing update, stage any additional files if needed, and then create a corrected commit using:

git commit -m "Update version number and related files"

Using git reset –mixed

Imagine we just committed several files, but we realize some of them don’t belong in that commit. We want to undo the last commit and unstage those files — without losing any changes in our working directory.

In this case, we can perform a mixed reset:

git reset --mixed HEAD~1

Or equivalently:

git reset HEAD~1

As a result, Git removes the most recent commit as well as resets the staging area to match the previous commit. All files that were part of the undone commit are now unstaged, but the actual changes remain safely in the working directory.

When we check the status with:

git status

We’ll see that our files are still modified — just not staged. From here, we can selectively add the correct files and recommit them properly:

git add <filename>
git commit -m "Add correct files"

Using git reset –hard

Suppose we’ve been experimenting with a new feature that didn’t go as planned. We made several changes, but none of them are worth keeping, and we want to return the project to its last stable commit.

In this case, we can perform a hard reset:

git reset --hard HEAD~1

This command tells Git to move the HEAD pointer one commit back and reset both the working directory and staging area to that earlier state.

All uncommitted changes — whether staged or unstaged — are permanently deleted.

When we run:

git status

The repository will show no pending changes. It’s as if the last commit and all local edits never existed.

Since we’re done learning how to perform each type of git reset, let’s discuss when it’s appropriate to use it.

When to use Git reset

Here are some ideal scenarios for using git reset:

  • Fixing a local mistake: Undo a recent commit that hasn’t been pushed yet.
  • Cleaning up commits: Combine or rewrite messy commits before sharing the branch.
  • Unstaging changes: Move files out of the staging area quickly.
  • Discarding unwanted work: Use git reset --hard to remove local experiments or drafts.

Next, we’ll compare git reset with a similar command, git revert.

Git reset vs Git revert

Both git reset and git revert help us undo changes, but they do so in different ways and serve different purposes:

Feature Git reset Git revert
Effect Moves HEAD and optionally changes files Creates a new commit that undoes a previous one
History Rewrites commit history Preserves history
Use case Local fixes and cleanup Undoing commits on public/shared branches
Safety Risky for shared repositories Safe for team collaboration
Changes Can unstage or discard work Keeps all changes recorded in new commit

In short, we should use git reset for local adjustments and git revert for shared repositories where preserving history matters.

Finally, let’s go through some of the best practices for using git reset efficiently.

Best practices for using Git reset

Apply these best practices to make the most out of git reset:

  • Choose the correct mode: Choose between --soft, --mixed, or --hard based on whether you want to keep the changes in the staging area or working directory.
  • Avoid resetting shared commits: Never use git reset on commits that are already pushed to a shared repository.
  • Use git status frequently: Check the state of your working directory and staging area before and after a reset.
  • Consider stashing changes: If you’re unsure, use git stash to save work before performing a reset.

Following these best practices will ensure effective usage of git reset.

Conclusion

In this guide, we explored what git reset is, its different types, and how to use each mode effectively with practical examples. We also discussed when to apply git reset, compared it with git revert, and shared best practices that we can follow to ensure safe and efficient use of this command.

git reset is a powerful tool for managing commits and project history. By understanding its types and how to use them, developers can confidently undo changes, refine commits, and maintain a clean, organized workflow. If used carefully, git reset gives us full control over our Git history and helps us recover from mistakes efficiently.

If you want to expand your knowledge of Git, check out the Learn Git & GitHub course on Codecademy.

Frequently asked questions

1. What does Git reset do?

git reset moves the current branch’s HEAD pointer to a specific commit and optionally changes the staging area and working directory to match that commit.

2. What is Git revert vs Git reset?

git reset rewrites history and can remove commits, while git revert produces a new commit that reverses the effects of a previous one, preserving history.

3. Is git reset –hard safe?

No, it’s risky. git reset --hard permanently deletes uncommitted changes from the staging area and working directory.

4. Will Git reset delete files?

Yes. In --hard mode, git reset can delete or overwrite files. Softer modes like --soft and --mixed will not remove files.

5. What is a soft reset?

A soft reset (git reset --soft) moves HEAD to a previous commit but keeps all changes staged, allowing us to easily recommit with modifications.

Codecademy Team

'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'

Meet the full team

Learn more on Codecademy