mdcq
mdcq

Reputation: 2026

Interleaving two Git repositories

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

Upvotes: 0

Views: 110

Answers (1)

eftshift0
eftshift0

Reputation: 30242

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

Upvotes: 1

Related Questions