When you want to rebase a branch keeping merge commits you pass the --preserve-merges
flag. When you are merging unrelated history in git you need to pass the --allow-unrelated-histories
flag.
If you are doing git rebase --preserve-merges
when an existing merge comes from an unrelated history, it fails:
fatal: refusing to merge unrelated histories
If you try git rebase --preserve-merges --allow-unrelated-histories
it fails with:
error: unknown option 'allow-unrelated-histories'
Is there some other way to tell rebase to allow the merge?
Edit: here is a minimal reproduction: https://github.com/vossad01/rebase-unrelated-merge-reproduction
To reproduce checkout master
then execute:
git rebase --preserve-merges --onto origin/a-prime HEAD~2
To combine two separate Git repositories into one, add the repository to merge in as a remote to the repository to merge into. Then, combine their histories by merging while using the --allow-unrelated-histories command line option.
In fact, all you need to do to merge unrelated branches is to use the flag --allow-unrelated-histories . This tells Git to combine all the files and commits of both unrelated branches into one branch, as long as there are no file conflicts.
The “fatal: refusing to merge unrelated histories” Git error occurs when two unrelated projects are merged (i.e., projects that are not aware of each other's existence and have mismatching commit histories).
Rebasing is not going to magically remove all merge conflicts. In fact, you may encounter conflicts while rebasing. Sometimes, you will have to repeatedly resolve the same conflict while rebasing. However, merge conflicts happen because multiple changes happen to the same chunk of code simultaneously.
When git rebase
fails on the merge it does not abort the rebase, so you have the opportunity to manually intervene.
If you are willing to to resolve this by hand, you can complete the merge as follows:
git merge --allow-unrelated ORIGINAL_BRANCH_THAT_WAS_MERGED --no-commit
git commit -C ORIGINAL_MERGE_COMMIT
git rebase --continue
Ideally, there would be a way for Git to handle this without manual intervention.
The brute-force method is to force a common root -- since you're trying to rebase roots, with no content history, make a nonce empty commit and tell git that's the parent of the histories you're merging:
git rev-list --all --max-parents=0 \
| awk '{print $0,empty}' empty=`:|git mktree|xargs git commit-tree` \
> .git/info/grafts
git rebase here
rm .git/info/grafts
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With