リポジトリをリモートとして追加する

Cloud Source Repositories を使用して、リモートの Google Cloud リポジトリをローカルの Git リポジトリに追加できます。

始める前に

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

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  2. Git をインストールします。
  3. Install the Google Cloud CLI. After installation, initialize the Google Cloud CLI 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 Repository の名前です。

  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 Repository の名前です。

リポジトリをリモートとして使用する

Google Cloud Repository は、多機能の Git リポジトリです。Git コマンドの標準セットを使用して、このようなリポジトリ(pushpullclonelog など)を操作できます。

Google Cloud Repository に push する

ローカルの Git リポジトリから Google Cloud Repository に push するには、次のコマンドを入力します。

git push google master

Google Cloud Repository から pull する

Google Cloud Repository からローカルの Git リポジトリに pull するには、次のコマンドを入力します。

git pull google master

Google Cloud Repository の commit 履歴を表示する

commit の履歴を表示するには、次のコマンドを入力します。

git log google/master

静的認証情報の生成

gcloud CLI にアクセスできない場合があります。たとえば、これらのツールにアクセスできないビルドサーバーで作業する場合などです。このような状況では、静的な認証情報を手動で生成できます。

  1. Google Cloud Console で、Cloud Source Repositories を開きます。

    Cloud Source Repositories を開く

  2. クローンを作成するリポジトリの上にポインタを置きます。

  3. [クローン] をクリックします。

    認証オプションを含むダイアログが開きます。

  4. [手動で生成した認証情報] タブをクリックし、手順に沿ってリポジトリのクローンを作成します。

次のステップ