Understanding Git Reset and Its Options

The git reset command allows you to undo commits, unstage files, or remove changes from your working directory. It functions by adjusting Git’s three key states: HEAD, Index, and Working Directory. While it is a very powerful tool, it can also be destructive if used carelessly—especially with the --hard option. Below, you’ll learn how it works and when each reset type should be used.

Git Reset Options Overview

Git provides three main reset options. Each one influences the repository’s HEAD, staging area (Index), and working directory differently. The following table summarizes their effects:

Flag HEAD Staging Area Working Directory Summary
–soft Yes No No Keeps changes staged
–mixed Yes Yes No Unstages but preserves file edits
–hard Yes Yes Yes Removes all committed changes

git reset –soft

Command Syntax and Breakdown

git reset --soft <commit>

  • git reset: The base command that alters Git history.
  • –soft: Moves the HEAD pointer but leaves the staging area and working directory unchanged.
  • <commit>: The target commit (e.g., HEAD~1, a commit hash).

What the Flag Does

  • Moves HEAD to the chosen commit.
  • Leaves changes staged in the index.
  • Does not modify the working directory.

Command Demonstration

Output:
abc1234 Second commit
def5678 Initial commit

This removes abc1234 from history but keeps changes staged. Confirm status:

Output:
On branch main
Changes to be committed:
modified: file.txt

$ git commit -m "Amended commit"

This creates a new commit with the same changes, allowing adjustments to the commit message.

git reset –mixed (default)

Command Syntax

git reset --mixed <commit>

  • –mixed: Resets HEAD and staging area to the commit. Leaves working directory unchanged.

Command Demonstration

Output:
abc1234 Second commit
def5678 Initial commit

$ git reset --mixed HEAD~1

This removes the last commit from history, unstages changes, but keeps file edits in the working directory.

Output:
On branch main
Changes not staged for commit:
modified: file.txt


$ git commit -m "Revised commit"

This lets you recommit with changes, useful for splitting or revising work.

git reset –hard

Command Syntax

git reset --hard <commit>

  • Resets HEAD, staging area, and working directory to match the target commit.
  • Use with caution, as it permanently discards changes unless recovered with git reflog.

What the Flag Does

  • Moves HEAD to the chosen commit.
  • Clears the staging area.
  • Reverts working directory files to that commit’s state.

Command Demonstration

Output:
abc1234 Buggy commit
def5678 Stable baseline

This discards the buggy commit and restores files to the stable baseline. Confirm with:

Output:
On branch main
nothing to commit, working tree clean

Reset Changes in Git

You can use git reset to move your repository back to a chosen commit. Below is the syntax and examples:

git reset [--soft|--mixed|--hard] <commit>

  • --soft: Keeps changes staged.
  • --mixed: Unstages changes, keeps edits.
  • --hard: Discards all changes.

Reset to a Specific Commit

Output:
a1b2c3d Fix typo
9f8e7d6 Add login feature
8e7f6a5 Initial commit

Examples:

  • Soft reset: git reset --soft a1b2c3d
  • Mixed reset: git reset --mixed a1b2c3d
  • Hard reset: git reset --hard a1b2c3d

Use git reflog if you need to recover after an accidental reset.

Undo Reset Changes in Git

Command Syntax

git reflog shows HEAD movements including resets.

$ git reset --hard HEAD@{n}

HEAD@{n} refers to how many steps back (e.g., HEAD@{1} is one action ago).

Command Demonstration


Output:
abc1234 HEAD@{0}: reset: moving to HEAD~1
def5678 HEAD@{1}: commit: Add login feature

$ git reset --hard HEAD@{1}

This restores the previous state before the reset. git reflog works with all reset types but is most vital with --hard, since it deletes working directory and index changes.

Conclusion

By mastering git reset, you can undo commits, unstage changes, and discard unwanted edits in your project history. Use the reset type suited for your situation:

  • –soft: Undo commits while keeping changes staged.
  • –mixed: Unstage changes while keeping your working directory intact.
  • –hard: Discard all changes and fully reset your repository. Use cautiously.

Source: vultr.com

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: