threads / discuss / 52472

Merging two Git-Repositories

Subject: Merging two Git-Repositories

## tl;dr

4 messages between Dec 17, 2019 and Dec 17, 2019.

replies: 3people: 4as markdown or json

Steve Keller· Dec 17, 2019, 09:28 UTC · lore

Is it possible to merge one Git-Repository into another? I have developed two independent repositories A and B, and now I find that B should be part of A in new sub-directory, e.g. A/B. I want to move B into A while keeping the whole development history of B.

How can I do that?
Steve
Roger Gammans· Dec 17, 2019, 10:02 UTC · re: Steve Keller · lore

Re: Merging two Git-Repositories

On Tue, 2019-12-17 at 10:28 +0100, Steve Keller wrote:
Show 6 quoted lines
> Is it possible to merge one Git-Repository into another?  I have
> developed two independent repositories A and B, and now I find that
> B should be part of A in new sub-directory, e.g. A/B.  I want to
> move B into A while keeping the whole development history of B.
> 
> How can I do that?

It is possible bit isn't straightforward, at least not if you want to interleave the commits.

If you don't want to interleave the commits, just rename everything (*) in B to B/*' and merge the repo with A's repo. Then you will have a the structure you want going forward, all the history together; but if you check out a historic commit you will have to decide to check out on the 'A' or 'B' branches.

If you do want to interleave the comits the best found was ( https://metacpan.org/pod/distribution/Git-FastExport/script/git-stitch-repo to merge the repos. (from https://github.com/book/Git-FastExport/ )

I've got some updates ( https://github.com/rgammans/Git-FastExport/tree/hintfiles ) to it when I did some work on this a couple of years ago but the it looks like the upstream has improved a little since then. My changes allow you to try to give it hints in case in make mistakes, on choosing the correct parent for a commit. The whole system worked pretty well except where there were a lot a diverging/remerging branches.

My process was to write a shell script to actually drive the process, and tweak the script to improve the results iteratively.

Hope this helps a little.
-- 
Roger Gammans <rgammans@gammascience.co.uk>
Jared Tobin· Dec 17, 2019, 11:03 UTC · re: Steve Keller · lore

Re: Merging two Git-Repositories

On Tue, Dec 17, 2019 at 10:28:36AM +0100, Steve Keller wrote:
> Is it possible to merge one Git-Repository into another?  I have
> developed two independent repositories A and B, and now I find that B
> should be part of A in new sub-directory, e.g. A/B.  I want to move B
> into A while keeping the whole development history of B.

You can accomplish this with git-subtree(1), for which IMO the intuition of "exotic merge commit" more or less suffices.

An example (from A):
  git remote add B B_REMOTE_ADDRESS
  git subtree add --prefix B B master
-- jared
Stephan Beyer· Dec 17, 2019, 20:13 UTC · re: Steve Keller · lore

Re: Merging two Git-Repositories

On 12/17/19 10:28 AM, Steve Keller wrote:
> Is it possible to merge one Git-Repository into another?  I have
> developed two independent repositories A and B, and now I find that
> B should be part of A in new sub-directory, e.g. A/B.  I want to
> move B into A while keeping the whole development history of B.

If I get you right, you want a simple merge of two branches with no common ancestors. Here's a line-by-line sh example with project A in directory A and project B in directory B, and B will be merged into a subdirectory "project-b" in A:

  # change to B's directory
  cd B
  # create the subdirectory where B's files will be in A
  mkdir project-b
  # move all files into project-b
  git mv -k * project-b
  # probably check if this worked for you
  ls
  git status
  # commit
  git commit -m "Move all files into subdirectory for inclusion into A"
  # change to A's directory
  cd ../A
  # fetch and merge B into A
  git pull --no-rebase --allow-unrelated-histories ../B
  # ... change commit message to something more meaningful ...
That's it.

If B is public you might want to replace its last commit by some commit that adds a note that further development is going on in A.

Stephan

← back to recent threads