How to Rebase
Say you have a branch called hotfix
. You originally started by branching off of develop
, but this was accidental. You need to rebase this branch from develop
to main
to release it as a hot fix. Here's an easy-to-follow guide on how to do it:
On your
hotfix
branch, rungit rebase develop
.You may see merge conflicts. These may need to be resolved. You will need to use your best judgement here, as it is always specific to the codebase and the branch. In my experience, I see this really commonly with database schema updates.
After you resolve conflicts, DO NOT push/pull.
Check out
main
andgit pull
to make sure you have the most up-to-date changes there.Back on
hotfix
branch, rungit rebase --onto main develop
. This is saying:git rebase --onto <new-branch> <old-branch>
.You'll probably see some diff in the push/pull section. Don't pull.
Instead, force push:
git push origin hotfix --force
.If you already have a PR open for this branch, you may see a bunch of commits that don't belong to you. In GitHub (or wherever your code lives), you will need to point the merge branch toward
main
. The commits that don't belong to you should disappear, and only your commits should be left.If you don't have a PR open already, open one to
main
.
To rebase a hotfix branch from develop to main, first rebase onto develop and resolve any conflicts. Then, update main and rebase the hotfix branch onto main. Finally, force push the changes and adjust any open PRs to point to main.