Skip to content

Instantly share code, notes, and snippets.

@nh2
Last active April 3, 2020 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nh2/3a16a20cecf59b21be248607bf9b9c46 to your computer and use it in GitHub Desktop.
Save nh2/3a16a20cecf59b21be248607bf9b9c46 to your computer and use it in GitHub Desktop.
`git rebase --onto` for switching target branches

git rebase --onto for switching target branches

Git rebase with --onto implements the idea:

cut and paste from start commit B to end commit E and put it "onto" commit O.

nixpkgs example

If you have a situation where somebody made a somebranch PR against master but it should have been against another branch, like this:

              o---o---o  somebranch
             / 
o---o---o---B---o  master
     \
      o---o---o---o---o  haskell-updates

(Note the commit where somebranch branches off master is called B for "merge base").

Then you can switch the the somebranch to be based on haskell-updates instead by using git rebase with --onto.

In this case, it is:

cut and paste from start commit B to end commit somebranch and put it "onto" commit haskell-updates.

So we run (while being on somebranch):

git rebase B somebranch --onto haskell-updates

Result

o---o---o---B---o  master
     \
      o---o---o---o---o  haskell-updates
                       \
                        o---o---o  somebranch

Then you can switch the base in Github. (You can also do this before.)

Interesting to know

This approach is a short-cut for going onto haskell-updates and using git cherry-pick for every single commit in somebranch manually (and then renaming the result of that back to somebranch).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment