Ufkoku
Ufkoku

Reputation: 2638

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

Upvotes: 12

Views: 8664

Answers (2)

VonC
VonC

Reputation: 1324537

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.

Upvotes: 12

l4mpi
l4mpi

Reputation: 5149

I doubt github supports this, as you should never rebase a public branch. From the official git docs:

Rebasing (or any other form of rewriting) a branch that others have based work on is a bad idea: anyone downstream of it is forced to manually fix their history. This section explains how to do the fix from the downstream’s point of view. The real fix, however, would be to avoid rebasing the upstream in the first place.

The easiest solution would be to simply use a merge. If you don't like that for any reason, you could create a new branch from main, apply the desired changes (e.g. by using git cherry-pick, or git diff in conjunction with patch), and then delete the old branch and create a new PR. If you really want to use rebase, you can do so locally and force-push the branch, but again, that's a really bad idea as it falsifies history and breaks the branch for everybody else.

Upvotes: -5

Related Questions