How to Rebase a Pull Request on GitHub?
Rebase is a Git command that allows you to replay your changes on top of a new base branch. This can be useful for keeping your pull requests up-to-date with the latest changes to the main branch, or for resolving merge conflicts.
On GitHub, you can rebase your pull request branch on top of the selected base branch. This will create a new commit history for your pull request, and it may introduce merge conflicts. You will need to resolve any merge conflicts before you can continue.
Once you have resolved all of the merge conflicts, you can click the "Merge" button to merge your pull request.
Why Rebasing your Pull Request on GitHub?
While you are aiming to merge your pull requests, other changes might get merged into the main branch a few minutes before your merge. These changes are making your pull request's head outdated and out of sync.
Now, you want to be sure your pull request is up-to-date with the latest version of the base branch. This way you can catch problems prior to merging.
You can update your pull request in two different ways: a rebase or a traditional merge. You can perform this update from the command line or the pull request page.
On the pull request page, the "Update branch" button is displayed and clickable when all these conditions are true:
- No merge conflicts between the PR branch and the main branch.
- The PR branch is not up to date with the main branch anymore.
- Branch protection requires branches to be up to date before merging.
Updating Pull Request: Traditional Merge Vs. Rebase
Once you are on GitHub's pull request page, you can choose to update your pull request using a traditional merge or by rebasing.
A traditional merge consists of merging the latest version of your base branch into the branch of your pull request. It will result in a merge commit that merges the base branch into the head branch of the pull request.
On the contrary, by rebasing, you apply the changes from the pull request's branch onto the latest version of the main branch. This will result in a linear history since no merge commit is created.
How to rebase a pull request on GitHub
- Go to the pull request on pull requests page on GitHub.
- Click the "update branch" drop down menu.
- Choose "Update with rebase".
- Click Rebase branch to update by rebasing on the base branch
Your pull request is now up-to-date with the main branch.
Additional tips for rebasing pull requests on GitHub
- Only rebase your pull request if you are sure that you have not pushed your changes to the remote repository.
- Before you rebase, make sure that you have a backup of your pull request branch. This way, if something goes wrong, you can revert to the original branch.
- If you are rebasing a pull request that has already been reviewed, be sure to notify the reviewer of the changes.
- Rebasing can be a useful tool for keeping your pull requests up-to-date, but it is important to use it carefully.
Automate Pull Request Rebasing
If you want to go further, you can automate the rebasing of your pull requests.
Yes, it's tempting: no more headaches, all your PRs will always be up to date.
You can do this with tools such as Mergify and its merge queue.
It rebases each PR before launching the tests and merging it. This ensures that you only test pull requests that are up to date, and secures your main branch.