将提交推送到其他 Git 存储库 - Amazon CodeCommit
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

将提交推送到其他 Git 存储库

您可以将本地存储库配置为将更改推送到两个远程存储库。例如,您可能想要在尝试使用 Amazon CodeCommit 时继续使用现有的 Git 存储库解决方案。按照以下基本步骤将本地存储库中的更改推送到 CodeCommit 单独的 Git 存储库。

提示

如果您没有 Git 存储库,则可以在除之外的服务上创建一个空仓库, CodeCommit然后将仓库迁移到该存储 CodeCommit 库。您应按照迁移到 CodeCommit 中的步骤进行操作。

  1. 在命令提示符或终端中,切换到本地存储库目录,然后运行 git remote -v 命令。您应该可以看到类似于如下所示的输出内容:

    对于 HTTPS:

    origin https://git-codecommit.us-east-2.amazonaws.com/v1/repos/MyDemoRepo (fetch) origin https://git-codecommit.us-east-2.amazonaws.com/v1/repos/MyDemoRepo (push)

    对于 SSH:

    origin ssh://git-codecommit.us-east-2.amazonaws.com/v1/repos/MyDemoRepo (fetch) origin ssh://git-codecommit.us-east-2.amazonaws.com/v1/repos/MyDemoRepo (push)
  2. 运行git remote set-url --add --push origin git-repository-name命令,其中git-repository-name是你要托管代码的 Git 存储库的 URL 和名称。这会将 origin 的推送目标更改为该 Git 存储库。

    注意

    git remote set-url --add --push 会覆盖推送的默认 URL,因此您必须运行该命令两次,如后面的步骤所示。

    例如,下面的命令将原始推送更改为 some-URL/MyDestinationRepo:

    git remote set-url --add --push origin some-URL/MyDestinationRepo

    该命令不返回任何内容。

    提示

    如果要推送到需要凭证的 Git 存储库,请确保在凭证辅助程序或 some-URL 字符串配置中配置这些凭证。否则,到该存储库的推送操作将失败。

  3. 再次运行 git remote -v 命令,它应产生类似以下内容的输出:

    对于 HTTPS:

    origin https://git-codecommit.us-east-2.amazonaws.com/v1/repos/MyDemoRepo (fetch) origin some-URL/MyDestinationRepo (push)

    对于 SSH:

    origin ssh://git-codecommit.us-east-2.amazonaws.com/v1/repos/MyDemoRepo (fetch) origin some-URL/MyDestinationRepo (push)
  4. 现在添加 CodeCommit 存储库。再次运行 git remote set-url --add --push origin,这次请指定您的 CodeCommit 存储库的 URL 和存储库名称。

    例如,以下命令将 O rigin 的推送添加到 https://git-codecommit.us-east-2.amazonaws.com/v1/repos/MyDemoRepo:

    对于 HTTPS:

    git remote set-url --add --push origin https://git-codecommit.us-east-2.amazonaws.com/v1/repos/MyDemoRepo

    对于 SSH:

    git remote set-url --add --push origin ssh://git-codecommit.us-east-2.amazonaws.com/v1/repos/MyDemoRepo

    该命令不返回任何内容。

  5. 再次运行 git remote -v 命令,它应产生类似以下内容的输出:

    对于 HTTPS:

    origin https://git-codecommit.us-east-2.amazonaws.com/v1/repos/MyDemoRepo (fetch) origin some-URL/MyDestinationRepo (push) origin https://git-codecommit.us-east-2.amazonaws.com/v1/repos/MyDemoRepo (push)

    对于 SSH:

    origin ssh://git-codecommit.us-east-2.amazonaws.com/v1/repos/MyDemoRepo (fetch) origin some-URL/MyDestinationRepo (push) origin ssh://git-codecommit.us-east-2.amazonaws.com/v1/repos/MyDemoRepo (push)

    你现在有两个 Git 存储库作为推送的目的地,但你的推送会先到某个网址/MyDestinationRepo 。如果到该存储库的推送失败,则您的提交不会被推送到任何一个存储库。

    提示

    如果另一个存储库需要您要手动输入的凭据,请考虑更改推送顺序,以便 CodeCommit 先推送到。运行 git remote set-url --delete 删除首先推送到的存储库,然后运行 git remote set-url --add 再次添加它,使其成为列表中的第二个推送目标。

    有关更多选项,请参阅 Git 文档。

  6. 要验证您是否正在推送到两个远程存储库,请使用文本编辑器在本地存储库中创建下面的文本文件:

    bees.txt ------- Bees are flying insects closely related to wasps and ants, and are known for their role in pollination and for producing honey and beeswax.
  7. 运行 git add 将更改暂存在本地存储库中:

    git add bees.txt
  8. 运行 git commit 提交本地存储库中的更改:

    git commit -m "Added bees.txt"
  9. 要将本地存储库的提交推送到远程存储库,请运行 git push -u remote-name branch-name,其中 remote-name 是本地存储库用于远程存储库的别名,branch-name 是要推送到存储库的分支的名称。

    提示

    您只需在第一次推送时使用 -u 选项。然后将设置上游跟踪信息。

    例如,运行 git push -u origin main 会显示推送到两个远程存储库中的预期分支,并产生类似以下内容的输出:

    对于 HTTPS:

    Counting objects: 5, done. Delta compression using up to 4 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 5.61 KiB | 0 bytes/s, done. Total 3 (delta 1), reused 0 (delta 0) To some-URL/MyDestinationRepo a5ba4ed..250f6c3 main -> main Counting objects: 5, done. Delta compression using up to 4 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 5.61 KiB | 0 bytes/s, done. Total 3 (delta 1), reused 0 (delta 0) remote: To https://git-codecommit.us-east-2.amazonaws.com/v1/repos/MyDemoRepo a5ba4ed..250f6c3 main -> main

    对于 SSH:

    Counting objects: 5, done. Delta compression using up to 4 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 5.61 KiB | 0 bytes/s, done. Total 3 (delta 1), reused 0 (delta 0) To some-URL/MyDestinationRepo a5ba4ed..250f6c3 main -> main Counting objects: 5, done. Delta compression using up to 4 threads. Compressing objects: 100% (3/3), done. Writing objects: 100% (3/3), 5.61 KiB | 0 bytes/s, done. Total 3 (delta 1), reused 0 (delta 0) remote: To ssh://git-codecommit.us-east-2.amazonaws.com/v1/repos/MyDemoRepo a5ba4ed..250f6c3 main -> main

有关更多选项,请参阅 Git 文档。