將存放區新增為遠端存放區

您可以使用 Cloud Source Repositories 將 Google Cloud存放區做為遠端存放區新增至本機 Git 存放區。

事前準備

  1. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  2. 安裝 Git
  3. After installing the Google Cloud CLI, initialize it by running the following command:

    gcloud init

    If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

將存放區新增為遠端

  1. 將您目前的工作目錄變更為包含本機 Git 存放區的目錄。

    cd [REPO_DIRECTORY]
    

    其中 [REPO_DIRECTORY] 是存放區的路徑。

  2. 在 Linux 或 macOS,執行憑證輔助程式指令碼:

    Linux 或 macOS

    git config --global credential.'https://source.developers.google.com'.helper gcloud.sh
    

    Windows

    git config --global credential.https://source.developers.google.com.helper gcloud.cmd
    

    憑證輔助程式指令碼可提供 Git 所需的資訊,以使用您的 Google 帳戶憑證安全連線至 Cloud Source Repositories。您不需要執行其他任何設定步驟 (例如上傳 ssh 金鑰) 即可建立此安全連線。

  3. 在 Cloud Source Repositories 中建立存放區:

    gcloud source repos create [REPO_NAME]
    

    其中 [REPO_NAME] 是 Google Cloud 存放區的名稱。

  4. 將存放區新增為遠端存放區:

    git remote add google \
    https://source.developers.google.com/p/[PROJECT_ID]/r/[REPO_NAME]
    

    其中:

    • [PROJECT_ID] 是您的 Google Cloud 專案 ID。
    • [REPO_NAME] 是 Google Cloud 存放區的名稱。

將存放區做為遠端存放區使用

Google Cloud 存放區是功能完善的 Git 存放區。您可以使用標準的 Git 指令與這些存放區互動,包括 pushpullclonelog

推送至 Google Cloud 存放區

如要從本機 Git 存放區推送至 Google Cloud 存放區,請輸入以下指令:

git push google master

從 Google Cloud 存放區提取

如要從 Google Cloud 存放區提取至本機 Git 存放區,請輸入以下指令:

git pull google master

查看 Google Cloud 存放區的修訂記錄

如要查看修訂記錄,請輸入下列指令:

git log google/master

產生靜態憑證

在某些情況下,您可能無法存取 gcloud 指令列介面。舉例來說,您作業的組建伺服器可能沒有這些工具的存取權。在這些情況下,您可以手動產生靜態憑證。

  1. 在 Google Cloud 控制台中,開啟 Cloud Source Repositories。

    開啟 Cloud Source Repositories

  2. 將滑鼠游標懸停在要複製的存放區上。

  3. 按一下「Clone」圖示

    系統會開啟含有驗證選項的對話方塊。

  4. 按一下「手動產生的憑證」分頁標籤,然後按照操作說明複製存放區。

後續步驟