DEV Community

Jeff Kreeftmeijer
Jeff Kreeftmeijer

Posted on • Originally published at jeffkreeftmeijer.com

Combine Git repositories with unrelated histories

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
Enter fullscreen mode Exit fullscreen mode
commit 733ed008d41505623d6f3b5dbee7d1841a959c34
Author: Alice <alice@example.com>
Date:   Sun Aug 8 20:12:38 2021 +0200

    Add one.txt
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
commit 43d2970b4dafa21477e1a5a86cad45bc5e798696
Author: Bob <bob@example.com>
Date:   Sun Aug 8 20:12:44 2021 +0200

    Add two.txt
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
Merge made by the 'recursive' strategy.
 two.txt | 0
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 two.txt
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode

References

  • https://git-scm.com/docs/git-merge:

    By default, git merge command refuses to merge histories that do not share a common ancestor. [The --allow-unrelated-histories​] option can be used to override this safety when merging histories of two projects that started their lives independently.

Top comments (0)