Open In App

Git Reset

Last Updated : 30 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

git reset is a powerful Git command used to move your project’s state backward. It helps undo changes at three key levels:

  • HEAD (which points to the latest commit),
  • index/staging area (where files are prepared for commit), and
  • working directory (your actual local files).

By understanding how git reset interacts with these areas, you can safely reverse commits, unstage files, or discard local changes.

The Working of Git Reset

Depending on how it’s used, git reset allows you to:

  • Unstage files that were accidentally added
  • Discard unwanted changes in the working directory
  • Roll back your current branch to a previous commit

This makes it extremely useful for cleaning up your history during development or fixing staging mistakes.

Before and After Reset (Commit History)

Let’s try to understand through the image below, which shows how commit history changes when you use git reset.

reset

Before Reset

1. You have two branches:

  • master branch (green commits)
  • hotfix branch (pink commit)

2. Both branches have some history (a series of commits).

3. Notice how both branches are pointing to their latest commits.

This refers to the typical state of your working directory., with everything in order.

After Reset

Now imagine you run this command on the master branch:

git reset --hard <some-older-commit>

This moves the master branch pointer back to an earlier commit. It effectively reverts your project history to a previous state.

Here's what happens:

  • The master branch now points to an older commit.
  • The commits that came after that reset point (in gray) are no longer part of the visible history.
    These are called “orphaned commits” they still exist inside Git (for now), but Git won’t show them in your log because they are not connected to any branch.
  • The hotfix branch stays untouched, pointing to the same pink commit.

Git Reset Modes

_modes_of_git_reset

There are three primary modes of git reset:

1. Soft Reset (--soft)

  • Moves the HEAD to an earlier commit.
  • Keeps changes staged in the index.
  • Leaves your working directory unchanged.

Use case: Undo a commit but keep all changes staged.

2. Mixed(Medium) Reset (--mixed) (default)

  • Moves HEAD and updates the index to match the target commit.
  • Unstages changes.
  • Leaves the working directory unchanged.

Use case: You added files with git add but want to unstage them.

3. Hard Reset (--hard)

  • Moves HEAD, index, and the working directory to the specified commit.
  • All changes (staged + unstaged) are lost.

Use with caution: It is irreversible and will delete your changes permanently.

Example

1. Unstage Files Without Losing Changes

To unstage changes you have added with git add, but keep them in your working directory:

git reset

This is a mixed reset and is helpful when you want to modify what’s staged without affecting the actual file content.

2. Revert to a Previous Commit Without Losing Work

To move the HEAD to a previous commit and keep your changes staged:

git reset --soft <commit-hash>

This lets you rewrite history and prepare your changes for a cleaner commit sequence.

3. Completely Discard All Changes

To discard all changes and reset your branch to a specific commit:

git reset --hard <commit-hash>

This will erase everything both staged and unstaged changes and set your branch exactly to the specified commit. Be extremely careful, as this action is irreversible and can result in data loss.


Article Tags :

Explore