Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging two different repositories

I've 3 repos, A, B and C, all related to the same project. A is the oldest version of the project, B is an experiment that didn't really go anywhere and C is the latest working version. All these repos have different files - they're different implementations of the same product.

I'd like to merge these 3 repos into one keeping their history - this is vital. I guess I want to stack B on top of A and C on top of B but when I checkout the project I only want to get the changes related to repo C.

My idea is to tag or create a named branch on A, hg rm *, commit and then pile B on top. Repeat the same with B so I can pile C and then continue on the project as usual.

What do you think? Also, some inspiration for what I want to do: 1, 2.

like image 540
ruipacheco Avatar asked May 04 '12 12:05

ruipacheco


People also ask

Can you merge two repos?

If you actually want to merge both repos, this step is unnecessary. $ git rm -rf * $ git commit -m "Delete all the things." Merge master-holder into master. (If you didn't do the delete step above, you have to option of git checkout master-holder; git rebase master instead.)

How do I merge two Git repository and keep history?

To combine two separate Git repositories into one, add the repository to merge in as a remote to the repository to merge into. Then, combine their histories by merging while using the --allow-unrelated-histories command line option.


1 Answers

You can just pull the changesets into one big repository. Start with the A repo:

$ hg init combined $ cd combined $ hg pull ../A $ hg update 

Then forcibly pull in B and dummy-merge the two heads so that you keep the files from B:

$ hg pull ../B --force $ hg merge --tool internal:other $ hg revert --all --rev tip $ hg commit -m "Merge A and B, kept B" 

Then repeat for C:

$ hg pull ../C --force $ hg merge --tool internal:other $ hg revert --all --rev tip $ hg commit -m "Merge B and C, kept C" 

That gives you three lines of history and a merge point where you first throw away A, then B, and finally end up with C.

Alternatively you can use the convert extension with a splice map to splice the three histories together. Start by forcibly pulling all the changesets into a single repository. Then do a Mercurial to Mercurial conversion where you add the tip of A as the first parent of the root of B. Similarly for B and C. That gives you one long history where there will be a very abrupt change when you go from A to B and from B to C.

I would go with the first option: it's the most explicit and shows best what happens without faking (converting) the history.

like image 170
Martin Geisler Avatar answered Oct 10 '22 15:10

Martin Geisler