DEV Community

Maksim Nesterenko
Maksim Nesterenko

Posted on

Rebase a feature branch that is based on another feature branch which got merged

So imagine you have 3 branches, where F1 is based on master and F2 depends on F1

*-*-*               (master)
     \ %-%-%        (F1)
            \ x-x-x (F2) 
Enter fullscreen mode Exit fullscreen mode

To have F2 up-to-date with F1 you rebase it like

git checkout F2
git rebase F1
Enter fullscreen mode Exit fullscreen mode

or, if someone else is pushing to F1 besides you and your local F1 might be not up-to-date, you do:

git checkout F2
git fetch
git rebase origin/F1
Enter fullscreen mode Exit fullscreen mode

Now, the work in the F1 branch is done and it's finally been merged into the master.

The problem though, is that F2 is still based on top of F1 and contains its commits, so when you open a pull request for F2 against the master it will contain the changes that do not make sense (commits from F1 that got merged already).

The git tree looks like this now:

*-*-*-%-%-%        (master)
     \            
       %-%-%-x-x-x (F2) 
Enter fullscreen mode Exit fullscreen mode

What we want here is to have our remaining F2 feature branch to be up-to-date with the master and contain only the relevant commits.

This can be done with --onto rebase flag. So you can do this:

# just to make sure the F2 is up to date with the F1 first
git checkout F2
git rebase F1

# then change the base to be master
git rebase --onto master F1
Enter fullscreen mode Exit fullscreen mode

That'll turn your git tree to be like this instead:

*-*-*-%-%-%       (master)
           \            
            x-x-x (F2) 
Enter fullscreen mode Exit fullscreen mode

After this, the commits in your pull request for F2 -> master will make sense again.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️