0

How can I join two Git repositories by interleaving their commit history according to commit / author date?

We can assume both repositories contain separate files. However, folder names might be identical and should be deep merged.

For example, repository a looks like:

a1.txt
src/a2.txt
A1---A2---A3    <--- a/main

While repository b looks like:

b1.txt
src/b2.txt
B1---B2---B3    <--- b/main

Assuming the commit / author date order is A1, B1, B2, A2, A3, B3, the resulting repository c should look like:

a1.txt
b1.txt
src/a2.txt
src/b2.txt
A1---B1---B2---A2---A3---B3    <--- c/main

We can also assume that no two commit / author dates will be identical such that the order is well defined.


This is unlike Merge two Git repositories without breaking file history which keeps two branches and creates a merge commit.

This may be a simpler case of How to merge several Git repos into one and interleave histories

mdcq
  • 1,593
  • 13
  • 30

1 Answers1

1

Given that the 2 histories relate to different things, you might be able to pull it off if you are able to list all revisions by their commit (or author) date..... I do not think that if you ask git log to show you revisions from both branches, it will intertwine them.... so you might have to sort them first before.... so, setup a repository so that you have access to both branches. Create a new empty branch (git checkout --orphan and clean the content), then this should work:

git log --pretty="%at %h" branch1 branch2 | sort -n | while read date revision; do git cherry-pick $revision; done

Or something along those lines

Taking @LeGEC's comment in, then it could be adjusted to this:

git log --reverse --date-order --pretty="%h" branch1 branch2 | while read revision; do git cherry-pick $revision; done
eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • 1
    sorting by author date outside of git (`.. | sort -n | ...`) can move commits around and break the parent - child order. I would suggest `git log --date-order ...`, which *preserves parent - child order*, and reorders by date when it can. – LeGEC Nov 14 '22 at 13:33
  • Good catch! Perhaps by commit date? – eftshift0 Nov 14 '22 at 14:17
  • 2
    just use `--date-order` : `git log --date-order branch1 branch2` will preserve parent-child order, and will interleave `branch1` and `branch2` history (since these two histories aren't related, git can choose how to order the commits one with respect to another) – LeGEC Nov 14 '22 at 14:48