Merge two Git repos and keep the history

后端 未结 3 953
眼角桃花
眼角桃花 2020-12-04 22:41

I want to extend on another question I had: Merge two Git repositories and keep the master history

I have succeeded in merging 2 different repo\'s into one repo. I n

3条回答
  •  渐次进展
    2020-12-04 23:41

    As you've discovered, rebase isn't the command you want to use to stitch histories together (because it actually rewrites history). Early Git had a feature (hack) designed specifically for what you're trying to do: graft points. Even better, since 1.6.5 you can use git replace --graft instead:

    git checkout master
    git replace --graft $(git log RepoB/master --format=%H | tail -1) HEAD
    git replace --graft $(git log RepoA/master --format=%H | tail -1) RepoB/master
    git reset --hard RepoA/master
    

    (git log RepoA/master --format=%H | tail -1 returns the initial commit from RepoA)

    Technically you could skip the first replace if you don't actually have anything of value yet in master, yielding just history with RepoB + RepoA.

    These commands create entries in refs/replace/* that can be pushed and pulled to share your revised history with others. Or, if you don't care about preserving the SHAs of RepoA/RepoB, you can make the replacements permanent by running git filter-branch --all to produce a "real" set of commits of the desired lineage.

提交回复
热议问题