合并两个Git回购并保留历史记录 [英] Merge two Git repos and keep the history

查看:182
本文介绍了合并两个Git回购并保留历史记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想延续另一个问题:



这是重新绑定后的结果。顶级回购的时间是回购期。原始日期已丢失!






  • 我想保留原始时间+合并历史记录。

    更新 - 答案

    答案这对我来说最合适,正在与移植点合作。但其他答案在其他用例中也非常有用。我已经在github上添加了我的结果,所以每个人都可以评估。



    答案1:对我来说最好的工作正确的工作答案给我。





    答案2 LeGEC中的替换选项对于某些使用情况也有很好的效果。一个反常现象留给我:



    答案3:值得添加来自'VonC'的答案。我无法得到'--preserve-merges working'这个选项。这可能适用于其他场景,但我没有测试这个furtner。正如你发现的那样,<$ c $

    解决方案 c> rebase 不是您想要用来将历史记录拼接在一起的命令(因为它实际上会重写历史记录)。早期的Git有一个特别的功能(黑客),专门设计用于你想要做的事情:接枝点。更好的是,从1.6.5开始,您可以使用 git replace --graft 改为:

      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 返回来自 RepoA 的初始提交。)



    从技术上讲,如果您实际上没有任何有价值的东西,您可以跳过第一个替换> master ,产生RepoB + RepoA的历史记录。



    这些命令在 refs / replace / * 可以推送和拉出来与其他人分享修改过的历史记录。或者,如果您不关心保留RepoA / RepoB的SHA,可以通过运行 git filter-branch --all 来替换永久 c>生成所需血统的真实提交集。

    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 needed a rebase to do this successfully. The master is correct, but I also want to keep the merge history. Is this possible?

    I have 2 repositories:

    This is the result after rebasing. The times of the top repo are the rebase-time. The original date is lost!

    This is how I did it:

    # Assume the current directory is where we want the new repository to be created
    # Create the new repository
    git init
    
    # Before we do a merge, we have to have an initial commit, so we'll make a dummy commit
    dir > Read.md
    git add .
    git commit -m "initial commit"
    
    # Add a remote for and fetch the old RepoA
    git remote add -f RepoA https://github.com/DimitriDewaele/RepoA
    
    # Do the same thing for RepoB
    git remote add -f RepoB https://github.com/DimitriDewaele/RepoB
    
    # Rebase the working branch (master) on top of repoB
    git rebase RepoB/master
    
    # Rebase the working branch (master with RepoB) on top op repoA
    git rebase RepoA/master
    

    Is it possible to have something like this? (painted solution!!!)

    I would like to keep the original time + the merge history.

    UPDATE - ANSWER

    The answer that worked best for me, was working with graft points. But other answers are also very usable in other use cases. I have added my results on github, so everybody can evaluate.

    Answer 1: Best working in my case The 'graft' did reveal the correct working answer for me.

    GitHub: RepoGraft

    Answer 2 the "replace" option from "LeGEC" also gives good results for some use cases. One anomaly stayed for me:

    GitHub: RepoHistory

    Answer 3: Worth adding The answer from 'VonC'. I could not get the option '--preserve-merges working' in my case. This might work in other scenario's, but I did not test this furtner.

    解决方案

    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.

    这篇关于合并两个Git回购并保留历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆