nccc
nccc

Reputation: 1155

Rebase a merge commit

Suppose my Git repository initially has two branches: Foo and Bar.

... ─ Foo

... ─ Bar

I create a third branch, FooBar in which I commit the merge of the two other branches.

... ─ Foo ──┐
            FooBar
... ─ Bar ──┘

FooBar is now one commit ahead of both Foo and Bar. Next, I do some more work, committing a few times on Foo only.

... ── A ───┬── B ── C ── D ── Foo
            FooBar
... ─ Bar ──┘

The question is: since the first parent of branch FooBar is no longer Foo, can I rebase the merge commit in branch FooBar to again have Foo and Bar as its two parents? In other words, can I incorporate the development in Foo into the previously merged FooBar along with the unchanged Bar?

... ── A ── B ── C ── D ── Foo ──┐
                                 FooBar
... ─ Bar ───────────────────────┘

Upvotes: 12

Views: 1724

Answers (3)

Aki Koskinen
Aki Koskinen

Reputation: 142

I realize this is quite an old topic but since I found this question while facing a similar situation I could as well tell what I used in the end.

git checkout FooBar
git rebase -p --onto Foo A

I tried to use the names from the original question. Note especially that A means the commit that is marked as A in the ascii art. You would probably use the hash of the commit instead or any other way to point to the commit where branches Foo and FooBar originally separate.

The -p flag is also known as --preserve-merges and it does pretty much what it says.

Note that the branch Bar doesn't play a role here so that line of commits might be a named branch or then not - it makes no difference.

Upvotes: 12

Mikko Rantalainen
Mikko Rantalainen

Reputation: 16025

You're basically trying to erase all traces of the previously made merge.

If you truly want to throw away all commits that only exists in FooBar (and in the working directory), then proceed as follows: first do git checkout FooBar, then make git forget about it's history and do git reset --hard Foo (if you intent to merge Bar into Foo). Then you recreate the merge with git merge Bar.

Upvotes: 0

Andy
Andy

Reputation: 46494

You couldn't rebase under branch FooBar without changing what defines FooBar. What you could do is merge FooBar and Foo. That would have the same contents that you desire.

Upvotes: 1

Related Questions