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:

  • Using the same deploy key for multiple github projects
  • How to manage multiple similar but different projects with git?
  • Did i just destroy my work by incorrectly using git rebase
  • Git unmerged paths
  • Reverting a broken merge and reapplying selected commits with Git
  • Accidentally deleted .git folder - can it be restored?
  • git remote add repoB path/to/repoB
    git checkout -b branchB repoB/master
    git merge branchB

    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.

  • Get the branch name of a tip commit
  • git.exe: error while loading shared libraries: ?: cannot open shared object file: No such file or directory
  • Automatically run Travis-ci with svn repo?
  • How to handle a Git discontinuity?
  • How do i get a list of all the files and their SHA1s from a remote Git repo?
  • How do I remove all traces of Git for my repository?
  • 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 -Xours or -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
    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 .

    Git Baby is a git and github fan, let's start git clone.