The git clone command creates a copy of a remote repository on your local machine. By default, the clone command saves your code in a folder that shares the name of your repository. This can be overwritten by specifying a folder name after the URL of the repository you want to clone.
Creating local copies of a Git repository stored elsewhere is a central part of the Git version control system.
The git clone command lets you 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. We’ll discuss how to clone a local and remote repository, and how to clone a bare repository.
What is the git clone Command?
The git clone command creates a copy of an existing repository to a working directory on your local computer.
Cloning is a central feature of Git. This is because cloning allows you to create a copy of your code independent of the main version. You can run this copy run and manipulate your code on your local machine without affecting the code in your main version.
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, you can create a pull request so that other people can review the code you have written.
There are two ways you can set up a repository using Git. You can clone an existing repository using the git clone command, or you can use the git init command to create a new repository.
Cloning a repository is typically a one-time action. After you’ve cloned a repository, you’ll have all the code you need on your local machine to work with a Git repository.
The git clone command clones all the metadata associated with a repository. Once you have cloned a repository, you’ll have a record of the entire history of the project. The clone command also clones all Git branches associated with a project.
How to Use Git Clone
The git clone command creates a clone of an existing repository 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.
We could use the following command line operation to clone our 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. Now we are viewing our new repository, we can start viewing and editing our files.
Custom Clone Operations
The git clone command accepts a few parameters. You can use these parameters 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
By default, a folder is created with the same name as the repository we cloned. In our first example, we cloned a repository without specifying a folder name. The resultant copy of our repository was called ck-git. This is because we did not state a folder name.
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.
We can run git clone commands with a second argument like so:
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. If your project has hundreds of commits, it will take longer 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. This means that the clone command 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
The above command has created a copy of our project in its current state. We can search back 10 commits in the history of the repository. However, our project does not data on any commits further back in the repository’s history.
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot
Read the official Git documentation on the git clone command to learn more about advanced configuration options.
Git URLs
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.
Git
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:
git://host.com/repo/path.git
The URL starts with “git:”, which tells the Git client to clone a repository using the git protocol.
SSH
Secure Shell, or SSH, lets you 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:
ssh://user@host.com/repo/path.git
In this example, “user” refers to the name of the user who is trying to access a particular repository. We must sign in over SSH before we can clone a repository.
HTTP
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:
https://host.com/repo/path.git
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 URL.
Conclusion
The git clone command is used to create a local copy of a Git repository. Cloning lets you create an independent copy of a repository from which you can edit without affecting the main version of your project.
When you are ready to save your changes to the main version of a project, you can create a commit.
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!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.