๐Ÿ› ๏ธ How to Change the Base of a Git Branch

Today

Sometimes while working with Git, you may accidentally create a branch from the wrong base. Here's a common scenario:

You have a tree like this:

(commit 1) - master
               \
                (commit 2) - (commit 3) - demo
                                             \
                                              (commit 4) - (commit 5) - PRO

In this case, the PRO branch was mistakenly created from demo, but you actually intended to branch it off from master.

What you want instead is:

(commit 1) - master
             |-- (commit 2) - (commit 3) - demo
             \-- (commit 4) - (commit 5) - PRO

โœ… Goal

Move the PRO branch so it branches directly from master, without affecting demo.

๐Ÿงช Solution โ€” Use git rebase --onto

Git allows you to change the base of a branch with a single command using --onto.

๐Ÿ“Œ Command

git checkout PRO
git rebase --onto master demo PRO

๐Ÿง  What It Means

This command tells Git:

"Take all commits on PRO that came after demo, and reapply them on top of master."

๐Ÿ“š Recap

If you accidentally branch off the wrong base in Git, donโ€™t panic. You donโ€™t need to delete or recreate your branch. Just use:

git rebase --onto <new-base> <old-base> <branch>

This is a clean and safe way to reorganize your commit history while keeping unrelated branches intact.


Tip: Always double-check your work with git log --graph or a visual tool before and after rebasing.