Description

The git clone is a git command, which creates a clone/copy of an existing repository into a new directory. It is also used to create remote-tracking branches for each branch in the cloned repository. It is the most common command which allows users to obtain a development copy of an existing central repository.

Gitclone

The git clone usage

First of all, the git clone command is used to target an existing repository and clone or copy it in a new directory. A local copy stored on a server, that is accessible at example.comcan be obtained using the SSH username x_person , like this:

git clone ssh://[email protected]/path/to/team-project.git
cd team-project
# You must work on this project

The git clone initializes a new Git repository in the team-project folder on your local machine and fills it with the contents of the central repository. After that, you can cd into the project starting modification of files, commitment of snapshots, and interaction with other repositories.

Cloning to a certain folder

You should make a clone of the repository at <repo> into the folder called <directory> on the local machine.

git clone <repo> <directory>

Cloning a certain tag

Clone the repository at <repo> and clone only the ref for <tag>.

git clone --branch <tag> <repo>

Difference Between git init and git clone

The git init and git clone are usually confused with each other. Here it’s important to note that git clone is dependant on the git init and creates a copy of a repository that already exists. In other words, for generating a git clone, we need a repository created with git init. Only after that, we run a git clone to copy the data that is included in our repository mentioned above.

Configuration Options of Git Clone

Configuration options are the tools you need for making Git work the way that suits best to you or your team. The most common ones are presented below:

  • git clone -branch

    The -branch argument specifies a branch which should be cloned instead of the one the remote HEAD is indicating to, usually the master branch. Besides, you can pass a tag instead of a branch for the same effect. Using the example below, you will have a clone of the new_feature branch from the remote repo:

    git clone -branch new_feature git://remoterepository.git
  • git clone --bare

    With the --bare argument passed to git clone, you will have a copy of the remote repo created with an excluded working directory. So, the repository will be created with the project history which can be pushed or pulled from but cannot be edited.

  • git clone --mirror

    The behavior of the --bare argument is inherited by the --mirror, which means that passing the --mirror argument will also pass the --bare. The --mirror argument will clone all the extended refs of the remote repo. It will also allow you to maintain a remote branch tracking configuration.

Git URLs

Git has its own URL syntax. It is used for transferring remote repository locations to Git commands. Git URLs are important because git clone is mostly used on remote repositories.

Git URL protocols

Git can use the following protocols for data transfer: Git , Secure Shell (SSH) , HTTPS and Local. It should be noted that all these protocols require Git to be installed and working on the server except for HTTPS protocols.

  • Git

    The Git protocol is unique to Git. It is a special daemon that runs on port (9418) providing a service similar to SSH but without any authentication.

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

    The advantage of the Git protocol is a fast transfer. The Git protocol has also some disadvantages: the absence of authentication and difficulty of the protocol configuration.

  • SSH

    Secure Shell (SSH) is a network protocol, which helps to login from one computer to another securely. In most cases, SSH access to servers is configured by default. It’s necessary to establish credentials with the hosting server before connecting.

    git clone ssh://user@server/project.git

    SSH is the only network protocol, which can be easily read from and write to. SSH has many other advantages such as easy configuration, secure access (all data transfer is encrypted and authenticated), and data compactness before its transfer. The disadvantage of SSH is that it doesn’t support anonymous access to the Git repository. You can find more information about SSH keys on our SSH key section.

  • HTTPS

    HTTPS stands for HyperText Transfer Protocol. This protocol is mostly used to transmit HTML data above the Internet. Git is configured to share information with HTTPS.

    git clone http://example.com/gitproject.git

    One of the advantages of HTTPS is set up simplicity. It doesn’t require a lot of resources on the server as well. It encrypts the content transfer. Corporate firewalls are set up to allow traffic through the port thus making HTTPS one of the commonly used protocols.

    The main disadvantage is inefficiency for the client because it takes a lot of time to clone or fetch from the repository.

  • Local

    Local is a basic protocol in which the remote repository is in another directory on disk. It is used when all the team members have access to a shared file system. Shared file system allows you to clone, push to, and pull from a local repository. The path to the repository can be used as the URL for cloning a repository like this to an existing project.

    git clone /opt/git/project.git

Practice Your Knowledge

What is the primary purpose of the 'git clone' command in Git?

Quiz Time: Test Your Skills!

Ready to challenge what you've learned? Dive into our interactive quizzes for a deeper understanding and a fun way to reinforce your knowledge.