skip to content
Andrei Calazans

How do you change the base of a branch in Git?

/ 1 min read

Today I Learned

How do you change the base of a branch in Git?

Assuming “newerMaster” is the branch you want to move your commits into, and “olderMaster” is the old base for your branch, you can use —onto for that:

git rebase —onto newerMaster olderMaster feature/branch

Example:

I needed to figure this out because I branched off a feature branch, this feature branch got merged into master, and now I needed to just change my base to master.

I could have simply merged the lastest master into my branch with git fetch && git merge origin/master. But, this adds a commit to my history and I might want to only display my changes in my commit history.

Instead I used the rebease --onto pattern:

  • Feature branch I checkout off of: new_signed_out
  • My branch: signed_out_button_v2

git checkout signed_out_button_v2 # Just to be clear which branch to be on. git rebase —onto master new_signed_out signed_out_button_v2

source