从现有代码库推送代码

本主题介绍如何将本地机器的现有代码库中的代码推送到 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

后续步骤

将本地代码库推送到本地机器后,您可以: