Creating local copies of a Git repository stored elsewhere is a central part of the Git version control system.
When you’re setting up a repository, the git clone command can be used to create a local copy of a repository stored elsewhere. This copy is also referred to as a “clone.”
In this tutorial, we’re going to explore the basics of the git clone command, how to clone a local and remote repository, and how to clone a bare repository. By the end of reading this tutorial, you’ll be an expert at cloning repositories using clone git command.
A git repository is a place in which you can store code for a project. Git repositories are capable of tracking different versions of code and changes made to a codebase, which you are able to access when you need to do so.
There are two ways you can set up a repository using Git.
If you are creating a new repository, you can use the git init command to create one. However, if a project has already been set up in another repository, you can use the git clone command to obtain a local copy of the existing repository. Then, you’ll have a local repository that you can use to add to the codebase.
Cloning a repository is typically a one-time action, after which point you’ll have all the code you need on your local machine stored within a repository.
Cloning is a central feature of Git because it allows you to create copies of code that you can run and manipulate on your local machine. This means that you don’t have to make changes to a repository until you are ready to do so. After you have made any changes, you can push the code to a remote repository to be stored, or create a pull request so that other people can review the code you have written.
How to Use Git Clone
The git clone command points to an existing Git repository and makes a clone of that repo in a new directory on your local machine. When you clone a repository to your local machine, a new environment will be created where the code for the repo will be stored.
Let’s walk through an example of how to use the git clone command to clone a repository. Suppose we are looking to clone a repository from GitHub to our local machine. Once we clone the repository, we’ll have a local copy of the code with which we can work.
We could use the following code to create a clone of the project:
git clone https://github.com/username/project-name.git cd project-name
In our code, the first command creates a new Git repository based on the one stored at the GitHub URL we specified. The name of the folder in which our code appears is equal to the name of the repository. In this case, Git creates a folder called project-name. Then, we use the cd command to move into our new Git repository so that we can start viewing and editing our files.
Custom Clone Operations
The git clone command has a few parameters which can be used to perform custom clone operations. Let’s explore an example of the main custom operations you may want to know about when you’re working with git clone.
Clone to a Folder
In our first example, we cloned a repository without specifying a folder name. This meant that, by default, a folder was created with the same name as the repository we cloned.
However, when you’re cloning a repository, you may want to clone it into a specific folder. You can do so by specifying a directory name in which a Git repository should be cloned.
Here’s the syntax for cloning to a specific folder using Git:
git clone <repo> <folder>
Suppose we want to clone the contents of a hypothetical Git repo to a folder called “my-project”. We could do so using this code:
git clone https://github.com/username/project-name.git my-project cd my-project
First, our code clones the GitHub repository we referenced. In our example, we specify a folder name, “my-project”, which is where the code for our repo will be cloned. Then, we use cd to move into the my-project folder the git clone command created.
Create a Shallow Clone
By default, when you clone a repository, the history of the repository will also be cloned. This means that if your project has been going on for years and has thousands of commits, it may take a longer period of time for a local clone to be created.
To skip cloning an entire repository, you can create a shallow clone. Shallow clones only clone the history of commits specified by the “depth” parameter that is used to create a shallow clone.
The syntax for creating a shallow clone is as follows:
git clone -depth=1 <repo>
The value of the “depth” parameter refers to the number of historical commits you want to clone. In this case, we specified 1 as the value of the “depth” parameter, which means that we will only copy the most recent commit to our local machine.
Suppose our hypothetical GitHub repository had 10,000 commits, which means it would take quite a while to copy completely to our local machine. We only want to clone a copy of the current code and its 10 last commits, without also cloning the entire repository’s history. We could do so using this code:
git clone -depth=10 https://github.com/username/project-name.git
Now, when we navigate into our project, we will have a copy of the project in its current state, as well as information on the last 10 commits. However, our project will not store data on any commits further back in the repository’s history.
Clone a Tag
When you’re cloning a repository, you may only want to clone a version of the repository with a specific tag. Tags are references that point to a specific version of a repository. For instance, the version of a project in beta may have a tag called “beta-v0.9”, which makes it clear that the version is in beta.
You can learn more about git tags in our beginner’s guide to git tags.
The syntax for cloning a version of a repository based on its tag is as follows:
git clone --branch <tag> <repo>
In this syntax, “tag” refers to the tag of the code’s version you want to copy, and “repo” refers to the repository from which you want to clone code. Suppose we wanted to clone a version of our hypothetical GitHub repository with the tag “v0.2.2”. We could do so using this code:
git clone --branch v0.2.2 https://github.com/username/project-name.git
The git clone command will clone the version of the repository with the tag “v0.2.2” to the local machine.
If you’re interested in finding out more about the configuration options offered by the git clone command, read the official Git documentation on the git clone command.
There are a number of different URL types that are supported by the git clone command. The git clone command is usually used to clone remote repositories, and so we will explore the main URL types supported by Git below.
The git protocol is used to clone a repository using the Git version control system. The git protocol does not use any methods of authentication. Here’s an example URL that uses the Git version control system:
As you can see, the URL starts with “git:”, which tells the Git client to clone a repository using the git protocol.
Secure Shell, or SSH, is a tool used to access networked servers remotely. SSH provides authentication, and so SSH URLs are often used for secure Git repositories. Here is an example Git URL that uses the SSH protocol:
In this example, “user” refers to the name of the user who is trying to access a particular repository. Before we are able to clone the repository at our location, we will need to sign in based on the credential protocol used by the server.
HyperText Transfer Protocol, or HTTP, is used for transferring web page data across the internet. HTTP URLs are commonly used for Git repositories, too. Here is an example Git URL that uses HTTP:
In this example, our repository uses the HTTPS protocol. Instead of using “git” or “ssh”, we instead specify “https” at the start of our Git repository.
git clone is used to create a local copy of a Git repository. The target repository cloned using git clone can either be stored locally or remotely, but in most cases, git clone is used to clone a remote repository.
This tutorial explored, with examples, how to use the git clone command. Now you have the knowledge you need to start cloning repositories using git clone like a professional programmer!