git revert commit after pushing

Greg Foster
Greg Foster
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


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.

Join 20,000+ developers at top companies
Stop wrestling with Git commands
The Graphite CLI takes all the pain out of Git, allowing you to ship faster and stop googling Git commands.
main
diff1
diff2

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:

Terminal
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:

Terminal
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:
    Terminal
    git add <file-name>
  2. Complete the revert process by running:
    Terminal
    git revert --continue

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

Join 20,000+ developers at top companies
The best engineers use Graphite to simplify Git
Engineers at Vercel, Snowflake & The Browser Company are shipping faster and staying unblocked with Graphite.
main
diff1
diff2

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

Terminal
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:

Terminal
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.

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2