jmarceli
jmarceli

Reputation: 20162

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:

git remote add repoB path/to/repoB
git checkout -b branchB repoB/master
git merge branchB

Problem:
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.

Upvotes: 2

Views: 1838

Answers (3)

jmarceli
jmarceli

Reputation: 20162

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 -Xours or -Xtheirs in this merge

Upvotes: 3

jthill
jthill

Reputation: 60275

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
git mergeother

If that doesn't get good results, just abort the merge and yank the .git/info/grafts file.

A general-purpose fallback is

git merge --no-commit --strategy oursother
git diff ..other | git apply
git add --patch .

Upvotes: 1

VonC
VonC

Reputation: 1323953

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".

Upvotes: 1

Related Questions