git clone命令的作用是将存储库克隆到新目录中,为克隆的存储库中的每个分支创建远程跟踪分支(使用git branch -r可见),并从克隆检出的存储库作为当前活动分支的初始分支。
$ git clone <版本库的网址>
比如,克隆jQuery的版本库。
$ git clone http://github.com/jquery/jquery.git
该命令会在本地主机生成一个目录,与远程主机的版本库同名。如果要指定不同的目录名,可以将目录名作为git clone命令的第二个参数。
$ git clone <版本库的网址> <本地目录名>
git clone支持多种协议,除了HTTP(s)以外,还支持SSH、Git、本地文件协议等。
在默认情况下,Git会把"Git URL"里最后一级目录名的'.git'的后辍去掉,做为新克隆(clone)项目的目录名: (例如. git clone http://git.kernel.org/linux/kernel/git/torvalds/linux-2.6.git 会建立一个目录叫'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
SSH协议还有另一种写法。
$ git clone [user@]example.com:path/to/repo.git
通常来说,Git协议下载速度最快,SSH协议用于需要用户认证的场合。
从上游克隆下来:
$ git clone git://git.kernel.org/pub/scm/.../linux.git mydir $ cd mydir $ make # 执行代码或其它命令
在当前目录中使用克隆,而无需检出:
$ git clone -l -s -n . ../copy $ cd ../copy $ git show-branch
从现有本地目录借用从上游克隆:
$ git clone --reference /git/linux.git git://git.kernel.org/pub/scm/.../linux.git mydir $ cd mydir
创建一个裸存储库以将您的更改发布给公众:
$ git clone --bare -l /home/proj/.git /pub/scm/proj.git
拉取指定分支
git clone -b branchA http://xxx.xxx.xxx.git