Git merge two similar repositories into one (both added conflicts)
My situation looks like that:
I have two repositories with more or less the same code (let say repo A and B). I would like to pull repo B from repo A.
On repo A I execute following commands:
I get tons of
both added conflicts because both repositories consist of many identical files which were added in the first commit and left unchanged.
How to merge this repos without spending whole day on checking if “both added” files have different content or identical?
I would like to merge identical files automatically and leave only files which has different content for manual merge.
3 Solutions collect form web for “Git merge two similar repositories into one (both added conflicts)”
Maybe someone can offer easier way to do this but the only option I found is:
- add repoB as new branch to repoA
git remote add repoB path/to/repoB git checkout -b branchB repoB/master
- rebase repoA to the initial commit (repo A and B has similar initial commit)
git rebase -i --root
- merge with branchB on its initial commit
git merge -Xours SHA_OF_BRANCHB_INITIAL_COMMIT
- rebase repoA to current state
git rebase --continue
- merge repos as normal
git merge branchB
Thanks to this solution all common files are merged in the first two commits so I can safely use
-Xtheirs in this merge
You could check if a
git merge strategy would allow you to resolve those conflicts automatically:
git merge -Xours branchB
Note that would apply to all files with conflicts, not just the ones “both added”.
When merge has to work with a bad base, it gets inaccurate change hunks — in this case, with the history showing no common ancestor at all, merge’s autoresolve starts with every file showing as a single added change hunk.
One thing you could try is grafting the roots together:
echo $(git rev-list --max-parents=0 other) \
$(git rev-list --max-parents=0 HEAD) > .git/info/grafts
If that doesn’t get good results, just abort the merge and yank the
A general-purpose fallback is
git merge --no-commit --strategy ours
git diff ..other | git apply
git add --patch .