# Merging two Git-Repositories

4 messages from 2019-12-17 to 2019-12-17. Participants: Steve Keller, Roger Gammans, Jared Tobin, Stephan Beyer.
Thread: https://gitlist.dev/t/52472

## Steve Keller, 2019-12-17 09:28

Subject: Merging two Git-Repositories
Message-ID: <trinity-c6dbf360-5abe-43df-8428-85514195a7a6-1576574916643@3c-app-gmx-bs62>
URL: https://gitlist.dev/e/trinity-c6dbf360-5abe-43df-8428-85514195a7a6-1576574916643%403c-app-gmx-bs62

```
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, 2019-12-17 10:02

Subject: Re: Merging two Git-Repositories
Message-ID: <fd4bd9c49d7d4daf4275b19a9b5f7478f2e4e145.camel@gammascience.co.uk>
URL: https://gitlist.dev/e/fd4bd9c49d7d4daf4275b19a9b5f7478f2e4e145.camel%40gammascience.co.uk
In-Reply-To: <trinity-c6dbf360-5abe-43df-8428-85514195a7a6-1576574916643@3c-app-gmx-bs62>

```
On Tue, 2019-12-17 at 10:28 +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.
> 
> 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, 2019-12-17 11:03

Subject: Re: Merging two Git-Repositories
Message-ID: <20191217110354.GA82154@castor>
URL: https://gitlist.dev/e/20191217110354.GA82154%40castor
In-Reply-To: <trinity-c6dbf360-5abe-43df-8428-85514195a7a6-1576574916643@3c-app-gmx-bs62>

```
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, 2019-12-17 20:13

Subject: Re: Merging two Git-Repositories
Message-ID: <a909ac32-c94a-fb17-ca4d-13e6ea42cc34@gmx.net>
URL: https://gitlist.dev/e/a909ac32-c94a-fb17-ca4d-13e6ea42cc34%40gmx.net
In-Reply-To: <trinity-c6dbf360-5abe-43df-8428-85514195a7a6-1576574916643@3c-app-gmx-bs62>

```
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

```
