git-fu
June 4, 2025
git
is one of those tools that you learn once and you never have t—and then you learn twice, and then you learn thrice, and then you learn…
And then you write a blog post about it. So anyway, this is an overview of learnings using git
over the years. They are mostly public notes for myself and I will try to keep this post updated whenever new things come up.
git rebase --onto
The command that prompted me to write this post originally.
Scenario:
- you have a
main
branch that is protected and only allows squash-merged PR commits; - you implement a feature in a
feature-A
branch; - you then branch off of
feature-A
as you’re working on a feature in afeature-B
branch that relies on changes implemented earlier in thefeature-A
.
How do you merge this? The normal stacked PR strategy would be:
- merge
feature-B
intofeature-A
- merge the updated
feature-A
intomain
.
However, what if the changes in feature-A
have been reviewed and should land ASAP. If you merge feature-A
into main
, the feature-B
will be orphaned, it will rely on commits that are no longer there (remember: we’re squash-merging).
This is where git rebase --onto
is helpful. You need to make sure of two things:
feature-B
branch locally is up to date with all the latest commits fromfeature-A
main
branch locally has the squash commit of the mergedfeature-A
Run:
git checkout feature-B # just to make sure we're here
git rebase --onto main feature-A
This command takes all commits from your current branch (feature-B
) that aren’t in feature-A
(i.e. that have been added on top of feature-A
changes), and replays them on top of main
.