Read Anthropic’s case study about Graphite Reviewer

How to revert merged pull requests via command line tools

Sara Verdi
Sara Verdi
Graphite software engineer
Try Graphite


Note

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


Reverting a merged pull request is a crucial skill for developers looking to undo changes that have been integrated into the main branch but later identified as problematic. This guide will cover the steps to revert pull requests using the command line for both Git and Bitbucket, including the use of the Graphite CLI to streamline these processes.

Before reverting a merge, you need to identify the commit SHA (a unique identifier for that commit) of the merge commit. You can find this using the git log command. Here's how you view the commit history:

Terminal
git log --oneline --graph

This will display a simplified commit history. Look for a commit labeled as "Merge pull request #xx from some-branch."

To revert the merge commit, use the git revert command followed by the commit SHA. Here’s an example:

Terminal
git revert -m 1 <commit-sha>

The -m 1 option tells Git to keep the parent side of the mainline, which is necessary for reverting a merge commit.

If the revert process introduces conflicts, Git will prompt you to resolve them manually. After resolving any conflicts, commit the changes:

Terminal
git commit -am "Revert merge of PR #xx"

Once the revert is committed locally, push the changes to the remote repository:

Terminal
git push origin main

This command updates the remote main branch with the reverted changes.

Bitbucket allows you to revert a merge through its UI, but you can also do this via command line using the same git revert command, as Bitbucket hosts Git repositories.

Identify the merge commit using the git log command, similar to the process in Git.

Run the git revert command with the -m 1 option to revert the specific merge commit:

Terminal
git revert -m 1 <commit-sha>

As with Git, you may need to resolve conflicts. Do this manually, then commit the changes:

Terminal
git commit -am "Revert PR merge in Bitbucket"

Push the revert to the remote Bitbucket repository:

Terminal
git push origin main

Graphite CLI simplifies various Git operations, including reverting merges. Here’s how you can use Graphite to revert a merged pull request:

First, ensure that the Graphite CLI is installed on your machine. You can install it using brew or npm.

brew:

Terminal
brew install withgraphite/tap/graphite
gt --version

npm:

Terminal
npm install -g @withgraphite/graphite-cli@stable
gt --version

Once Graphite is installed, you can use it to revert a merge commit more efficiently. The CLI provides a streamlined command that can handle the revert operation, including conflict resolution guidance:

Terminal
gt stack revert <stack-name>

Replace <stack-name> with the appropriate stack or branch name that you need to revert.

Reverting merged pull requests is an essential operation when dealing with errors in codebases. Using the command line provides a direct and controlled method to roll back changes, ensuring that developers can manage their code efficiently. With tools like Graphite CLI, the process becomes even more streamlined, offering additional functionality to handle complex scenarios involved in reverting code changes.

Git inspired
Graphite's CLI and VS Code extension make working with Git effortless.
Learn more

Built for the world's fastest engineering teams, now available for everyone