How to Revert a Commit in Git Without Issues

Summarize this article with:
You pushed a commit. Something broke. Now what?
Knowing how to revert a commit in Git saves you from panic-deleting files or explaining to your team why production just crashed.
The git revert command creates a new commit that undoes changes from any previous commit while preserving your entire commit history.
No destroyed timelines. No angry teammates.
This guide walks through finding commit hashes, reverting single and multiple commits, handling merge commits, and resolving conflicts that pop up along the way.
Whether you’re fixing a bug in a shared branch or rolling back a feature gone wrong, you’ll have the exact commands ready in under five minutes.
How to Revert a Commit in Git

Reverting a commit in Git is the process of creating a new commit that undoes changes from a previous commit while keeping your entire commit history intact.
Developers need this when a bug slips into production, when unwanted changes get pushed to a shared branch, or when rolling back features without destroying the project timeline.
This guide covers 5 methods for using the Git revert command, requiring 2-5 minutes and basic version control knowledge.
Prerequisites
Before running the git revert command, confirm you have:
- Git 2.0 or later installed
- Terminal or command line access
- An existing Git repository with commits
- Clean working directory (run
git statusto check) - Basic familiarity with Git commands
Time estimate: 2-5 minutes per revert.
Step 1: How Do You Find the Commit Hash to Revert?
Run git log --oneline in your terminal to display all commits with their SHA-1 hash identifiers; copy the 7-character hash of the target commit.
Action
- Command:
git log --oneline - Location: Terminal inside repository root
- Result: List of commits showing hash + message
Purpose
The git log output pinpoints the exact commit to undo, preventing accidental reverts on the wrong changes in your codebase.
Step 2: How Do You Revert the Most Recent Commit?
Execute git revert HEAD to generate a new commit that reverses all changes from the latest commit on your current branch.
Git opens your default text editor for the commit message, then applies the inverse changes automatically.
Action
- Command:
git revert HEAD - Editor: Accept or modify the default revert message
- Result: New commit with opposite changes created
Purpose
HEAD references the most recent commit without requiring a hash lookup.
The git revert command preserves repository history while safely undoing changes, which matters when working with remote repositories and team collaboration.
Step 3: How Do You Revert a Specific Older Commit?
Run git revert to undo changes from any commit in your history without affecting commits that came after it.
Action
- Command:
git revert a1b2c3d - Location: Terminal in repository directory
- Result: New commit reversing only the specified commit’s changes
Purpose
Targeting a specific SHA-1 hash lets you surgically remove one bad commit while preserving all subsequent work in your source control history.
Step 4: How Do You Revert Multiple Commits at Once?
Use range syntax git revert or list individual hashes to undo several commits; add --no-commit to combine all reversals into one commit.
Action
- Range command:
git revert abc123^..def456 - Individual command:
git revert --no-commit abc123 def456 ghi789 - Finalize:
git commit -m "Revert multiple commits"
Purpose
Reverting newest-to-oldest prevents conflicts; the --no-commit flag keeps your branch history cleaner with a single revert commit.
Step 5: How Do You Revert Without Creating an Immediate Commit?
Add the --no-commit flag to stage inverse changes in your staging area without auto-committing, allowing further edits before finalizing.
Action
- Command:
git revert --no-commit - Review: Check changes with
git diff --staged - Commit manually:
git commit -m "Your message"
Purpose
Useful when combining multiple reverts or making additional modifications before committing to the repository.
Step 6: How Do You Resolve Conflicts During a Git Revert?
When subsequent commits modified the same lines, Git pauses the revert and marks conflicts in affected files for manual resolution.
Action
- Open conflicted files: Look for
<<<<<<<markers - Resolve and stage:
git add - Continue:
git revert --continue - Or abort:
git revert --abort
Purpose
Conflict resolution ensures the reverted state makes sense with all changes that happened after the original commit; check our guide on how to resolve merge conflicts in Git for detailed steps.
Step 7: How Do You Revert a Merge Commit?
Merge commits have two parents, so you must specify which parent line to keep using the -m flag with parent number 1 or 2.
Action
- Command:
git revert -m 1 - Parent 1: The branch you merged into (mainline)
- Parent 2: The branch that was merged
Purpose
Without -m, Git cannot determine which side of the merge represents the baseline for reverting changes.
Step 8: How Do You Push the Reverted Commit to a Remote Repository?
After the local revert completes, run git push to sync the new revert commit with your remote.
Action
- Command:
git push origin - Example:
git push origin main - Result: Remote updated with revert commit
Purpose
Git revert creates forward-moving history, making it safe for shared branches on GitHub, GitLab, or Bitbucket without forcing teammates to reset.
Verification
Confirm the revert worked correctly:
git log --oneline– verify new revert commit existsgit diff– compare changesHEAD - git status – ensure clean working directory
- Check file contents match expected state
Troubleshooting
Issue: “error: could not revert” message
Solution: Run git status to check for uncommitted changes; use git stash to temporarily store them before reverting.
Issue: Revert creates empty commit
Solution: The changes were already undone in a later commit; verify with git log -p to trace the history.
Issue: “Reverting is not possible because you have unmerged files”
Solution: Resolve existing conflicts first with git mergetool or manually, then run git revert --continue.
Alternative Methods
git reset (Local Commits Only)
- Time: 1 command
- Best for: Unpushed commits where history rewrite is acceptable
- Risk: Destroys commit history; problematic for shared branches
Learn more about what git reset does before using it on collaborative projects.
git revert (Current Guide)
- Time: 1-2 commands
- Best for: Pushed commits, shared branches, preserving history
- Risk: None; creates safe forward-moving history
Choose git reset when commits exist only locally.
Choose git revert when commits are already pushed or when working in teams with continuous integration pipelines.
Related Processes
- How to unstage a file in Git
- What is git checkout
- What does git stash do
- How to switch branches in Git
- How to create a new branch in Git
FAQ on How To Revert A Commit In Git
What is the difference between git revert and git reset?
Git revert creates a new commit that undoes changes while preserving history.
Git reset moves the branch pointer backward, erasing commits from the timeline.
Use revert for shared branches; reset only for local, unpushed work.
Can I revert a commit that has already been pushed?
Yes. Git revert is designed for pushed commits because it adds forward-moving history rather than rewriting it.
Run git revert locally, then push to your remote normally.
How do I find the commit hash I need to revert?
Run git log --oneline to display commits with their SHA-1 identifiers.
Copy the 7-character hash next to the commit message you want to undo.
Use git diff to compare changes if unsure.
What happens if I revert a merge commit?
Merge commits have two parents, requiring the -m flag to specify which parent line to preserve.
Use git revert -m 1 where 1 represents your mainline branch.
Can I undo a git revert?
Yes. Run git revert to reverse the revert, effectively restoring the original changes.
This creates another commit in your history, keeping everything traceable.
Why does git revert create a new commit instead of deleting the old one?
Git preserves complete history for configuration management and team collaboration.
Deleting commits from shared branches causes sync issues for teammates who already pulled those changes.
How do I revert multiple commits at once?
Use range syntax: git revert .
Add --no-commit to stage all reversals without committing, then create one combined commit manually for cleaner history.
What should I do if git revert causes conflicts?
Open conflicted files and resolve the <<<<<<< markers manually.
Stage resolved files with git add, then run git revert --continue.
Use git revert --abort to cancel entirely.
Is git revert safe for production branches?
Yes. Git revert is the safest method for undoing changes on production or shared branches because it never rewrites history.
Teams using continuous deployment pipelines rely on this approach.
Can I revert a commit without opening the editor?
Add the --no-edit flag to accept the default commit message automatically.
Run git revert --no-edit for scripted operations or build pipeline automation.
Conclusion
Mastering how to revert a commit in Git keeps your repository history clean and your team workflows intact.
You now have the commands to undo single commits, handle merge reverts with the -m flag, and resolve conflicts when they appear.
The key difference matters: use git revert for pushed changes on shared branches, git reset only for local work.
Both protect your codebase differently.
Practice these commands in a test repository before applying them to production code.
Build the muscle memory now so you can execute a clean rollback under pressure.
Version control mistakes happen to everyone. Knowing how to reverse them quickly separates confident developers from those scrambling through Stack Overflow at midnight.
Keep git log –oneline` close. You’ll need it.
- What Happens When You Offload an App on iPhone - May 9, 2026
- How to Use Digital Wellbeing on Android - May 8, 2026
- Why Buyers Trust a Well-Built Data Room - May 7, 2026







