Working with Git is empowering—but mistakes happen. Maybe you committed the wrong file, pushed a half-finished feature, or just realized something needs to be undone. The good news? Git makes it possible to roll back commits safely and efficiently.
In this blog post, we’ll walk through different ways to undo or rollback a commit in Git, depending on your situation—whether it’s local or already pushed to GitHub.
🧠 Understand the Context First
Before choosing how to rollback a commit, ask yourself:
- ❓ Is the commit local or pushed to GitHub?
- ❓ Do you want to remove the commit completely or just undo the changes?
- ❓ Are other people working on the branch (i.e., shared work)?
Your answers will guide you to the right rollback strategy.
🔙 Common Ways to Rollback a Commit in Git
1. Undo the Last Commit (Local Only)
If you just made a mistake and haven’t pushed it yet:
git reset --soft HEAD~1
This removes the last commit but keeps your changes staged (ready to recommit).
Or:
git reset --mixed HEAD~1
This removes the commit and unstages the changes (but keeps them in your working directory).
Or the nuclear option:
git reset --hard HEAD~1
This removes the commit and discards the changes completely. ⚠️ Use with caution!
2. Undo a Commit That Has Been Pushed to GitHub
If you’ve already pushed your commit to GitHub, use these with caution (especially on shared branches):
Option A: Revert the Commit (Safe for Shared Branches)
git revert <commit-hash>
This creates a new commit that undoes the changes introduced by the bad commit—without modifying history.
Steps:
- Find the commit hash using
git log
- Run:
git revert a1b2c3d
- Push the new “revert” commit:
git push origin branch-name
✅ Safe and recommended when working in teams.
Option B: Reset to a Previous Commit (Risky on Shared Branches)
If you’re okay rewriting history (e.g., on a personal branch):
git reset --hard <commit-hash>
git push --force
This will erase all commits after the specified commit. Use only if you’re sure it’s safe to overwrite the history.
🧪 Bonus: Undo a Specific Commit in the Middle of History
You can use git revert
to target any commit—even if it’s not the most recent one.
git log
# Find the hash of the commit you want to undo
git revert a1b2c3d
If it’s a merge commit, use:
git revert -m 1 <merge-commit-hash>
⚠️ Best Practices When Rolling Back Commits
- Never use
git reset --hard
on shared branches. - Use
git revert
for safety—it preserves history. - Always communicate with your team if you’re rewriting history.
- Create a backup branch before risky operations:
git branch backup-before-reset
📝 Summary Table
Goal | Command | Safe for Shared Branches? |
---|---|---|
Undo last commit (keep changes) | git reset --soft HEAD~1 | Yes (before push) |
Undo last commit (discard changes) | git reset --hard HEAD~1 | No |
Undo pushed commit safely | git revert <commit-hash> | ✅ Yes |
Erase pushed commit (rewrite history) | git reset --hard <hash> && git push --force | ❌ No (unless solo) |
🚀 Final Thoughts
Rolling back a Git commit doesn’t have to be scary. Whether you’re working on your own or as part of a team, Git gives you powerful tools to undo mistakes without losing your progress. Just remember: choose the right command for your context, and always think before rewriting history.
Pro tip: If you’re ever unsure, create a backup branch and test your changes there first.