David Parks
David Parks

Reputation: 32081

Reordering git branches: rebasing to an earlier branch in a chain pulls in many extra commits

I have the following:

master
      \
       branchA -> branchB -> branchC

I want to change the order to branchA -> branchC -> branchB, which is the order I'll submit merge requests to master.

When, on branchC I do rebase branchA it seems like I'm causing branchB's commits to become part of branchC.

I can rebase -i to remove them. But I'm clearly doing something wrong here, and I don't really understand what it is I'm doing wrong. What is the correct way to reorder these branches?

Upvotes: 1

Views: 35

Answers (1)

eftshift0
eftshift0

Reputation: 30212

The thing is that git can't just guess that branchB is something you don't want when you ask to rebase branchC on top of branchA. branchB is part of its history and so git guesses you want it too. There's a special syntax in rebase for this to work: git rebase --onto branchA branchB branchC.


Addition: Copying in the relevant part of the documentation as it was quite helpful:

https://git-scm.com/book/en/v2/Git-Branching-Rebasing

$ git rebase --onto master server client

This basically says, “Take the client branch, figure out the patches since it diverged from the server branch, and replay these patches in the client branch as if it was based directly off the master branch instead.” It’s a bit complex, but the result is pretty cool.

Upvotes: 2

Related Questions