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
11 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)
- Summary Comparison of Reset Modes
- 4. Undoing Unstaged and Staged Changes
- Discard Unstaged Changes in Working Directory
- Unstage Files from the Staging Area
- 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
Summary Comparison of Reset Modes
| Reset Mode | Moves HEAD? | Updates Index (Staging)? | Alters Working Tree (Disk)? | Data Loss Risk? |
|---|---|---|---|---|
--soft | Yes (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) |
--hard | Yes (moves back) | Yes (clears staged files) | Yes (overwrites disk files!) | High risk (uncommitted edits erased) |
Pro Tip: If you accidentally perform
git reset --hardand lose uncommitted changes that were previously committed, you can rescue them usinggit 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
$ 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.