Signup/Sign In

Git Clone Command

Cloning is the process of creating a replica of something. In Git terminology, cloning implies copying a repository. Creating a clone or a copy of a repository helps developers to build something on top of an already existing project. Cloning also enables us to freely work on the different versions of someone else's project and collaborate with them.

Cloning

  • Cloning is the process of creating a new copy of an existing repository. It is a very important command for collaboration purposes as it allows us to view the work of others.
  • Whenever you start working on an existing project, the Git Clone will most likely be the first command you execute. This will fetch you an up-to-date copy of the project and you can start working on it. Most of the time we will be cloning from a central remote repository.
  • Git works on a Distributed Model which allows us to copy the entire history of a project. This includes all the commits that were made, different branches that were used, all the tags that were created, and so on.
  • The cloned repository is version-controlled, which means that we don't need to initialize it after cloning.

Git Clone Illustration

How does Cloning work?

Cloning a repository is not a single-step process. Let's look at what happens behind the scenes when we run a Git Clone command.

  1. When we clone a repository, Git first initializes a new empty repository on our system. This step is similar to using the Git Init command to initialize a new repository.
  2. Next Git creates a remote connection to the repository from which we need to clone(similar to the Git Remote command). This also helps us in the future when we need to push or pull from that same repository.
  3. Now Git fetches the commits, branches, and other necessary data from the repository(Git Fetch). This will also create remote-tracking branches which will help us in the future if we again want to fetch from the same repository.
  4. Next Git creates a local branch called master which will have all the commits from the remote repository's master branch.

Let's clone a repository to understand the above steps better.

  • Suppose we clone a repository from the URL https://github.com/codebreaker003/Bubble-Sort. We see that the directory bubble-sort is a Git Repository as it contains a .git directory.

Cloning a repository

Viewing the .git folder using ls -a command

Let's take a look at the remote connections that were added using Git Remote. We can see a remote named origin was created.

Viewing the remote that was created by Git Clone

Now let's take a look at the commits that were fetched by using the Git Log.

Viewing the commit history by using the Git Log command

We can see the remote branches by using the Git Branch command.

Viewing the remote tracking branches.

We can also see that a local master branch was also created.

Viewing the local master branch

Git Clone Command

We can simply clone a remote repository by using its URL.

$ git clone <remote-repo-url>

The above command will by default clone the repository in our current working directory, We can specify a particular folder where want to clone the repository.

$ git clone <remote-repo-url> <directory-name>

Git also provides us with the choice of cloning only some part of the repository. This type of cloning is called Shallow Cloning. We can specify the depth up to which we want to clone the commits. The following command will clone only the n most recent commits from the repository.

$ git clone ~depth=n <remote-repo-url>

We can also clone just a single branch of a remote repository by using the --single-branch option. By default, this will clone the master branch but we can specify a branch name and Git will only clone that branch.

$ git clone <remote-repo-url> --branch <branch-name> --single-branch

We can also clone the history up to a particular tag by using the following command.

git clone --branch <tag-name> <remote-repo-url>

Summary

Git Clone is a command that we will often use while working with Git. It is the first command that we execute when building something on top of an existing project. It also helps us collaborate with other developers. Git being a distributed version control system ensures that we get the entire history of a repository whenever we clone it. We learned about the working of the Git Clone command and also saw some important options we can use with it.



About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight