git revert commit after pushing

Greg Foster
Greg Foster
Graphite software engineer

Sometimes, after pushing changes, you may need to undo these changes due to errors, or other unintended consequences. This guide will explore how to effectively revert a commit after it has been pushed to a remote repository, covering several methods and best practices.

"Reverting" in Git refers to creating a new commit that undoes the changes made by previous commits. This is different from "deleting" or "removing" a commit from the repository's history which makes it look like the commit never existed in the first place. Reverting retains the context of the commit and why it was removed.

Here’s a step-by-step guide on how to revert a pushed commit without rewriting the project history.

First, you need to identify the commit you want to revert. You can view the commit history by running:

git log --oneline

This command shows a simplified commit log. Find the commit hash of the commit you wish to revert (the unique alphanumeric identifier generated by the SHA-1 hashing algorithm), and copy it.

Once you have the commit hash, you can revert it using:

git revert <commit-hash>

This command creates a new commit that undoes the changes made by the commit specified by <commit-hash>. If the commit in question introduced changes across multiple files, Git might prompt you to confirm the revert process for each conflicted file.

If the revert process introduces conflicts (because the changes in the commit being reverted interact with changes made in subsequent commits), Git will pause the operation and allow you to manually resolve these conflicts. Once you have resolved any conflicts:

  1. Add the resolved files to the staging area using:
    git add <file-name>
  2. Complete the revert process by running:
    git revert --continue

For a detailed guide on resolving merge conflicts, see this guide.

After successfully reverting the commit locally, push the changes to the remote repository:

git push origin <branch-name>

This updates the remote branch with the new commit that effectively undoes the changes made by the previous commit.

If you need to revert more than one commit, you can use the git revert to specify a range of commits:

git revert --no-commit <newest-commit-hash>^..<oldest-commit-hash>
git commit -m "Revert multiple commits"

This command sequence reverts all changes in the specified range, combining them into a single commit.

  1. Communicate with your team: Before reverting commits that have been pushed, especially in shared repositories, inform your team about the changes.
  2. Avoid rewriting public history: Prefer git revert over methods like git reset for public branches to avoid creating synchronization issues for others.

For further reading on reverting commits in Git see the official Git documentation.

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2