Did you know about 'git rebase --onto'?

1. You create branch1 from master
2. You create branch2 from branch1
3. PR for branch1 gets merged into master

Now you want to rebase branch2 off of the new master, so you can make a clean PR for it.

This is actually easy!

$ git checkout branch2
$ git rebase --onto origin/master branch1 branch2

This takes all the changes between branch1 and branch2 and replays them onto master, so it's as if branch2 had been branched from master.

@fribbledom If branch1 has already been merged into master, wont a simple git rebase origin/master do this?

@fribbledom But in this case, why not simply do git rebase master?

@deshipu @fribbledom All the times I've tried to `--onto` I've messed it up and ended up cherry-picking to fix the mess.

@gws that is why `git rebase --interactive` exists 😉
use it lavishly when you're not sure where the changes come from or go to.

I think I am fairly good with branching and rebasing, but I still use the interactive flag when I'm not sure what is coming from where and where it is going.

@deshipu @fribbledom

@shine @deshipu @fribbledom `rebase` gets uncooperative with my aberrant (or was that 'abhorrent'? yeahyourerightitsboth) git+Perforce custom workflow... But good news, I heard recently we're going to migrate to all-git, so things are looking up.

@fribbledom Or... just do this:

git checkout master
git pull --ff-only
git checkout branch2
git rebase master

Same effect as that --onto.

@fribbledom I avoid creating branches that is not branched out of the default branch unless it is a long-running branch ( which is a rare-case scenario ).

by the way, thank you for the tip :)

@fribbledom I do! I discovered it recently and by accident!

Pretty handy when I am working on a branch of a branch and the parent branch gets squashed & merged.
Sign in to participate in the conversation

Server run by the main developers of the project 🐘 It is not focused on any particular niche interest - everyone is welcome as long as you follow our code of conduct!