开发者

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 u开发者_开发知识库s 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.


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


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜