0

The short story is that I have two git repositories, one a fork of the other, where the fork's history has been rewritten. I need to merge the fork back, but that seems to be tricky.

As an example, suppose I have a repository named foo. The commit history looks like the following:

Commit 3: Typo fixes
Commit 2: Some feature
Commit 1: Quick bug fixes
Commit 0: Initial commit

foo is then forked as bar, and then a process is used to rewrite / remove history, so that bar's history looks like the following:

Commit C: Type fixes
Commit B: Some feature
Commit A: Quick bug fixes

At this point, commit IDs have changed, and Commit 0 no longer exists in the fork. Everything else is the same.

Now, time has passed. Both repositories have had work done to them by many people. So we might have something like:

foo                                   bar
----------------------------------|-------------------------------------
Commit 5: Fix the major bug fix   |
Commit 4: Major bug fix           |   Commit D: Project specific fixes
Commit 3: Typo fixes              |   Commit C: Type fixes
Commit 2: Some feature            |   Commit B: Some feature
Commit 1: Quick bug fixes         |   Commit A: Quick bug fixes
Commit 0: Initial commit          |

Is there any way I can merge back bar in to foo? Technically 1 == A, 2 == B, & 3 == C, but I'm uncertain how that would be reconciled.

  • You might be looking for a merge with `--allow-unrelated-histories` option, as suggested in [this answer](https://stackoverflow.com/a/10548919/7598462). – kowsky May 16 '19 at 08:32

2 Answers2

1

Have a single repo have those two repos as remotes, fetch both, then checkout one of the branches and cherry-pick what you need from the other. Then you can push to the remote this branch belongs to. Same can be done with the branch from the other repo.

eftshift0
  • 26,375
  • 3
  • 36
  • 60
0

I found a solution using a different means of getting there. Here's roughly what I've found.

  1. Add the forked repo as an upstream to your current repo.
  2. Create a branch to work in.
  3. Find the common commits you can use from both repos that gives you a good merge point.
  4. From the forked repo, find the next commit as well.
  5. Use git replace --graft

So from above, the common commits would be Commit 2 / Commit B, and the next commit would be Commit C. You'd then do:

git replace --graft C 2 B
git merge upstream/master