Live streamJoin us for "State of code review 2024" on July 18thRegister today

How to clone a git repository with git clone

Greg Foster
Greg Foster
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


The git clone copies an existing Git repository into a new directory on your local machine. Whether you're cloning a repository from GitHub, another remote server, or a local repo, git clone sets up a version-controlled workspace for you to start working on the project.

This guide provides detailed instructions on how to use git clone, covering various scenarios and commands.

In order to clone a repository you can either use SSH or HTTPS. For further detail see this guide on the differences between cloning with SSH and HTTPS.

To clone a repository:

  • Open a terminal (or Git Bash on Windows).

  • Run the command:

    git clone <repository-url>

  • Replace <repository-url> with the URL of the Git repository you wish to clone. This URL can be found on the repository's page on GitHub or any Git hosting service.

screenshot of where to find a repo url on github

You can also specify a specific directory on your local machine, that the repository will be cloned into:

  • Specify the directory name at the end of the git clone command:

    git clone <repository-url> <directory>

  • Replace <directory> with your desired directory name or file path. If the directory does not exist, Git will create it for you.

To clone a specific branch from a repository:

  • Use the b option with git clone:

    git clone -b <branch-name> <repository-url>

  • Replace <branch-name> with the name of the branch you want to clone.

To clone using SSH (assuming you have set up SSH keys for your Git account):

  • Use the SSH URL of the repository:

    git clone git@github.com:user/repository.git

  • This method is secure and allows you to clone repositories without entering your username and password, provided your SSH key is added to your Git account.

For more information on cloning with SSH see this walkthrough on SSH Git cloning.

  • Shallow clone: To clone with limited commit history (reducing size and time), use the -depth <depth> option to specify the number of recent commits you want in your clone.

    git clone --depth 1 <repository-url>

  • Cloning quietly: To suppress the output of the clone command, use the --quiet option.

  • Basic Repository Clone:

    git clone <https://github.com/my-username/my-repo.git>

  • Cloning into a Specific Folder:

    git clone <https://github.com/my-username/my-repo.git> ~/workspace/my-repo-folder

  • Cloning a specific branch:

    git clone -b my-branch <https://github.com/my-username/my-repo.git>

  • Shallow clone of a repository:

    git clone --depth 1 <https://github.com/my-username/my-repo.git>

  • Windows: Use Git Bash or the Windows Command Prompt with the same git clone command. Ensure Git is installed on your system.

  • Linux/Ubuntu: Open the terminal and use the git clone command. Git might need to be installed using your distribution’s package manager.

  • Mac: Open the Terminal app and use the git clone command. Git comes pre-installed on most Macs, or it can be installed via Xcode Command Line Tools. For further detail see this guide on installing Git on Mac.

  • Not cloning all files: Ensure you have access to all branches and files. Some files might be ignored by .gitignore or stored in LFS, Git’s solution for storing large files not compatible with the traditional Git storage schema.

  • Permission denied: When cloning via SSH, ensure your public key is added to your Git account. For HTTPS, check if your access token or password is correct.

For further reading on cloning with Git, see the official Git documentation.

Stay unblocked. Ship faster.
Experience the new developer workflow - create, review, and merge code continuously. Get started with one command.
Get started

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2