You can use Cloud Source Repositories to add a Google Cloud
repository as a remote to a local Git repository.

> [!NOTE]
> **Note:** You can add only private repositories as remotes.

## Before you begin

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 role (`roles/resourcemanager.projectCreator`), which contains the `resourcemanager.projects.create` permission. [Learn how to grant
     roles](https://docs.cloud.google.com/iam/docs/granting-changing-revoking-access).

   > [!NOTE]
   > **Note**: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.

   [Go to project selector](https://console.cloud.google.com/projectselector2/home/dashboard)
2. Install [Git](http://git-scm.com/).
3.
   [Install](https://docs.cloud.google.com/sdk/docs/install) the Google Cloud CLI.

   After installation,
   [initialize](https://docs.cloud.google.com/sdk/docs/initializing) the Google Cloud CLI by running the following command:

   ```bash
   gcloud init
   ```


   If you're using an external identity provider (IdP), you must first
   [sign in to the gcloud CLI with your federated identity](https://docs.cloud.google.com/iam/docs/workforce-log-in-gcloud.md.txt).

## Add the repository as a remote

1. Change your current working directory to the directory that contains your
   local Git repository:

       cd [REPO_DIRECTORY]

   Where `[REPO_DIRECTORY]` is the path to your repository.
2. On Linux or macOS, run the credential helper script:

   ### Linux or 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

   Credential helper scripts provide the information that Git needs to connect
   securely to Cloud Source Repositories using your Google Account
   credentials. You don't need to perform any additional configuration steps
   (for example, uploading `ssh` keys) to establish this secure connection.

   > [!NOTE]
   > **Note:** The Google Cloud CLI must be in your `$PATH` for the credential helper scripts to work. If you cannot use the Google Cloud CLI, you can manually generate static credentials. For example, if you're on a build server, you might not have access to the gcloud CLI. For more information, see [Generating static credentials](https://docs.cloud.google.com/source-repositories/docs/adding-repositories-as-remotes.md.txt#generate_static_credentials) in this topic.

3. Create the repository in Cloud Source Repositories:

       gcloud source repos create [REPO_NAME]

   Where `[REPO_NAME]` is the name for your Google Cloud repository.
4. Add a repository as a remote:

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

   Where:
   - `[PROJECT_ID]` is your Google Cloud project ID.
   - `[REPO_NAME]` is the name of your Google Cloud repository.

## Use the repository as a remote

Google Cloud repositories are fully featured Git repositories. You can use
the standard set of Git commands to interact with these repositories, including
`push`, `pull`, `clone`, and `log`.

### Push to a Google Cloud repository

To push from your local Git repository to a Google Cloud repository, enter
this command:

    git push google master

### Pull from a Google Cloud repository

To pull from a Google Cloud repository to your local Git repository, enter
this command:

    git pull google master

### View the commit history of a Google Cloud repository

To view the commit history, enter this command:

    git log google/master

## Generate static credentials

In some cases, you might not have access to the gcloud CLI. For example,
you might work on a build server that doesn't have access to these tools. In
these situations, you can manually generate static credentials.

1. In the Google Cloud console, open Cloud Source Repositories.

   [Open Cloud Source Repositories](https://source.cloud.google.com/repos)
2. Hold the pointer over the repository you want to clone.

3. Click **Clone** .

   A dialog with authentication options opens.
4. Click the **Manually generated credentials** tab and follow the
   instructions to clone your repository.

## What's next

- After you have pushed source files to Cloud Source Repositories, you can
  view repository files from within the Google Cloud console.

- [Learn how to use the source browser](https://docs.cloud.google.com/source-repositories/docs/using-source-browser).