Git 从两个不同仓库的两个不同分支拉取到一个本地分支

Git pull from two different branches in two different repositories into one local branch

我在本地克隆了一个 Git 存储库——称之为 A。这个存储库 A 有一个名为 aaa 的分支。我创建了一个新的本地分支--bbb。然后我创建了一个新的 GitHub 存储库(空)——称之为 B——并将 bbb 推送到 B。接下来,我将本地存储库的来源更改为指向 B。然后我还添加了一个名为“上游”到我的本地存储库,它指向原始源 A。我将这个上游遥控器的“推送”url 设置为伪造的(“不推送”),因为我不想不小心push 从我的本地仓库返回到 A。我只想从 A pull 到我的本地仓库。

在过渡期间,一些开发人员将对 A/aaa 进行更改。其他人正在提交对 B/bbb.

的更改

我希望能够 更改从A/aaa 拉到我本地的bbb 分支然后push 那些到 B/bbb 这样 B 上的代码将包含团队提交给 A 的所有更改以及团队提交给 B 的所有更改。我还想将 B/bbb 中的更改拉入我本地的 bbb 分支。本质上,我想暂时使用我的本地存储库作为两者之间的桥梁,直到我们在将来某个时候停止使用 A。

我 运行 可以使用哪些 git 命令来完成此操作?(每个命令的作用是什么,所以我不只是盲目的复制命令)

到目前为止,我所做的一切都没有奏效,我想我一定是在理解中遗漏了一些关键要素。

I want to be able to pull changes from A/aaa into my local bbb branch and then push those to B/bbb so that the code on B will have all of the changes from the team committing to A as well as all of the change from the team committing to B

您可以确定:

  • bbb的上游分支是A/aaa
  • 推送分支是 B/bbb(如“How to set up branches with different pull/push upstream”中所做的那样)
cd /path/to/local
git switch bbb
git branch -u A/aaa bbb
git push -u B bbb

最终,我删除了名为“upstream”的遥控器,并 re-added 使用不同的名称将其删除,只是为了从头开始重复这些步骤。这些是我使用的有效命令:

git remote add A_upstream <<url_to_git_repo>>
git remote remove upstream
git remote set-url --push A_upstream no-pushing
git fetch --all
git checkout bbb
git merge A_upstream/aaa
git pull origin
git push origin

第一个命令为 A 添加了一个新的遥控器。下一个命令删除了之前的遥控器,然后在下一步中我再次将新遥控器的推送 URL 设置为虚假的东西,因为我只想从 A 拉取。接下来,fetch --all 从 A 和 B 获取所有内容,然后我使用下一个命令将 aaa 分支的更改合并到我的本地 bbb 分支。来自 origin 的 pull 确保我与 bbb 中的提交保持同步,然后来自 push 的 origin 确保我从 aaa 获得的更改转到了 B.