Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge two similar remote git repos into one

Ive done some searches and read a git book and some places on the web like git by example, but I cannot find the correct way to do this.

I have two git repos that sit on two different machines. Each of these git repos holds configuration for a program. When you compare the two repos, some parts of the configuration are identical and some parts are not.

What we want to do is create a third repository, and merge the contents of the other two repositories into this new one. What I hope to achieve by this is to have one repository that has all the data from the other two repos but only have one copy of it. This way, we hope that git will tell us what is different between the two repos and merge these changes together into one.

Hopefully this is easy enough to understand.

I have tried creating a new git repo, doing a git clone of one server, creating a new branch and doing a git clone of the other repo then asking git to merge them. I've also tried the subtree merge and neither of these produced what I wanted.

The first example, simply said remove all files and add a bunch of new files. This isnt what we wanted, we wanted a single git repository holding a single copy of configuration produced as a result of merging the two remote repos together.

If anyone can help with this problem it would be much appreciated.

By the way, both repos data consists of the same files with the same file names but slightly different content.

like image 588
Matt Faraday Avatar asked Jun 01 '11 15:06

Matt Faraday


People also ask

Can I merge two git repositories?

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 we merge branches from different git repositories?

Added the source repository as a remote, by hitting the Settings button and adding the source repository. Branches from both repository now show in the branch list. I used the merge tool to merge a branch from the source repository to my new destination repository's branch. Commit the changes in my branch.


1 Answers

If we assume the repos are called A and B, you could start off by cloning either A or B into C:

$ git clone git://path/to/A.git

Now C is identical to A (and A is the origin of C). We can now add B as a remote to C and create a tracking branch masterB that tracks the master branch of B:

$ git remote add B git://another/path/to/B.git
$ git fetch B
$ git checkout --track masterB B/master

Now the master branch of C is the same as the master branch of A and masterB branch is the same as master on B. We can now merge masterB into master:

$ git checkout master
$ git merge masterB
$ .. solve conflicts

Unless you need to keep track of the original repositories, we can clean up with:

$ git remote rm origin
$ git remote rm B
$ git branch -d masterB
like image 157
ralphtheninja Avatar answered Oct 27 '22 07:10

ralphtheninja