Skip to content

Provide gh cli Support to Revert Pull Request #6034

Description

@duffenterprises

Describe the feature or problem you’d like to solve

To support automation and CI/CD it would be beneficial for the GitHub CLI to support reverting a pull request equivalent to what the UI is able to accomplish.

Proposed solution

gh pr revert 416967111

Additional context

In a multi-contributor, pull-request managed repository there are limits to what linting/formating/plan (e.g. terraform plan) can achieve before attempting to deploy (e.g. terraform apply). To increase velocity, and minimize effort required by maintainers, it would be beneficial to be able to revert a PR following a failed build/deploy.

Activity

  1. added
    platformProblems with the GitHub platform rather than the CLI client
    and removed on Aug 4, 2022
  2. mislav commented on Aug 4, 2022

    @mislav
    Contributor

    Thanks for the feature request! Agreed that this would be useful, but I do not think there is an API to revert a merged PR. Until then, we cannot act on adding this to CLI.

    In the meantime, you can use gh pr view to determine the merge commit for a PR and use git locally to revert that merge commit:

    gh pr view PRNUM --json mergeCommit -q .mergeCommit.oid
    
  3. azizshamim commented on Aug 5, 2022

    @azizshamim

    There's a revertResourcePath attribute in the pullrequest Object in the GraphQL API. Unsure if that's useful for request, but worth looking at.

  4. duffenterprises commented on Jan 11, 2023

    @duffenterprises
    Author

    @azizshamim @mislav Any chance there has been any progress here?

  5. vilmibm commented on Jan 11, 2023

    @vilmibm
    Contributor

    There's a revertResourcePath attribute in the pullrequest Object in the GraphQL API. Unsure if that's useful for request, but worth looking at.

    This URL is for use in the browser; the best the CLI could do is link to it.

  6. colindembovsky commented on Jan 12, 2023

    @colindembovsky

    One team is migrating from Gerrit and want to mimic some of the capabilities that Gerrit has. Gerrit has topics which are essentially multi-repo PRs. GitHub PRs are scoped to a single repo.

    What they are planning to do is link a set of PRs (to simulate the topic construct). I think this request comes from their commit process - if they approve a topic, they will merge the set of PRs - but if something fails, they want to be able to revert the merge.

  7. mislav commented on Jan 27, 2023

    @mislav
    Contributor

    Update: the platform team just shipped a new API to revert a PR with:

    mutation ($pr: ID!, $asDraft: Boolean = false, $title: String, $body: String) {
    	revertPullRequest(
    		input: {
    			title: $title,
    			body: $body,
    			draft: $asDraft,
    			pullRequestId: $pr,
    		}
    	) {
    		revertPullRequest {
    			url
    		}
    	}
    }

    You can already use this as so:

    # reads the query from a file:
    gh api graphql -F [email protected] -f 'pr=<PR-ID>'

    How should we expose this as a command in gh?

    1. gh pr revert <PR>
    2. gh pr merge --undo <PR> (sort of like gh pr ready --undo)
  8. added and removed
    platformProblems with the GitHub platform rather than the CLI client
    on Jan 27, 2023
  9. 5HT2 commented on Jan 30, 2023

    @5HT2

    I feel like gh pr revert <PR> makes more sense intuitively, as in the web UI 'revert' is shown separately akin to how 'close' is shown separately.

    When you revert a PR, you're not inherently "undoing" the merge, you're rather making a new change that reverts the commit itself.
    gh pr merge --undo might imply to some people that it will undo the state of the pull request / reopen it and so on. It could also be understood to mean restoring the branch to it's former state, as if you had force pushed an older commit back to entirely get rid of the commit history / the fact it was ever merged.

  10. 2 remaining items

  11. mislav commented on Jan 30, 2023

    @mislav
    Contributor

    Work prioritization looks like this: if something is tagged help wanted, that means that there should be enough information in the thread for an outside contributor to pick up the feature and produce a pull request. https://github.com/cli/cli/blob/trunk/.github/CONTRIBUTING.md

    Eventually, our team might also pick up this work, but before we manage to get to it, anyone is welcome to!

  12. dtbell91 commented on May 7, 2024

    @dtbell91

    What's needed to help the PR progress to being reviewed and (hopefully) merged?

    I'm pretty interested in using this functionality after trying out the GraphQL approach with some success.

  13. connor15mcc commented on Sep 10, 2024

    @connor15mcc

    Wanted to check in on the above as well. Happy to jump in if needed to help merge the open PR!

  14. BagToad commented on Apr 2, 2025

    @BagToad
    Member

    @connor15mcc there's nothing currently blocking it AFAIK - it's in our review backlog. Sorry!

  15. connor15mcc commented on Apr 14, 2025

    @connor15mcc

    Ack. LMK if I can provide an extra set of hands!

  16. eknuth commented on Jul 18, 2025

    @eknuth

    AI thinks you can already do that. Looks like reality needs to catch up! :)

    Image
  17. added
    help wanted candidateIssue may be marked as help-wanted but not yet ready to accept PR
    and removed on Jul 28, 2025
  18. BagToad commented on Aug 18, 2025

    @BagToad
    Member

    Status update

    A brief status update from our Help Wanted sync, cc @babakks

    We need to define Acceptance Criteria ✏️

    • revertpullrequestinput takes body, draft, and title. So we should probably support --body, --draft, and --title flags.
    • There's no clean or apparent API driven way to do --web, so we don't want that scoped in on this work.
    • We don't want to prompt for --body, --draft, or --title.
    • The URL of the new revert PR should be returned or an error if there was an error.
    • We do not want any local branch PR detection to decide which PR to revert, which would make the argument optional. We don't want this because the value proposition for such a feature isn't very apparent and there are light risks to reverting a PR that you didn't intend to 🤷

    After that, we should mention the interested parties and see if anyone is interested in continuing the work on the #8826

  19. self-assigned this
    on Aug 18, 2025
  20. BagToad commented on Sep 24, 2025

    @BagToad
    Member

    Acceptance Criteria

    1. Missing argument
      Given I run gh pr revert with no <PR>
      Then it exits non-zero and prints an argument error.

    2. Accepted PR identifier formats
      Given a merged PR exists
      When I run gh pr revert 123, gh pr revert owner/repo#123, or gh pr revert https://github.com/owner/repo/pull/123
      Then a revert PR is created and only its URL is printed.

    3. Merged PR requirement
      Given the target PR is not merged
      When I run gh pr revert <PR>
      Then it fails (API error), no URL printed, non-zero exit.

    4. Basic revert (no flags)
      Given a merged PR
      When I run gh pr revert <PR>
      Then a revert PR is created with backend-generated title/body; only URL printed.

    5. With title/body
      Given I pass --title "T" and/or --body "B"
      When I run the command
      Then those values are sent; revert PR created; only URL printed.

    6. With draft
      Given I pass --draft
      When I run the command
      Then the revert PR is a draft; only URL printed.

    7. API failure (permissions / already reverted / conflict / other)
      Given the API rejects the mutation
      When I run the command
      Then stderr shows the API error; stdout empty; non-zero exit.

    8. Multiple invocations
      Given I run the command multiple times on the same merged PR
      Then behavior depends solely on API response (no local duplicate guard).

    9. No implicit PR inference
      Given I am on a branch related to the original PR
      When I omit <PR>
      Then the command fails as in Scenario 1 (no auto-detection).

  21. added and removed
    help wanted candidateIssue may be marked as help-wanted but not yet ready to accept PR
    on Sep 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementa request to improve CLIgh-prrelating to the gh pr commandhelp wantedContributions welcome

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions