Alex Rivera | Logout

How to merge two branches without a common ancestor?

Asked 2009-09-28T18:56:55.337
38

I have started using Git in the middle of my project, where the first two commits are just some initial settings (.gitignore and .gitattributes), and the third commit M2 adds the content of the SVN trunk:

I1 -- I2 -- M2 -- N -- .. -- Z

I have imported the SVN history in a branch named svn, where M1 is the SVN trunk (with the same content as M2, except .gitignore and .gitattributes):

A -- B -- ... -- K -- L -- M1

Q: What is the best approach in merging both branches?

I could merge M1 and M2 into M3, and then rebase, but I don't know how to delete the I1 and I2 commits and if I can safely remove the M3 commit (I have found some advices to preserve the merge commits, but in this case M3 it's not necessary anymore).

A -- B -- ... -- K -- L -- M1
                             \
                              M3 -- N' -- .. -- Z'
                             /
               I1 -- I2 -- M2 -- N -- .. -- Z

Another way would be to cherry-pick the N .. Z commits into svn branch by hand, but I would like to avoid this approach.

The most elegant solution would be to rebase the changes introduced by N .. Z commits on top of svn branch, but I didn't found yet the required syntax for two branches without a common ancestor.

Edit
Report

1 Answer

5

I'd do the following:

git checkout M1
git cherry-pick I1
git cherry-pick I2

That adds .gitignore and .gitattributes to your branch containing the nicer history.

And then just set the new commits on top of that one:

git filter-branch --parent-filter 'if $GIT_COMMIT = $hash_of_N; then printf -- '-p
$hash_of_cherrypicked_I2\n'; else cat; fi'

The downside of this is you rewrite history.

So an alternative approach is to create a script similar to the one for the Linux kernel and put that in your repository.

answered 2011-01-17T20:05:55.223

Your Answer