Pushing code from an existing repository

This topic describes how to push code from an existing repository on your local machine to Cloud Source Repositories.

Before you begin

You must have a repository in Cloud Source Repositories. To create a repository, see Creating an empty repository.

Push a local repository into Cloud Source Repositories

You can push a local repository to Cloud Source Repositories by using either SSH, the Cloud SDK, or manually generated credentials.

SSH

  1. Ensure you have set up local authentication with SSH.

  2. Add your local repository as a remote:

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

    Where:

    • [EMAIL] is your Google Cloud Platform (GCP) username.
    • [PROJECT_ID] is the ID of the GCP project where the repository is located.
    • [REPO_NAME] is the name of the repository.

    For example:

    git remote add google ssh://user@gmail.com@source.developers.google.com:2022/p/example-project/r/test-repo
    
  3. Push your code to Cloud Source Repositories:

    git push --all google
    

Cloud SDK

  1. Ensure that the Cloud SDK is installed on your machine.

  2. Open a terminal window.

  3. If you're using Windows, enter the following command:

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

    If you're using Linux or macOS, enter the following command:

    gcloud init && git config credential.helper gcloud.sh
    
  4. Add your local repository as a remote:

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

    Where:

    • [PROJECT_NAME] is the name of your GCP project.
    • [REPO_NAME] is the name of your repository.
  5. Push your code to Cloud Source Repositories:

    git push --all google
    

Manual

  1. Ensure you have generated and stored your Git credentials.

  2. Open a terminal window and add your local repository as a remote:

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

    Where:

    • [PROJECT_NAME] is the name of your GCP project.
    • [REPO_NAME] is the name of your repository.
  3. Push your code to Cloud Source Repositories:

    git push --all google
    

Use the repository as a remote

GCP 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 GCP repository

To push from your local Git repository to a GCP repository, enter the following command:

git push google master

Pull from a GCP repository

To pull from a GCP repository to your local Git repository, enter the following command:

git pull google master

View the commit history of a GCP repository

To view the commit history, enter the following command:

git log google/master

What's next

After you push your local repository to your local machine, you can:

Was this page helpful? Let us know how we did:

Send feedback about...

Cloud Source Repositories Documentation