This guide covers how to undo or reverse commits in Git depending on your workflow state (local vs. remote) and what you want to do with your code changes.
The Core Choice: Reset vs. Revert
Before running any command, you must answer one crucial question:
Have you already pushed your commit to a remote repository ?
graph TD A[Want to undo a commit?] --> B{Has it been pushed?} B -- No (Local Only) --> C[Use git reset] B -- Yes (Shared/Remote) --> D[Use git revert] C --> C1[--soft: Keep changes staged] C --> C2[--mixed: Keep changes unstaged] C --> C3[--hard: Nuke changes completely] D --> D1[Creates a new safe commit that undoes the work]
1. Local Commits Only (Not Yet Pushed) — git reset
If your changes only exist on your local computer, you can safely rewrite your commit history using git reset.
Danger Zone
git resetalters commit history. Never use it on commits that others have pulled or built upon, as it will break their local repositories.
Mode A: Keep Changes Staged (--soft)
Use this when you want to undo the commit but immediately re-commit the files (e.g., to fix a typo in the commit message or add a missing file).
- Command:
git reset --soft HEAD~1 - History: The last commit vanishes.
- Your Code: Your files remain modified and are sitting in the Staging Area (ready to be committed again).
Mode B: Keep Changes Unstaged (--mixed / Default)
Use this when you want to undo the commit and step back to rethink your work. Your code is preserved, but you have to choose which files to stage again.
- Command:
git reset HEAD~1(orgit reset --mixed HEAD~1) - History: The last commit vanishes.
- Your Code: Your files remain modified but are moved back to your Working Directory (unstaged).
Mode C: Destroy Everything Permanently (--hard)
Use this when you realize the entire commit was a terrible mistake and you want to completely erase it from existence.
- Command:
git reset --hard HEAD~1 - History: The last commit vanishes.
- Your Code: Permanently deleted. Any uncommitted changes in your working directory are also wiped out.
2. Pushed Commits (Public / Shared History) — git revert
If your commit is already on a remote server, altering history with git reset will cause errors for teammates. Instead, you must use git revert.
- Command:
git revert HEAD - History: Your bad commit remains in the history. Git creates a brand new commit that introduces the exact opposite changes, cleanly neutralizing the mistake.
- Your Code: Your working directory updates to reflect the rolled-back state.
Reverting Older Commits
If you want to undo a commit further back in time, use
git logto find its unique alphanumeric SHA hash and pass it directly:git revert <commit-hash>.
Summary
| Command | Target Environment | Modifies History? | What happens to your code? |
|---|---|---|---|
git reset --soft HEAD~1 | Local Only | Yes (Deletes commit) | Kept in Staging Area (Ready to commit) |
git reset HEAD~1 | Local Only | Yes (Deletes commit) | Kept in Working Directory (Unstaged) |
git reset --hard HEAD~1 | Local Only | Yes (Deletes commit) | Permanently Deleted 🛑 |
git revert HEAD | Pushed / Remote | No (Appends new) | Safely rolls back to previous state |
Eg
You can test these scenarios safely by running this step-by-step simulation inside a blank folder on your computer.
Setup a Test Repository
Open your terminal (Mac/Linux) or Git Bash (Windows) and run:
# 1. Create a fresh sandbox folder and enter it
mkdir git-sandbox && cd git-sandbox
# 2. Initialize it as a Git repository
git init -b main
# 3. Create a baseline file and make a first commit
echo "Line 1: Good Code" > app.txt
git add app.txt
git commit -m "Initial baseline commit"Scenario 1: Test a Local --soft Reset
Let’s make a mistake and undo it while keeping our files ready to re-commit.
# 1. Make a bad modification and commit it
echo "Line 2: Oops, a typo here!" >> app.txt
git add app.txt
git commit -m "Add line 2 with a typo"
# 2. Run the soft reset
git reset --soft HEAD~1
# 3. Verify status (You will see the file is still staged!)
git status
# Output: "Changes to be committed: modified: app.txt"Scenario 2: Test a Public git revert
Let’s simulate undoing a commit in a way that is perfectly safe for a pushed, remote repository.
# 1. Commit the change normally again
git commit -m "Add line 2 with typo"
# 2. Revert the commit safely
git revert HEAD --no-edit
# 3. Check your file content—the bad line is gone, but the history is safe!
cat app.txt
# Output: Line 1: Good Code
# 4. View your log to see the new revert commit appended cleanly
git log --oneline