Reset in Git
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.
Table of Contents
8 sections
- 1. Undoing Changes in Git
- 2. Interactive Reset Modes Comparison
- 3. The Three Modes of git reset
- 1. Soft Reset (git reset --soft HEAD~1)
- 2. Mixed Reset (git reset --mixed HEAD~1 - Default)
- 3. Hard Reset (git reset --hard HEAD~1)
- 4. Undoing Unstaged and Staged Changes
- 5. Undoing Commits After Pushing: git revert
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:
- HEAD: Your active commit pointer.
- Staging Area (Index): Proposed next commit snapshot.
- 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)- ⏪ HEAD: Rewinds commit
- ✅ Staging (Index): Kept staged
- ✅ Working Tree: Untouched
- ⏪ HEAD: Rewinds commit
- ⚠️ Staging (Index): Unstaged
- ✅ Working Tree: Kept on disk
- ⏪ 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
$ 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.