Reset in Git

02 Mins
🔁
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

4. Undoing Unstaged and Staged Changes

# Undo unstaged modifications in a file (revert to last committed state)
git checkout -- filename

# Unstage a staged file (remove from index)
git reset filename

# Unstage all staged files
git reset

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.