Going back in time without losing the rest of your work
Sometimes you realize that one specific Git commit introduced a bug, broke your build, or simply wasn’t needed but you still want to keep all the other commits that came afterwards.
The good news is: Git lets you undo or revert a single commit without throwing away your later work. Here’s how to do it safely.
🛑 Why Not Use git reset
?

It’s tempting to use git reset
when you want to go back to a previous state.
But reset actually rewinds history, discarding everything that came after the target commit (unless you stash or cherry-pick those changes).
If you just want to undo one commit while preserving later commits, git revert
is the safer and recommended option.
✅ Using git revert
for a Single Commit
The easiest way to revert a specific commit is with the commit hash. For example, if the hash is abc1234
:
git revert abc1234
This creates a new commit that cancels the changes from abc1234
, while leaving all the other commits intact.
This is often called a safe rollback.
🎯 Reverting a Commit in a Specific File
If you don’t want to undo the entire commit but only the changes it introduced in a single file, you can do a partial revert:
git restore --source=abc1234^ -- path/to/file
This command restores that file to its state before the commit. Stage and commit it manually to finalize:
git add path/to/file
git commit -m "Revert changes from commit abc1234 in path/to/file"
🔄 Handling Merge Conflicts
Sometimes reverting a commit will cause conflicts if the same parts of code have been modified by later commits. Git will pause and ask you to resolve them. You can check what needs fixing with:
git status
After resolving conflicts, finish the revert by staging and committing:
git add .
git commit
💡 Best Practices for Git Reverts
- Always work on a feature branch when testing reverts before merging to main.
- Communicate with your team reverting changes on a shared branch can surprise others.
- Use clear commit messages like: “Revert commit abc1234: removed accidental debug code.”
- Prefer
git revert
togit reset
when working with remote branches (GitHub, GitLab, Bitbucket).
Conclusion
With git revert
, you can undo one bad commit without disturbing the rest of your history. Whether it’s fixing a bug, rolling back an unwanted change, or cleaning up a file, revert is the safest way to handle mistakes in collaborative projects. Next time you need to undo a commit, remember: revert keeps history intact.
Frequently Asked Questions
- What is the difference between
git reset
andgit revert
? - git reset rewinds history and can remove later commits. git revert adds a new commit that undoes a specific commit without deleting history.
- How can I undo one commit without losing later changes?
- Run
git revert <commit-hash>
. It keeps all subsequent commits and creates a new “revert” commit. - Can I revert a commit that has already been pushed to GitHub?
- Yes. Because it doesn’t rewrite history,
git revert
is safe on shared/public branches. - How do I revert multiple commits?
- You can pass a range like
git revert OLDEST..NEWEST
or several hashes. Git creates one revert commit per target (unless you use--no-commit
and squash). - Can I undo a commit in just one file?
- Yes. Use
git restore --source=<commit>^ -- path/to/file
, thengit add
andgit commit
. This performs a partial revert. - What happens if there are conflicts during a revert?
- Git pauses the process and marks conflicts. Resolve them, then run
git add
followed bygit commit
to complete the revert. - Can I revert a merge commit?
- Yes, specify the parent to keep with
-m
, for examplegit revert -m 1 <merge-hash>
. - Does
git revert
delete history? - No. It preserves history and records a new commit that cancels the targeted changes.
- Is
git revert
better thangit reset
on a shared branch? - Generally yes. Use
git revert
on shared branches because it doesn’t rewrite public history. Reservegit reset
for local, unpublished work. - What command should I use to undo the last commit?
- If it’s the most recent commit and not pushed,
git reset --soft HEAD~1
(or--hard
if you want to discard changes). If you want to keep later commits and just negate one commit (even if pushed), usegit revert <hash>
.