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
master
: the new base you want to movePRO
to.demo
: the old base wherePRO
was originally created from.PRO
: the branch you're rebasing (you must be on this branch before running the command).
This command tells Git:
"Take all commits on
PRO
that came afterdemo
, and reapply them on top ofmaster
."
๐ 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.