Lesson

Git Revert

10 min reading
Free Course

Git Revert: Safely Undoing Pushed Commits

git revert creates a brand-new commit that applies the exact opposite changes of an earlier commit. Unlike git reset, it never deletes or rewrites existing commit history, making it the safest way to roll back bugs already pushed to main or shared team branches.

Reverting a Standard Commit

First find the hash of the broken commit with git log --oneline, then pass that hash to git revert:

git log --oneline -n 8
git revert 4a9c12e

Git applies the inverse diff, opens your editor with a pre-filled "Revert ..." message, and saves a new commit on top of your branch. If you want to skip opening the editor and keep the default message, pass --no-edit:

git revert --no-edit 4a9c12e

Reverting Multiple Commits or Merge Commits

To undo several commits together as a single cleanup commit, pass --no-commit (-n) so Git stages the inverse changes without committing after each hash:

git revert --no-commit 4a9c12e 8f3b21a
git commit -m "Revert broken header padding and banner alignment"

When reverting a merge commit, Git needs to know which parent branch was the mainline (main). Pass -m 1 to keep parent 1 (main) and undo the merged feature branch:

git revert -m 1 c92a410

Common Revert Mistakes

If you revert a feature branch from main and later try to merge that exact same branch again, Git sees those old commits already in history and skips them. To re-introduce the feature later, revert the revert commit itself first (git revert <revert-sha>).

Frequently Asked Questions

When should I use git revert instead of git reset?

Always use git revert for commits already pushed to main or any shared branch. Reserve git reset for local unpushed commits on private branches.

Can git revert cause a merge conflict?

Yes, if later commits modified the same lines as the commit you are reverting. Resolve the conflict markers normally, stage the file, and run git revert --continue.

How do I cancel a revert that hit conflicts?

Run git revert --abort to return your working tree and branch to their exact state before the revert started.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol
Git Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum