

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

# 将提交推送到其他 Git 存储库
<a name="how-to-mirror-repo-pushes"></a>

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

**提示**  
如果您没有 Git 存储库，则可以在其他服务上创建一个空仓库， CodeCommit然后将仓库迁移到该存储 CodeCommit 库。您应按照[迁移到 CodeCommit](how-to-migrate-repository.md) 中的步骤进行操作。

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)
   ```

1. 运行**git remote set-url --add --push origin *git-repository-name***命令，其中*git-repository-name*是你要托管代码的 Git 存储库的 URL 和名称。这会将 `origin` 的推送目标更改为该 Git 存储库。
**注意**  
**git remote set-url --add --push** 会覆盖推送的默认 URL，因此您必须运行该命令两次，如后面的步骤所示。

   例如，以下命令将 Origin 的推送更改为*some-URL*/MyDestinationRepo：

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

   该命令不返回任何内容。
**提示**  
如果您要推送到需要凭据的 Git 存储库，请务必在凭证帮助程序或*some-URL*字符串配置中配置这些凭据。否则，到该存储库的推送操作将失败。

1. 再次运行 **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)
   ```

1. 现在添加 CodeCommit 存储库。再次运行 **git remote set-url --add --push origin**，这次请指定您的 CodeCommit 存储库的 URL 和存储库名称。

   例如，以下命令将 Orig **in** 的推送添加到 m https://git-codecom it.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
   ```

   该命令不返回任何内容。

1. 再次运行 **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 先到*some-URL*/。如果到该存储库的推送失败，则您的提交不会被推送到任何一个存储库。
**提示**  
如果另一个存储库需要您要手动输入的凭据，请考虑更改推送顺序，以便 CodeCommit 先推送到。运行 **git remote set-url --delete** 删除首先推送到的存储库，然后运行 **git remote set-url --add** 再次添加它，使其成为列表中的第二个推送目标。  
有关更多选项，请参阅 Git 文档。

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

   ```
   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.
   ```

1. 运行 **git add** 将更改暂存在本地存储库中：

   ```
   git add bees.txt
   ```

1. 运行 **git commit** 提交本地存储库中的更改：

   ```
   git commit -m "Added bees.txt"
   ```

1. 要将提交从本地存储库推送到远程存储库，请运行，**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 文档。