Git clone allows you to create a copy of your repository in a new directory or location. It can be used to replicate a remote repository on your local system or create a local clone to be shared over an intranet. This recipe covers the git clone
command. We will learn to create a clone of a remote repository and then take a look at various transport protocols supported by Git for cloning.
You will need Git binaries installed on your local system, plus a remote repository. Note down the full path (clone URL) of the remote repository.
Create a clone of the repository with the git clone
command, as follows:
$ git clone ssh://[email protected]:22/home/ubuntu/cookbook.git ubuntu_cookbook
You will be asked to enter a password for the user account ubuntu
.
This command will create a new directory named ubuntu_cookbook
and clone the repository cookbook.git into this directory.
As seen in the previous example, the git clone
command will create a new copy of an existing repository. The repository can be a local repository or one located on a remote server. Git supports various protocols to transfer the content between two systems. This includes well-known protocols such as SSH, HTTP/S, and rsync. In addition, Git provides a native transport protocol named Git. Note that the Git protocol does not require any authentication and should be used carefully. In the previous example, we have used the SSH protocol. When working with local repositories, you can use file///path/to/repo.git
or even an absolute path /path/to/repo.git
format.
Cloning requires a single argument, which is the path of the repository to be cloned. You can skip the destination directory and Git will create a clone in a new directory named after the repository name.
You can also create a new bare clone with the --bare
option of the git clone
command. This is useful for creating a shared central clone that is used by a group of people.
Another important option is the depth clone. When cloning a large repository that contains years of work, and you do not really need the entire history of the repository, the option --depth
can be used to copy only a specified number of revisions. This will help you in quickly downloading just the tip of an entire repository, and will save you some bandwidth by avoiding unnecessary downloads. The syntax for the --depth
option is as follows:
git clone --depth 1 https://github.com/torvalds/linux.git mylinux