How to Rebase


2 min read

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:

  1. On your hotfix branch, run git rebase develop .

  2. 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.

  3. After you resolve conflicts, DO NOT push/pull.

  4. Check out main and git pull to make sure you have the most up-to-date changes there.

  5. Back on hotfix branch, run git rebase --onto main develop. This is saying: git rebase --onto <new-branch> <old-branch>.

  6. You'll probably see some diff in the push/pull section. Don't pull.

  7. Instead, force push: git push origin hotfix --force.

  8. 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.

  9. 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.