從現有存放區推送程式碼

本主題說明如何從本機電腦上的現有存放區將程式碼推送至 Cloud Source Repositories。

事前準備

您必須在 Cloud Source Repositories 有一個存放區。如要建立存放區,請參閱「建立空的存放區」。

將本機存放區推送至 Cloud Source Repositories

您可以使用 SSH、Google Cloud CLI 或手動產生的憑證,將本機存放區推送至 Cloud Source Repositories。

安全殼層 (SSH)

  1. 請確認您已使用 SSH 設定本機驗證

  2. 將本機存放區新增為遠端存放區:

    git remote add google ssh://[EMAIL]@source.developers.google.com:2022/p/[PROJECT_ID]/r/[REPO_NAME]
    

    其中:

    • [EMAIL] 是您的 Google Cloud 使用者名稱。
    • [PROJECT_ID] 是存放區所在 Google Cloud 專案的 ID。
    • [REPO_NAME] 是存放區的名稱。

    例如:

    git remote add google ssh://user@gmail.com@source.developers.google.com:2022/p/example-project/r/test-repo
    
  3. 將程式碼推送至 Cloud Source Repositories:

    git push --all google
    

gcloud CLI

  1. 確認機器已安裝 gcloud CLI

  2. 開啟終端機視窗。

  3. 如果您使用 Windows,請輸入以下指令:

    gcloud init && git config credential.helper gcloud.cmd
    

    如果您使用的是 Linux 或 macOS,請輸入以下指令:

    gcloud init && git config credential.helper gcloud.sh
    
  4. 將本機存放區新增為遠端存放區:

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

    其中:

    • [PROJECT_NAME] 是您的 Google Cloud 專案名稱。
    • [REPO_NAME] 是存放區的名稱。
  5. 將程式碼推送至 Cloud Source Repositories:

    git push --all google
    

手動

  1. 請確認您已產生及儲存 Git 憑證

  2. 開啟終端機視窗,並將本機存放區新增為遠端存放區:

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

    其中:

    • [PROJECT_NAME] 是您的 Google Cloud 專案名稱。
    • [REPO_NAME] 是存放區的名稱。
  3. 將程式碼推送至 Cloud Source Repositories:

    git push --all google
    

將存放區做為遠端存放區

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

後續步驟

將本機存放區推送至本機電腦後,您可以: