Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I merge two Git repositories with similar content, but not sharing any ancestry?

Tags:

git

merge

I have two code bases, Code base-1, Code Base-2.

I run the commands ...

git init
git add .
git commit -m "Initial Commit"

... in both repositories (directories).

There are code differences between code base-1 and code base-2.

I can't branch them because they already contain differences of about 0.1%. The filenames are the same but there are some slight edits to the code.

Is there a way to merge the differences between the two repositories?

Edited:

For example, let's say I have the following. This is what I am starting out with. There are slight differences between codebase 1 and 2.

[oldest code case] 
code-base-1/ 
code-base-1/.git  [git stuff with already created repo] 
code-base-1/file1 
code-base-1/file2 

code-base-2/ 
code-base-2/.git [git stuff with already created repo] 
code-base-2/file1 
code-base-2/file2 

Ideally, I could delete code-base-2 because it is a little newer.

How would I merge these code bases, so that I eventually come out with one with the merged files?

like image 499
Berlin Brown Avatar asked Jul 17 '09 03:07

Berlin Brown


People also ask

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.

Can you merge two GitHub repositories?

You can merge repository A into a subdirectory of a project B using the subtree merge strategy. This is described in Subtree Merging and You by Markus Prinz. (Option --allow-unrelated-histories is needed for Git >= 2.9.

Does merging change both branches?

Merging Branches. Once you've completed work on your branch, it is time to merge it into the main branch. Merging takes your branch changes and implements them into the main branch. Depending on the commit history, Git performs merges two ways: fast-forward and three-way merge.


1 Answers

The problem might comes from the fact those are two different repository (with a different SHA-1 first node), and not one repo cloned and then modified.
That means a 'git fetch' is probably not enough.

You could simply use an external tool (external to Git that is) to merge the content of CB2 (Code-Base-2) into CB1, like WinMerge as suggested in the question "Merging in changes from outside a git repository".

The other option would be using some graft technique to complete a fetch: see the question "Can two identically structured git repos be merged when they have never had any common history?"

$ cd project1
$ git config remote.project2.url /path/to/project2
$ git config remote.project2.fetch 'refs/heads/*:refs/project2/*'
$ git fetch project

Plus modify the graft file (.git/info/grafts) to link the commits of project2 to project1.

like image 149
VonC Avatar answered Sep 19 '22 09:09

VonC