Ian Barkley-Yeung
Ian Barkley-Yeung

Reputation: 151

git: How to base a branch on multiple local branches?

I'm developing a new feature. I need to modify two different parts of the existing code base before I can write my feature in a third place.

Since our code review policy is to make changes as small as possible, I made the two changes in different branches. So my commit tree looks like

              branch_1
             /
remote/master
             \
              branch_2

Where both branch_1 and branch_2 are have their upstream set to remote/master (that is, I did git branch --set-upstream-to=remote/master on both).

So far, so good; after syncing remote/master, I can do git pull --rebase to update each branch.

Now, however, I want to work on my actual feature, which requires the changes in both branch_1 and branch_2. But I expect that the commit at the tips of both branch_1 and branch_2 will be updated as I develop the feature, so I need to be able to pull in changes from both branch_1 and branch_2 whenever they are updated. In other words, I want:

              branch_1
             /        \
remote/master          feature
             \        /
              branch_2

In the feature branch, I want to see the latest changes from both branch_1 and branch_2, or at least be able to pull them in by using something like git pull

Is there any way to do this?

Note: I can't have branch_1 use branch_2 as its upstream, or vice versa. The tooling we use (Gerrit's repo upload) just doesn't support it.

Upvotes: 2

Views: 1523

Answers (1)

VonC
VonC

Reputation: 1324218

Once you have identified that the feature to be delivered needs both branch1 and two, one possible path forward would be to merge both branches, and work only with feature from now on.
You would rebase only feature whenever you need to keep it current.

If not, you would need to repeat the merge to feature, which can be problematic if feature has commits of its own which impacts code from both branches: conflicts might occur (git rerere might help to avoid repeating conflict resolution). That seems overly complicated.

Upvotes: 1

Related Questions