git rebase-onto

Problem

You have a branch feature-a. You finish it and submit it for review, and then branch off of feature-a to start working on feature-b which depends on your latest changes.
Your code is reviewed and some changes are requested. You update feature-a, push, and merge. You want to keep feature-b up to date, so you check it out and run git rebase origin/master. However, your latest revisions lead to a self merge conflict when trying to rebase feature-b off of master.
1
# Before code review
2
# origin/master
3
master
4
# feature-a
5
master -> feature-a
6
# feature-b
7
master -> feature-a -> feature-b
8
9
# After code review
10
# origin/master (using master as parent for consistency, though HEAD is now feature-a')
11
master -> feature-a' # revised feature-a is now HEAD
12
# feature-b (feature-a conflicts with feature-a')
13
master -> feature-a -> feature-b
Copied!

Solution

Instead of git rebase origin/master, use git rebase --onto origin/master feature-a.
Specifically, this means "take all commits that are on top of feature-a, and place them on top of origin/master's HEAD." In the common scenario where feature-a is the only difference between origin/master and feature-b, it can be thought of as "drop all changes between origin/master's HEAD and feature-a inclusive."
Copy link