Skip to content

gh pr merge sometimes fails with "Base branch was modified. Review and try the merge again." #8092

Open

Description

@itchyny

Describe the bug

We're automating merging PRs with Actions using gh command, but it sometimes fails with the error message GraphQL: Base branch was modified. Review and try the merge again. (mergePullRequest).

Steps to reproduce the behavior

Although it might be possible to create a reproduction workflow, but let me explain by what it does in overview because tedious.

  • Checkout the repository and make some changes.
  • Push and create a new branch with git push origin HEAD:update-20230928175133
  • Create a PR with gh pr create --base develop --head update-20230928175133 --title 'Update develop' --body "some body"
  • Merge the PR with gh pr merge update-20230928175133 --merge --admin
    • This step fails with GraphQL: Base branch was modified. Review and try the merge again. (mergePullRequest)

The version is gh version 2.30.0 (2023-05-30).

This is slightly off topic but our Actions Workflow is aimed for updating k8s manifests, and is triggered by dispatching via API, sometimes multiple requests concurrently.

Expected vs actual behavior

I believe this is a time lag of GitHub internal processing, and it has the same origin as we see in browsers the merge button grayed out in a few seconds when different PR is merged.
I hope gh handles this error gracefully for easiness of use; i.e. retry automatically when this error happens.

Logs

+ gh pr merge update-20230928175133 --merge --admin
GraphQL: Base branch was modified. Review and try the merge again. (mergePullRequest)

Activity

  1. added
    bug
    on Sep 28, 2023
  2. added
    needs-triage
    on Sep 28, 2023
  3. andyfeller commented on Oct 2, 2023

    @andyfeller
    Contributor

    @itchyny : thank you for your patience! 🙇

    Are there any branch protection rules and/or repository rulesets in place that are requiring the branch be up to date before merging?

    This error coming from the GitHub GraphQL API mergePullRequest mutation is stating there have been other changes merged into your base branch and you are unable to merge in your changes despite being a repository administrator. The --admin flag is largely to bypass the GitHub CLI client checks on the PR; if there are branch protection rules or repository rulesets that prevent administrators from bypassing them, then you will have to satisfy the rules in order to merge.

    If one of these rules is in place, then we might need to figure out how to ensure your branch is up to date via the CLI so you can merge it. updatePullRequestBranch comes to mind 🤔

  4. added
    enhancement
    gh-pr
    and removed
    bug
    needs-triage
    on Oct 2, 2023
  5. itchyny commented on Oct 2, 2023

    @itchyny
    ContributorAuthor

    We enable Require status checks before merging but we do not enable Require branches to be up to date before merging. We use --admin to allow it bypass lint checks. We now retry the command to work around this issue.

    # Retry to work around "Base branch was modified." error.
    # Ref: https://github.com/cli/cli/issues/8092
    for i in {1..3}; do
      if gh pr merge "${branch}" --merge --admin; then
        break
      fi
      if [[ $i -eq 3 ]]; then
        exit 1
      fi
      sleep 5
    done
  6. matanagazan commented on Oct 4, 2023

    @matanagazan

    I also experience the same error and I use --admin flag

  7. andyfeller commented on Oct 10, 2023

    @andyfeller
    Contributor

    @itchyny @matanagazan : Out of curiosity, how does this change if you remove --admin?

    You mentioned above you add it in order to ignore your lint status check. Have you tried making that check optional and not leveraging the admin bypass?

  8. itchyny commented on Oct 24, 2023

    @itchyny
    ContributorAuthor
  9. andyfeller commented on Oct 27, 2023

    @andyfeller
    Contributor

    @itchyny , thank you sincerely for following up and doing a little more testing! 🙇 ✨

    I'm raising this up internally with our PR folks to understand the edge case in merging PRs. I'm generally hesitant building in retry logic and increasing API pressure if possible, hence I'm trying to solve this at the source. 💪 Thank you for your patience!

  10. matthiasr commented on Nov 1, 2023

    @matthiasr

    See this discussion – this is happens when the mergeability computation hasn't completed yet; there is some information about this process in the Get PR REST documentation. I believe this would happen if the merge request is the first API request after the base branch has changed; at that time, mergeability is not computed yet, so the request fails. Because this triggers the computation, a retry a few seconds later will be successful (unless there's yet another commit to the base branch in such short order).

  11. andyfeller commented on Nov 16, 2023

    @andyfeller
    Contributor

    @matthiasr has the right of it: the base branch has changed between generating the merge commit and GitHub trying to merge that merge commit.

    From internal discussions, there are no sure fire ideas on how to improve the current experience either server or client side because of what is going on under the hood. There is concern that building in wholesale retry logic would create issues. So the advice, would be putting in a small weight on your end to have it succeed without error more frequently. 🙇

  12. itchyny commented on Nov 17, 2023

    @itchyny
    ContributorAuthor

    Thank you for sharing your discussion conclusions. I understand the difficulty of fixing this kind of behavior (I imagine the complexity of internal asynchronous jobs of mergeability computation). But what solution you would recommend to solve this kind of concurrent processing situation? Do you mean by putting a small wait, sleeping for random duration before invoking the command? This looks less reliable way of solving the issue than retrying like #8092 (comment). I thought handling the errors within the command enables us to implement more sophisticated retrying logic (not sure what it means in graphql but in general practice retry only on 5xx errors or network connection errors, but not on 4xx errors). If gh doesn't support it, users would keep retrying the whole command regardless of the error kinds.

  13. andyfeller commented on Dec 4, 2023

    @andyfeller
    Contributor

    Do you mean by putting a small wait, sleeping for random duration before invoking the command? This looks less reliable way of solving the issue than retrying like #8092 (comment).

    I imagine both would be useful to have: giving the backend GitHub pull request service a moment to handle the merging and retrying in case it we catch it at some point.

    I believe the sleep/wait suggestion was for giving the pull request merge process to recompute commits to avoid an error for race conditions.

  14. samcoe commented on Dec 4, 2023

    @samcoe
    Contributor

    Just wanted to chime in here and say I think adding in an automatic retry mechanism inside of gh feels appropriate considering we know that there is not any path to improve this on the platform side of things. We have a couple cases of using backoff and retrying in the codebase already to address platform limitations, for example in repo fork. I don't think it would be too difficult to do something similar for pr merge.

  15. added a commit that references this issue on Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementgh-pr

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions