Skip to main content

Command Palette

Search for a command to run...

TIL - Rebasing a branch onto a different base

Updated
2 min read
TIL - Rebasing a branch onto a different base

Today I learned how to move a branch that was incorrectly based on another branch to a different starting point. This was useful when I wanted to merge a feature branch sooner but realized it was branched off the wrong base.

The Scenario

  • Branch A is the main branch.

  • Branch B was created from A and has some changes.

  • Branch C was created from B but makes completely different changes than B.

  • I needed Branch C to be based directly on Branch A so I could merge it sooner without waiting for B.

The Solution: Rebasing Onto a Different Base

To move Branch C’s changes onto A, I used the following rebase command:

git checkout c
git rebase --onto a b c

Breakdown of the Command:

  • git checkout c → Switch to Branch C.

  • git rebase --onto a b c → Rebase branch C from being based on B to instead be based on A.

    • --onto a → Moves branch C to start from branch A.

    • b c → Tells Git that branch C was originally based on B and should be moved.

Handling Conflicts

If there were conflicts, I resolved them manually and continued the rebase with:

git rebase --continue

If needed, I could abort the rebase with:

git rebase --abort

Pushing the Updated Branch

Since this rewrites history, if Branch C was already pushed, I had to force push:

git push --force

Key Takeaways

  • Use git rebase --onto <new-base> <old-base> <branch> to move a branch to a new base.

  • Always check for conflicts and resolve them carefully.

  • Force-pushing (--force) is needed if the branch was previously pushed, but use it cautiously.

This trick helped me clean up my Git history and merge my feature branch faster!