简体   繁体   中英

How to merge GIT repo's with common code BUT NO common ancestry

How can I merge two git repos with common code but no common ancestry?

Here is what happened:

  1. repoRoot was created
  2. repoRoot was lost but there was a file only backup
  3. repoNew was created from the same files but with no history
  4. in repoNew all code was moved to a new subFolder
  5. repoRoot was found again

What we are desiring now is to have ONE repo with

  1. First the history of repoRoot
  2. Followed by the history of repoNew

Preferably with all branches that existed in both intact

Here are two sample repos

https://github.com/patrickwolf/sandbox_repoRoot
https://github.com/patrickwolf/sandbox_repoNew

Thanks!

The easiest would be a graft/replace. That will allow you to connect both histories without much trouble, to the best of my knowledge. Check https://stackoverflow.com/a/6802005/2437508

But if you don't mind rewriting the later part of history, you could rewrite the history of the second repo on top of the first.... you just need to find the two revisions that share the same content so that you work from there.

Let's assume you have a local repo that can see both repos as remotes, old and new .

The first revision in branches of new is branch new/first , which matches in content with old/main . If we wanted to rewrite new/main on top of old/main :

git rebase --rebase-merges --onto old/main new/first new/main

That should do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM