將遞交推送至其他 Git 儲存庫 - AWS CodeCommit

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

將遞交推送至其他 Git 儲存庫

您可以設定本機儲存庫將變更推送到兩個遠端儲存庫。例如,您在試用 AWS 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 和儲存庫名稱。

    例如,下列命令會將原始伺服器推送新增至 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 儲存庫做為推送的目的地,但您的推送會先移至 some-URL/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 文件。