15

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.

Zombies
  • 25,039
  • 43
  • 140
  • 225
Matt Faraday
  • 151
  • 1
  • 3
  • 1
    Sounds like you need to create an artificial common ancestor, rebase/graft the history of both repos onto it, then merge the two resulting branches. – Cascabel Jun 01 '11 at 16:10
  • 1
    possible duplicate of [How to merge two remote repository.](http://stackoverflow.com/questions/1767980/how-to-merge-two-remote-repository) – karlphillip Jun 01 '11 at 16:44

3 Answers3

21

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
rtn
  • 127,556
  • 20
  • 111
  • 121
  • 1
    +1 worked beautifully. one thing though `git checkout --track...` failed with a fatal: missing branch error. did `git checkout -b masterB B/master` instead which seems to work fine. – arunkumar Apr 01 '13 at 13:06
0

Merging 2 similar repos into one is pretty straight forward.

  1. create a bare repo

  2. git add remote repo1 into bare repo

  3. git merge master of repo1 into the bare repo

  4. git add remote repo2 into bare repo

  5. git merge master of repo2 into the bare repo

  6. git rm remote of repo1 and repo2

  7. push the bare repo onto server.

0

You will have 2 different branches coming from both of the repos. Let's say they are

repo1/master
repo2/master

I would at this point just merge them and solve the conflicts so that it represents one of the repos. I would then do a merge again and this time pick the other repo as the master in case of any conflicts. You will now have:

common1
common2

branches. Merge common2 into common1 with the "ours" strategy. Now do the same with the other one. This will capture a common point in history and keep the config diffs from giving you issues on subsequent merges. This is essentially having one see the other as a development branch.

hope this helps.

Adam Dymitruk
  • 124,556
  • 26
  • 146
  • 141