Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update branch with rebase instead of merge

Tags:

github

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

like image 231
Ufkoku Avatar asked Jan 19 '26 07:01

Ufkoku


2 Answers

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.

like image 66
VonC Avatar answered Jan 21 '26 02:01

VonC


IMPORTANT: The recommendation below will not work if main contains any merge commits. :(

NOTE: With the recommendation below, a GitHub bug causes "Rebase branch" to be initially displayed as "Update branch". Refreshing the PR page will show "Rebase branch" and is required to perform a rebase. :(

This is now supported by Github by enforcing a linear branch history for all branches (see reference). Steps:

  1. Open repository Settings

  2. Rules → Rulesets

  3. New Ruleset → New Branch Ruleset

  4. Ruleset Name: “Linear History”

  5. Enforcement Status: Active

  6. Add Target → Include all branches

  7. Branchset rules → Require linear history (only)

  8. Create

The default when a GitHub branch is out of date should now be "Rebase branch" (requires page refresh as per note above).

like image 27
michael Avatar answered Jan 21 '26 01:01

michael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!