Git Clone

Learn how to clone remote repositories to your local machine in this lesson.

The git clone command

We can use the git clone command to clone or copy the entire codebase of a project from a remote repository and set it up as a local repository on our machines.

While cloning the project, two more actions occur as well:

  1. The git clone command will also create a remote link to the remote repository being cloned and name it origin. This is similar to manually entering the command git remote add origin <remote_repository_url>.

  2. It will copy and set up the primary branch, which is the master branch in most cases, as the active branch in the working directory.

You can test this command out yourself in the terminal provided at the end of the lesson. You can look up a repository on GitHub that you like and clone it.

git clone <link_to_repository>

Cloning a particular branch

In case you want to clone a branch that the HEAD in the remote repository doesn’t point to by default, you can also provide the name of that specific branch with the git clone command. For example, you might want to download or clone a branch other than the master branch.

The HEAD is the latest commit or snapshot of the branch that you are on. Consider it to be the last piece of a chain.

For a remote repository, if you used the git clone command without specifying a particular branch name, the HEAD will be considered to be the latest commit in the master branch.

git clone --branch <branch_name> <link_to_repository>

Shallow cloning

Sometimes, we might come across a remote repository that we want to clone, but its commit history might be too long, resulting in longer times to download and clone. This occurs when the project is very large and has a very large commit history. You can opt to clone the commit history up to a certain point by using the --depth flag.

Try the following command in the terminal provided below:

git clone <repository_url> --depth 1

You can change the depth number according to your requirement.

Try it yourself

In the terminal provided below, try out the commands to clone a repository from GitHub on your own. You can clone a repository that you’ve created or clone any other repository you want. As an example, try cloning this repository:

https://github.com/githubteacher/github-slideshow.git

This is a simple public repository for getting familiar with GitHub. You don’t need to worry about the contents of the repository itself.

You will need to enter the following command:

git clone https://github.com/githubteacher/github-slideshow.git

Try entering the command in the terminal below:

Get hands-on with 1200+ tech skills courses.