Skip to content
← Back to all posts

How to move commits from one branch to another with git rebase onto

22 March 2019

Written by 

A photograph of Richard Gubby

Richard Gubby

You’ve branched off a branch. Unfortunately the original branch is not ready to be merged and you need to promote your branch to a first class citizen, branched off of master.

However, when you raise the pull request for the second branch, you get all the changes from the first! Of course it does - you’ve branched off a branch!

You could wait till the original branch is ready, but alternatively, you can use git rebase to help you.

This is how your branch setup might look like:


And you want it to look like this:


Here is a useful (and I have also found to be much under-documented) CLI command that you might want to try out:

git rebase --onto master branchA branchB

This moves branchB from branchA and onto master. You could also do this with other branches too - but then that could become really complicated!

You’ll also need to do a git push -f origin branchB to force the update - and now when you raise a pull request, you will only pick up the changes from branchB.

When might this be useful? One example might be when you have long lived branches and you need to move subsequent branches from it and onto master for merging.

Do you have any git tips you can share? If you do, let us know your “Git Tip of the Day” via Twitter.