Lars
Lars

Reputation: 3583

Git Merge 2 repositories with shared origin. Recombine into monorepo

Context:

  1. There was a mono repo
  2. The repo is 'split', new origins for both;
  3. The separate repo's clean up/remove things from the other repo.
  4. A bunch of new commits happen in both repo's.
  5. we now want to combine those back into a single mono repo.

A simple merge doesn't go well. Since their origin is shared, and both deleted files from each other. The simple merge would remove files that where still used in the other repo.
Reverting just the 'delete commits' is hard given that they're spread out over a longer period.
We could simply forgo the history of one repo, but prefer to keep it. It seems to me that if we could treat the merge as 'separate' parent and merge with --allow-unrelated-histories it would probably be fine.

Is there a way to only keep the history of the existing files, and leave out the deleting commits?

## Simplified version of actual events:
git init my-repo
cd my-repo

## add 2 dirs with their own stuff
mkdir repo1
touch repo1/file1.txt
mkdir repo2
touch repo2/file2.txt

## commit in mono repo
git add .
git commit -m"mono repo commit"

## decided to not be a mono repo
## for a while they commit to sperate origins.
## both decide to remove the other repo's stuff
git checkout -b repo1 
rm -rf repo2
git add .
git commit -m"single-repo1 commit"

git checkout master
git checkout -b repo2 
rm -rf repo1
git add .
git commit -m"single-repo2 commit"

## now they decide that we will be a mono repo again.
## we need to merge  repo1 into repo2.
## trying to find the delete commit proves to be tricky.
## basically i would prefer to pretend that repo2 doesn't share the same origin.
## so that merging would result in: 
#  - repo1/file1.txt
#  - repo2/file2.txt

Upvotes: 1

Views: 191

Answers (1)

Wes Hardaker
Wes Hardaker

Reputation: 22262

So I have done what you're talking about, and your own answer was one option: use --allow-unrelated-histories to merge completely independent repos together. It's actually fun to even try: create two totally different repos like you did and merge them together. As a git training exercise for a group of 10 people I had them all merge their repos together after they created independent ones using a filename based on their account name. It was fun to watch :-)

BUT, if they have a similar code base with a ton of changes you will have a huge number of conflicts most likely. There is no easy solution around this. git will help you as much as possible, but it won't ever be pretty unless they were mostly independent changes (unlikely).

The other option is to recreate the original mono-repository and try to add the other two diverging repositories as branches off of the recreated mono one, and then merge them together by either a merge or a rebase back on to the mono-repository's main branch. This will functionally do the same thing but will more clearly show the real history. But the pain will still be there with respect to the merges.

Upvotes: 1

Related Questions