Scroll to top

What I wish I new about `git rebase --onto`

By Tzvi Melamed

Working in a shared git repository, though much nicer (and safer) than trying to manage a large team directly changing files on an ftp server, still has its challenges.

How many times have you been waiting for a PR to merge and created your own stacks of PRs on top of PRs only to have one of those "base" PRs land with a squash merge and send you on an uncomfortable trip down rebase lane? For me, too many times.

What if you could just apply your changes onto the squashed commit without having to handle merge conflicts between the base branch and itself? It turns out, you can with git rebase --onto.

"Git graph diagram depicting a main branch with a develop offshoot that has its own feature X offshooting from hash base-sha. The develop branch is squash-merged back into main with hash merge-sha"

simply find the 2 commit SHAs base-sha from the initial base branch and merge-sha from the new base branch and run git rebase --onto merge-sha base-sha

I hope this helps someone else as much as it helped me 😄