Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Git clone command usage


May 25, 2021 Git



git clone is a command commonly used in git to clone the repository into a new directory. So in git, how exactly does git clone work?


To learn Git faster and easier, click Git Microseed


The role of the git clone command is to clone the repository into the new directory, create a remote trace branch for each branch of the cloned repository (visible using git branch -r), and check out the repository from the clone as the initial branch of the current active branch.

After cloning, normal git extracts without parameters update all remote trace branches, and git pull without parameters merges the remote main branch separately into the current main branch, if any.

This default configuration is implemented by creating a reference to the remote branch header under refs/remotes/origin and by initializing the configuration variables of remote.origin.url and remote.origin.fetch.

The first step in performing a remote operation is usually to clone a repository from the remote host, at which point the git clone command is used.

$ git clone <版本库的网址>

For example, clone the repository of jQuery.

$ git clone http://github.com/jquery/jquery.git

The command generates a directory on the local host with the same name as the repository of the remote host. I f you want to specify a different directory name, you can use the directory name as the second parameter of the git clone command.

$ git clone <版本库的网址> <本地目录名>

git clone supports a variety of protocols, in addition to HTTP(s), SSH, Git, local file protocols, and so on.

By default, Git removes the '.git' of the last level directory name in the Git URL as the directory name for the new clone project: (for example, git clone http://git.kernel.org/linux/kernel/git/torvalds/linux-2.6.git establishes a directory called 'linux-2.6')

$ git clone http[s]://example.com/path/to/repo.git
$ git clone http://git.oschina.net/yiibai/sample.git
$ git clone ssh://example.com/path/to/repo.git
$ git clone git://example.com/path/to/repo.git
$ git clone /opt/git/project.git 
$ git clone file:///opt/git/project.git
$ git clone ftp[s]://example.com/path/to/repo.git
$ git clone rsync://example.com/path/to/repo.git


The SSH protocol has another way of writing.

$ git clone [user@]example.com:path/to/repo.git

In general, the Git protocol is the fastest to download, and the SSH protocol is used in situations where user authentication is required.


Example of a scenario

Clone from upstream:

$ git clone git://git.kernel.org/pub/scm/.../linux.git mydir
$ cd mydir
$ make # 执行代码或其它命令

Use clones in the current directory without checking out:

$ git clone -l -s -n . ../copy
$ cd ../copy
$ git show-branch

Borrowing from an existing local directory from an upstream clone:

$ git clone --reference /git/linux.git 
    git://git.kernel.org/pub/scm/.../linux.git 
    mydir
$ cd mydir

Create a bare repository to publish your changes to the public:

$ git clone --bare -l /home/proj/.git /pub/scm/proj.git

These are some common uses of Git clone commands, and I hope they will help you. To learn more about the use of Git commands, click on: Git Common Command Quick Check table