Reset in Git

🔁
Course Under Active Revision Re-visiting

This article belongs to the Git Basics to Advanced course, which is currently being revisited with updated architectural patterns and content. Content may be refined shortly.

1. Undoing Changes in Git

Git provides a powerful reset command that allows you to undo changes at different levels.

To master git reset, understand how it affects the three Git areas:

  1. HEAD: Your active commit pointer.
  2. Staging Area (Index): Proposed next commit snapshot.
  3. Working Directory: The actual files on disk.

2. Interactive Reset Modes Comparison

Toggle through the comparison below to see what happens under --soft, --mixed, and --hard:

Git Reset Modes Comparison

Step 1 of 3: Soft Reset (--soft)
1 / 3
--soft Safest
  • ⏪ HEAD: Rewinds commit
  • ✅ Staging (Index): Kept staged
  • ✅ Working Tree: Untouched
--mixed (Default) Standard
  • ⏪ HEAD: Rewinds commit
  • ⚠️ Staging (Index): Unstaged
  • ✅ Working Tree: Kept on disk
--hard Destructive
  • ⏪ HEAD: Rewinds commit
  • ❌ Staging (Index): Cleared
  • ❌ Working Tree: Wiped permanently
$ git reset --soft HEAD~1

Moves HEAD back 1 commit. Changes remain safely preserved in the Staging Area (Index).


3. The Three Modes of git reset

1. Soft Reset (git reset --soft HEAD~1)

  • Moves HEAD back 1 commit.
  • Keeps changes in the Staging Area.
  • Ideal when you committed prematurely and want to add more files or adjust the commit message.
git reset --soft HEAD~1

2. Mixed Reset (git reset --mixed HEAD~1 - Default)

  • Moves HEAD back 1 commit.
  • Unstages the changes, but keeps them in your working directory as modified files on disk.
  • Ideal when you want to review and re-stage changes incrementally.
git reset --mixed HEAD~1
# (or simply: git reset HEAD~1)

3. Hard Reset (git reset --hard HEAD~1)

  • Moves HEAD back 1 commit.
  • Clears both Staging Area and Working Directory.
  • Destructive: all uncommitted changes are discarded!
git reset --hard HEAD~1

Summary Comparison of Reset Modes

Reset ModeMoves HEAD?Updates Index (Staging)?Alters Working Tree (Disk)?Data Loss Risk?
--softYes (moves back)No (files remain staged)No (files unchanged)Zero risk (fully reversible)
--mixed (default)Yes (moves back)Yes (unstages files)No (files remain on disk)Zero risk (files still present)
--hardYes (moves back)Yes (clears staged files)Yes (overwrites disk files!)High risk (uncommitted edits erased)

Pro Tip: If you accidentally perform git reset --hard and lose uncommitted changes that were previously committed, you can rescue them using git reflog!


4. Undoing Unstaged and Staged Changes

In addition to whole commits, Git provides precise commands to discard edits or unstage files:

Discard Unstaged Changes in Working Directory

Reverts a modified file back to the last committed state:

# Modern syntax (Git 2.23+):
git restore filename

# Classic syntax:
git checkout -- filename

Unstage Files from the Staging Area

Removes a file from the index without altering your code on disk:

# Modern syntax (Git 2.23+):
git restore --staged filename

# Classic syntax:
git reset filename

5. Undoing Commits After Pushing: git revert

If you already pushed a commit to a shared remote (like GitHub main), never use git reset --hard because it rewrites shared history.

Instead, use git revert:

Git Revert: Safe Public Undo

Step 1 of 2: Commit Pushed to Shared Remote
1 / 2
main
master
C1
#c1 Stable release
HEAD → master
C2
#c2 Broken discount logic (Pushed)
$ git log --oneline

Commit C2 has a bug but is already pushed to GitHub. Running reset --hard would rewrite public history.

# Safely creates an inverse commit that undoes c2
git revert c2

git revert leaves past commits untouched and appends a new commit that undoes the changes, keeping history safe for teamwork.