同步两个与Ancestraly相关的Git存储库,忽略了一些提交

时间:2017-07-10 09:02:52

标签: git git-branch git-merge git-submodules

我有许多git存储库,我想:

  1. 移植到新的git服务器。
  2. 向其添加子模块。
  3. 这本身就是直接的,事情是我想逐步做到这一点。也就是说,我希望用户继续克隆/拉/推到旧的回购,同时将这些提交移植到新的回购。最终将删除“旧”服务器,用户将开始使用带有子模块的新服务器。我有一个jenkins服务器,它将负责将所有这些repos从旧服务器移植到每晚的新服务器。 再说一次,这将是非常直接的,除了我希望新的repos有一些旧的repos没有的子模块。所以情况就像:

    Old Repo        New Repo
        |               |
     CommitA-------->CommitA
        |               |
        |            CommitB (Add submodules)
        |               |
     CommitC-------->CommitC
        |               |
     CommitD-------->CommitD
        |               |
        .               .
        .               .
        .               .
    

    因此,您可以看到我想将旧回购中的所有提交移植到新的回送中,而不会丢失任何信息(我不想只从旧回复中复制来源并在新回购中覆盖它们,因为这会导致注释和中间提交失效,因为移植操作将每天执行一次。)

    有人可以给我一些关于如何实现这一点的提示,以及我如何在“自动化”模式下执行它(不仅仅是樱桃一个接一个地提交)。

    谢谢!

    修改

    所以我一直在尝试,现在我有:

    #Clone "New Repo"
    git clone ssh://<NewRepoServer>/bitbucket_tests bitbucket_tests
    cd bitbucket_tests
    #Add "Old Repo" remote
    git remote add old_repo <OldRepoServer/bitbucket_tests>;
    #update New Repo remote
    git remote update origin;
    #update Old Repo remote
    git remote update old_repo;
    #Loop through all branches, and try to merge them
    for remote_branch in `git branch -r | grep old_repo | grep -v master | grep -v HEAD | awk '{gsub(/^[^\/]+\//,"",$1); print $1}'`; do 
        git branch -f --track $remote_branch
        git checkout $remote_branch
        git pull -s recursive -X patience -X theirs old_repo $remote_branch
        git pull
    done
    git checkout master
    git pull -s recursive -X patience -X theirs old_repo master
    git pull
    
    #Push results to Old Repo
    git push origin refs/remotes/old_repo/*:refs/heads/*;
    

    虽然如果没有对“New Repo”分支进行修改,这样可以正常工作,但是当NewRepo有一些OldRepo中不存在的提交时(例如上图中的CommitB),它会失败:

    git push origin 'refs/remotes/old_repo/*:refs/heads/*'
    To ssh://<NewRepoServer>/bitbucket_tests
     ! [rejected]        old_repo/testBranch -> testBranch (non-fast-forward)
    error: failed to push some refs to                 'ssh://<NewRepoServer>/bitbucket_tests'
    hint: Updates were rejected because a pushed branch tip is behind its remote
    hint: counterpart. Check out this branch and integrate the remote changes
    hint: (e.g. 'git pull ...') before pushing again.
    hint: See the 'Note about fast-forwards' in 'git push --help' for details.
    

    如何在不需要用户干预的情况下自动将这些更改自动合并到OldRepo的内容中?

1 个答案:

答案 0 :(得分:0)

我希望我理解你。我想最简单的方法是将新的远程分支/分支添加到新服务器并获取&amp;拉动变化。所以你将拥有所有的提交和历史记录。 有关详细信息,请阅读https://git-scm.com/book/en/v2/Git-Branching-Remote-Branches

如果我错了,请纠正我。