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.
git reset rewinds history and can remove later commits. git revert creates a new commit that undoes the changes of a specific commit without deleting history.
Use git revert <commit-hash>. This cancels the effects of that commit while keeping all subsequent commits.
Yes. Since git revert doesn’t rewrite history, it’s safe to use even on public or shared branches.
You can specify a range like git revert OLDEST..NEWEST or pass multiple commit hashes. Each revert creates a new commit.
Yes. Use git restore –source=<commit>^ — path/to/file, then stage and commit the result.
Git will stop and mark conflicts. Resolve them manually, then use git add and git commit to complete the revert.
Yes, but you must specify which parent branch to keep using the -m flag. Example: git revert -m 1 <merge-hash>.
No. Unlike reset, revert keeps the commit history intact and simply adds a new commit that cancels changes.
Yes. Revert is much safer on shared branches because it doesn’t rewrite history. Reset should only be used locally.
If it’s the most recent commit and not pushed yet, git reset HEAD~1 works. But if you want to keep later commits, use git revert <hash>.