Git

How to use Git Clone Command?

The most commonly used Git command is the ‘Git Clone’. This is used to create a copy or clone of an existing target repository in a new directory. The original repository will be stored on the remote machine or the local file system with accessible supported protocols.

In this article, you will explore the use of the Git clone command in detail. A command-line utility of Git that is used to point an existing repository and creates a copy of that targeted directory. Here, we will examine the different Git clone command configuration options and their corresponding examples. We have implemented the Git clone examples on Ubuntu 20.04 Linux system.

Cloning Git Repository by Using Git Clone Command

If you want to make a clone of an existing Git repository, then you can easily do this using the Git clone command. For example, you would like to contribute to a project, then simply use the Git clone command. If you have used VCS systems before, like Subversion, then you will be familiar with commands ‘clone’ and not ‘checkout’. These systems only take the working copy. Here, the Git clone is the whole server repository instead of just a working copy. When you run a Git clone command on your system, every version of the file with the whole project is pulled down by default at your specified location. Let’s suppose if your server disk is corrupted due to any reason, then by using the clones of any client, you can set the server back to its state. You may lose server-side hooks but all file versions would be available there.

Git Clone command Syntax

$ git clone <git-hub URL>

Example

For example, we want to clone a library called ‘libgit2’. By using the Git link, which is shown below, you can create a copy of that library.


Now, by running the following Git clone command, create a clone of that repository:

$ git clone https://github.com/libgit2/libgit2


The above command creates a directory named ‘libgit2’ in which the .git directory initializes inside it, all data pull-down of the above repository, then checks out the latest version of the working copy. Now, you can navigate into the directory ‘libgit2’ that was already created. You will find all project files there, ready to be used now.

You can clone a repository into a renamed directory instead of libgit2, then you can specify an additional argument as the name of the directory.

$ git clone https://github.com/libgit2/libgit2 mytestproject


The above command will do the same as the previous one, but now the name of the target directory is called ‘mytestproject’. Using the following command you can navigate into the above directory and list the files of the ‘mytestproject’ directory:

$ cd mytestproject

$ ls -a

Git Clone options

With the Git clone command, you can use many different options according to your requirements. To display all Git clone options, you will type the following command on the terminal:

$ git clone

You will observe the different options that you can easily use with the Git clone command.

Git URLs Protocol Examples

You can find Git URLs protocols in the following syntax :

SSH

ssh://[user@]host.xz[:port]/path/to/repo.git/

Git :

git://host.xz[:port]/path/to/repo.git/

HTTP :

http[s]://host.xz[:port]/path/to/repo.git/

Conclusion

From the above information, we have discussed how to use the Git clone command on Ubuntu 20.04. Furthermore, we have seen how to clone a target repository. Git supports different URLs protocols including those we have mentioned in this article. For more information about the Git clone command, you can get help from the main page of the Git clone command.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.