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 Google Cloud CLI, 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 username.
    • [PROJECT_ID] is the ID of the Google Cloud project where the repository is located.
    • [REPO_NAME] is the name of the repository.

    For example:

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

    git push --all google
    

gcloud CLI

  1. Ensure that the gcloud CLI 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 Google Cloud 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 Google Cloud 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

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 the following command:

git push google master

Pull from a Google Cloud repository

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

git pull google master

View the commit history of a Google Cloud 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: