【问题标题】:Synching two ancestraly related Git repositories ignoring some commits同步两个与祖先相关的 Git 存储库,忽略一些提交
【发布时间】:2017-12-13 22:32:51
【问题描述】:

我有一些我想要的 git 存储库:

  1. 端口到新的 git 服务器。
  2. 向它们添加子模块。

这本身就很简单,问题是我想循序渐进地做到这一点。也就是说,我希望用户继续克隆/拉取/推送到旧存储库,同时将这些提交移植到新存储库。最终,“旧”服务器将被删除,用户将开始使用带有子模块的新服务器。我有一个詹金斯服务器,它将负责每晚将所有这些存储库从旧服务器移植到新服务器。 同样,这将是非常简单的,除了我希望新的仓库有一些旧仓库没有的子模块。所以情况会是这样的:

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

如您所见,我想将所有提交从旧仓库移植到新仓库,但不会丢失任何信息(我不想只是从旧仓库复制源代码并在新仓库中覆盖它们,因为这会丢失 cmets 和中间提交,因为该移植操作将每天执行一次)。

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

谢谢!

编辑

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

#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 的内容中?

【问题讨论】:

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


    【解决方案1】:

    我希望我理解你的正确。我想最简单的处理方法是将另一个/新的远程分支/分支添加到您的新服务器并获取并提取更改。因此,您将拥有所有的提交和历史记录。 更多信息可以阅读https://git-scm.com/book/en/v2/Git-Branching-Remote-Branches

    如果我错了,请纠正我。

    【讨论】:

    • 在从旧遥控器中拉出所有分支并将它们推送到其中时,我遇到了问题。如果旧遥控器上出现新分支怎么办?您实际上会推荐什么样的命令序列?
    • 1.使用 'git fetch --all' 和 'git pull' 从旧的远程分支获取所有更改并将其拉到本地存储库 2. 添加新的远程 url 并将本地分支推送到新的远程使用 'git remote add newRemote URL ' 并使用 'git remote -v' 您可以检查新远程的 URL 是否正确。然后将您的本地分支推送到您的新远程
    猜你喜欢
    • 2022-01-21
    • 2022-11-17
    • 1970-01-01
    • 2010-11-11
    • 2022-06-16
    • 2011-06-19
    • 2020-12-06
    • 2023-04-05
    • 2012-04-23
    相关资源
    最近更新 更多