tags:

views:

47

answers:

1

Oops! It seems I did something wrong the last time I merged two branches in my repository:

Original repository

This was far from what I was expecting. Is there a way to arrange this mess and obtain something like the following? What did I do wrong?

What I am looking for

+4  A: 

A merge commit should have two parents. That's the entire point of a merge.

If you have a really good reason for wanting a merge with only one parent, you can use the --squash option:

# starting with branch-full-reordering at "Corrected GPU float measures"
git merge --squash branch-full-reordering-wo-auxarray-wo-diver
# go on to make the "improve performance commit"

but this is very, very rarely what you want. It dumps all the work done on that other branch into a merge commit, destroying history.

The history you describe, though, isn't a merge at all, squash or otherwise. It's a rebase.

# starting with branch-full-reordering at "Corrected GPU float measures"
git rebase branch-full-reordering branch-full-reordering-wo-auxarray-wo-diver
# go on to make the "improve performance commit"

In this case there will be no merge commit - you've simply transplanted (rebased) the secondary branch onto the primary one.

I should emphasize again: the history you ended up with is likely what you want. It records the fact that some of the development took place on a side branch, then was merged back in.

To revise your history to look the way you want it, you could do something like this:

# recreate your topic branch
git branch topic <SHA1 of commit "Correct pesky bug">
# overkill: you could also specify that as branch-full-reordering^^2
# which means the second parent of the [first] parent of the branch tip

# rebase the topic branch like you meant to in the first place :)
# ~2 means two commits before
# you could also just use the SHA1 of commit "Corrected GPU float measures"
git rebase branch-full-reordering~2 topic

# rebase the main branch onto the topic
# the merge will be ignored, so this will just move the "Improve performance" commit    
git rebase topic branch-full-reordering

# delete the topic branch
git branch -d topic
Jefromi
Yes, maybe what I was looking for was really a rebase and not a merge. Sorry, I'm a bit newbie in the git intricacies. But what it's done, it's done. Can I change repository in its current state to something like the second pic?
Auron
@Auron: Yes, definitely. You can do anything! I'll edit in some instructions.
Jefromi
I tried rebasing but it is more complicated than I originally thought. I think I will leave the two branches because, as you said, it reflects better the way I developed both branches. Thank you!
Auron