How to Clone a Git Repository with Git Clone (Remote and Local) Examples

In this tutorial, you will learn about how to use the git clone command to clone an existing repository from a remote.

Cloning a repository means that you will create a copy of an existing repository into a new repository.

When you clone a repository, be it a local or a remote repository, you clone all the files and also all the Git history.


Subscribe to my Newsletter


Here, we will focus on cloning remote repositories locally on your computer.

Remote repositories are Git repositories that are hosted in an online hosting service such as Github, Gitlab or Bitbucket.

If you are using Windows, you can read the more detailed steps on this article: Clone a Git Repository on Windows.

What Does it Mean to Clone a Repository?

When you clone a repository, you create a copy on your computer of the files within the repository, along with its entire history, branches, and tags.

Cloning a git repository to a local folder takes an existing remote, makes a full copy out of it on your computer and sync both locations. The copy is made at a point in time and includes all the files and folders, and all their versions for the project.

When cloning a repository, Git knows where the original repository was by storing a remote tag in the new repository’s configuration. You can view the remote using the git remote command.

When you clone the repository, Git will automatically name the remote origin.

How to Git Clone a Local Repository

You can clone a remote or a local repository using the git clone command.

To clone a local repository:

  1. Open your Terminal
  2. Navigate to the location where you want the local repository to be copied, using the cd command
  3. Use the git clone command followed by the path to the project directory
  4. You can also give it a name using git clone /path/to new_repo

Clone a Remote Repository on Your Local Computer

You can use the git clone <URL> command to clone a remote repository.

The <URL> shows the location of the repository that you want to clone.

The git clone command looks something like this:

git clone https://github.com/user/repo.git

How to Clone a Repository From Github

You can male a copy of an existing remote repository on your local computer by cloning it. Here are the steps to do so.

  1. On Github.com, go to the repository

  2. On top of the files, click on <> Code

    git clone code

  3. Copy the URL of the repository

    There are three ways that you can clone your repository:
    Clone the repository using HTTPS under “HTTPS”.
    Clone the repository using an SSH key under “SSH”
    Clone a repository using GitHub CLI under “GitHub CLI”

    View the section later to know if you should choose HTTPS or SSH.
    copy the url of the github repository

  4. Open Terminal and Move to the Repo where to Clone the Remote

    cd repo/location
    move to directory where to clone your repo in Terminal

  5. Add the git clone command

    git clone command

  6. Press ENTER

    git clone result

How to Clone a Repository From Gitlab

The steps to clone a repository from Gitlab are similar.

You can also find the clone URL straight from the Gitlab repository.

How to Clone a Repository From Bitbucket

Again, similar technique to clone a git repository from Bitbucket.

  1. Open Bitbucket and go to the repository you want to clone.
  2. Go on the left hand side of the screen, select the clone button (second)
  3. Click on “Clone” on the right-hand side of the page.
  4. Copy the URL
  5. Open your terminal (or command prompt)
  6. cd into the directory where you want to clone the repository.
  7. Type the “git clone” command with the URL that you want to clone.

Git Clone with Username and Password

To provide a username and password when running the “git clone git@remote.git” command use the username@github.com format in the cloning URL.

$ git clone https://username@github.com/username/repository.git

It is not necessary to add the password in the command as Git with prompts you for the password. It is more secure this way.

Otherwise, adding the password to the clone URL this way will store the password in the Bash history file.

$ git clone https://username:password@github.com/username/repository.git

Git Clone Branch

To clone a specific git branch use the git clone -b flag (short for --branch):

$ git clone -b <branch> <repository URL>

Here is an example of cloning a branch without switching branches on the remote repository.

$ git clone -b branch-name git@github.com:user/repository.git

Should You Use HTTPS or SSH For Git?

When connecting to a Git remote, you can use either the SSH or HTTPS protocol.

Using HTTPS or SSH to connect to remote Git repositories like Gitlab and Github depends on the level of security that your project requires.

  • HTTPS is easier to set up
  • SSH is more secure as it offers a stronger encryption and authentication. Transferred data is encrypted when using SSH. It is also faster.

Git Clone HTTPS

The HTTPs clone command looks like this:

git clone https://github.com/user/repo.git

Git Clone SSH

While the SSH command you connect using a password-protected SSH key using this pattern:

git@github.com:user/repo.git

Multiple Ways to Copy a Repository

There are many ways that you can use to create a copy of a Git Repository: cloning, forking, mirroring, archiving and exporting.

  • Cloning: Create a full copy of the repository on your local machine, including all of its commit history, branches, and tags. Use cloning when working on an existing repository.
  • Forking: Create a copy of the repository on the Git hosting service instead of your local machine. Use Forking when you want to contribute to an open-source project that you don’t have write access.
  • Mirroring: Create a read-only copy of a Git repository. Use mirroring to create backups.
  • Archiving: Create a compressed copy of a Git repository. Use archiving to create backups or to share to non-git users.
  • Exporting: Create a copy of a Git repository in a non-Git format. Use exporting to share to non-git users.
  • Cloning a specific branch: Create a copy of a single branch of a Git repository. Clone a branch using the “git clone -b <branch_name> <URL>” command.

Difference Between Cloning and Forking a Repository

Cloning and forking are two ways to copy a Git repository, but have different purposes.

To clone a repository means that you create a copy of the full repository on your computer. When cloning, you download all the files, folders, along with the commit history, the branches, on your local computer. When you clone a repository, you are able to make, commit and push changes made to your local copy.

To fork a repository means that you create a copy of the repository on a Git hosting service (e.g. Github, Gitlab, Bitbucket). When forking, you create a new repository (identical to the original). When you fork a repository, you have full control over it and are able to make, commit and push changes made to the fork. You can’t however commit to the original repository without a merge request.

Debug Cloning Errors

Error: Repository not found

The Error: Repository not found error in git can happen when the repository does not exist or when you don’t have:

  • The right repository URL. Check for a typo or the wrong casing when cloning
  • Enough permissions to access the repository (e.g. private repository)
  • SHH Key connected to your personal Github account

Fatal: Git Clone Authentication Failed for URL

The fatal Authentication failed issue faced when cloning a git repository can be caused by the following things:

  1. User logged out due to multiple incorrect login attempts,
  2. two-factor authentication is turned on
  3. Windows messed up with credentials stored in the credentials manager
fatal: Authentication failed for <URL>

Debugging Fatal: Git Clone Authentication Failed

  1. Got to Gitlab/Github/Bitbucket website and log in manually so that you can complete the CAPTCHA.
  2. Generate access token on Github, go to Applications and generate token (steps)
  3. In Windows, Search for Credential Manager. Select Windows manager and select your Gitlab/Github/Bitbucket credentials and modify them.

Other Git Methods and Github Posts

Learn Git and Github (Complete Guide)
Basics of Version Control
How to Use Git and Github with VSCode
Get Started With Github
How to Push Code to Github
Enjoyed This Post?