Combine Git repositories with unrelated histories

By on

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.

As an example, there are two repositories that both have a single root commit. Running git log produces the log for the first repository, as that repository’s directory is the current directory:

git log
commit 733ed008d41505623d6f3b5dbee7d1841a959c34
Author: Alice <alice@example.com>
Date:   Sun Aug 8 20:12:38 2021 +0200

    Add one.txt

To get the logs for the second repository, pass a path to that repository’s .git directory to the git log command via the --git-dir command line option:

git --git-dir=../two/.git log
commit 43d2970b4dafa21477e1a5a86cad45bc5e798696
Author: Bob <bob@example.com>
Date:   Sun Aug 8 20:12:44 2021 +0200

    Add two.txt

To combine the two repositories, first add the second repository as a remote to the first. Then, run git fetch to fetch its branch information:

git remote add two ../two
git fetch two

Then merge, with the remote set up, merge the second repository’s history into the first by using the --allow-unrelated-histories flag:

git merge two/main --allow-unrelated-histories
Merge made by the 'recursive' strategy.
 two.txt | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 two.txt

Unlike extracting part of a repository—which rewrites history by definition—combining repositories is done by merging their two histories. This means the commits from the second repository are appended to the ones from the first, retaining history:

git log
commit 0bd5727a597077b5a5db9590b3f6aaf70eb10b60
Merge: 733ed00 43d2970
Author: Alice <alice@example.com>
Date:   Sun Aug 8 20:13:01 2021 +0200

    Merge remote-tracking branch 'two/main' into main

commit 43d2970b4dafa21477e1a5a86cad45bc5e798696
Author: Bob <bob@example.com>
Date:   Sun Aug 8 20:12:44 2021 +0200

    Add two.txt

commit 733ed008d41505623d6f3b5dbee7d1841a959c34
Author: Alice <alice@example.com>
Date:   Sun Aug 8 20:12:38 2021 +0200

    Add one.txt

References