Search code examples
github

Update branch with rebase instead of merge


Is there any way to replace merge with rebase at GitHub PRs? I looked through protected branches settings but didn't find such option.

Image


Solution

  • GitHub now (Feb. 2022) supports this (*: caveat, see discussion 12032, detailed at the end):

    More ways to keep your pull request branch up-to-date

    The Update branch button on the pull request page lets you update your pull request's branch with the latest changes from the base branch.
    This is useful for verifying your changes are compatible with the current version of the base branch before you merge.

    Update your pull request branch by rebasing:

    https://user-images.githubusercontent.com/2503052/152357644-6484dc2b-4aae-4977-b76c-b284c5388d7b.mp4

    When your pull request's branch is out of date with the base branch, you now have the option to update it by rebasing on the latest version of the base branch.

    Rebasing applies the changes from your branch onto the latest version of the base branch, resulting in a branch with a linear history since no merge commit is created.

    To update by rebasing, click the drop down menu next to the Update Branch button, click Update with rebase, and then click Rebase branch.

    Previously, Update branch performed a traditional merge that always resulted in a merge commit in your pull request branch. This option is still available, but now you have the choice.

    Note: Because rebasing rewrites the history of the branch, if you are working with the branch locally, you will need to fetch it and do a hard reset to ensure your local branch matches the branch on GitHub.com.

    Learn more about keeping your pull request in sync with the base branch.


    Note: Feb. 2022 discussion 12032 from Feb. 2022 asks (author: Valentin Agachi):

    The new update branch feature is great!

    But for teams which are only using the rebase functionality, it's not optimal to always have to pick the rebase option from a drop down menu.

    It would be ideal if there were a repository level setting to configure which option is the default for that button.

    Another user, Waleed Ashraf confirms more than 2 years later (Sept. 2024, emphasis mine):

    Response from GitHub support as of today,

    Currently, "Update with rebase" as the default when updating a branch from the main branch option is not available.

    We have however heard this request from other customers as well.
    I have gone ahead and added your voice to our internal issue tracking this feature.
    I can not say if or when this feature might be added, but we'll make sure to document it in the GitHub Changelog or the GitHub Blog if it becomes available.