git-clone,将一个仓库克隆到新的目录。
命令行语法格式(SYNOPSIS)
git clone [--template=<template_directory>] [-l] [-s] [--no-hardlinks] [-q] [-n] [--bare] [--mirror] [-o <name>] [-b <name>] [-u <upload-pack>] [--reference <repository>] [--dissociate] [--separate-git-dir <git dir>] [--depth <depth>] [--[no-]single-branch] [--recurse-submodules] [--[no-]shallow-submodules] [--jobs <n>] [--] <repository> [<directory>]
命令简述(DESCRIPTION)
将仓库克隆到新创建的目录中,为仓库中克隆的每个分支创建远程跟踪分支(使用git branch -r查看),然后创建并检出初始分支,该初始分支是所克隆的仓库中的当前活动的分支。
在克隆之后,没有参数的普通git fetch将更新所有远程跟踪分支,而没有参数的git pull将另外将远程master分支合并到当前master分支(如果有的话)(当指定“–single-branch“另说,见下文)。
此默认配置通过在refs/remotes/origin下创建对远程分支头的引用,并通过初始化remote.origin.url和remote.origin.fetch配置变量来实现。
命令支持的选项及含义(OPTIONS)
–local, -l
当克隆的仓库在本地机器上时,-l使git绕过正常的“Git aware”传输机制,并通过创建「HEAD副本」和「对象和引用目录下的所有内容」来克隆仓库。.git/objects/目录下的文件是硬链接的,以尽可能节省空间。
如果仓库被指定为本地路径(例如,/path/to/repo),则该选项是默认值,而–local本质上是一个no-op。如果仓库被指定为URL,则-l被忽略(我们从不使用本地优化)。如果给出了/path/to/repo,指定–no-local将覆盖默认值,使用正常的Git传输。
–no-hardlinks
强制从本地文件系统上的仓库复制.git/objects目录下的文件,而不是使用硬链接。
如果尝试备份仓库,可以这么做。
–shared, -s
当克隆的仓库在本地机器上时,而不是使用硬链接,自动设置.git/objects/info/alternate,以便与源仓库共享对象。生成的仓库开始没有任何自己的对象。
注意:这是一个可能危险的操作;不要使用它,除非你明白它的作用。如果使用此选项克隆仓库,然后在源仓库中删除分支(或使用可以使任何现有提交变成未引用的其他Git命令),则某些对象可能会变成未被引用(或悬挂)。这些对象可以通过正常的Git操作(例如git commit)被删除,它会自动调用git -auto。(参见git-gc(1))。如果这些对象被删除并被克隆的仓库引用,则克隆的仓库将被破坏。
注意,在使用-s克隆的仓库中,运行没有-l选项的git repack将对象从源仓库复制到克隆的仓库中的包中,从而消除了克隆-s的磁盘空间节省。但是,默认情况下使用-l选项,运行git gc是安全的。
如果要破坏在其源仓库上使用-s克隆的仓库的依赖关系,则可以简单地运行git repack -a将所有对象从源仓库复制到克隆的仓库中的包中。
–reference[-if-able] <repository>
如果<repository>仓库在本地机器上,则自动设置.git/objects/info/alternate来从<repository>仓库中获取对象。
将「已经存在的仓库」用作「备用库」,会减少需要从要克隆的仓库中复制的对象,从而降低网络和本地存储成本。
当使用–reference-if-able时,会跳过一个不存在的目录,而不是中止该clone。
注意:参阅–shared及–dissociate的注意事项。
–dissociate
Borrow the objects from reference repositories specified with the –reference options only to reduce network transfer, and stop borrowing from them after a clone is made by making necessary local copies of borrowed objects. This option can also be used when cloning locally from a repository that already borrows objects from another repository—the new repository will borrow objects from the same repository, and this option can be used to stop the borrowing.
–no-checkout, -n
当clone完成后,不执行HEAD的检出。
GIT中的URL(GIT URLS)
一般来说,URL包含有关传输协议、远程服务器的地址、仓库的路径的信息。根据传输协议,有些信息可能不存在。
Git支持ssh,git,http和https协议(此外,ftp和ftps也可以,但是效率低,不推荐使用)
原始的传输(即git://URL)不进行身份验证,在不安全的网络上应谨慎使用。
支持的几种URL格式如下:
- ssh://[user@]host.xz[:port]/path/to/repo.git/
- git://host.xz[:port]/path/to/repo.git/
- http[s]://host.xz[:port]/path/to/repo.git/
- ftp[s]://host.xz[:port]/path/to/repo.git/
对于SSH协议,还有一种替代的SCP风格的URL:
只有在第一个冒号之前没有斜杠,才会识别此语法。这有助于区分包含冒号的本地路径。例如,可以将本地路径foo:bar指定为绝对路径或./foo:bar,以避免被误解为ssh URL。
ssh和git协议还支持~username扩展:
- ssh://[user@]host.xz[:port]/~[user]/path/to/repo.git/
- git://host.xz[:port]/~[user]/path/to/repo.git/
- [user@]host.xz:/~[user]/path/to/repo.git/
对于本地仓库,Git也支持,可以使用以下语法:
- /path/to/repo.git/
- file:///path/to/repo.git/
这两个语法大多是等价的,前者还暗含了–local选项。
当Git不知道如何处理某个传输协议时,它会尝试使用remote-<transport>远程帮助程序(如果存在)。要显式请求远程助手,可以使用以下语法:
其中<address>可能是被调用的特定远程助手所识别的路径、服务器和路径、任意类似URL的字符串。有关详细信息,请参阅gitremote-helpers(1)。
如果有大量类似命名的远程仓库,并且想要为其使用不同的格式(以便使用的URL将被重写到可以工作的URL),则可以创建如下形式的配置部分:
[url "<actual url base>"] insteadOf = <other url base>
例如:
[url "git://git.host.xz/"] insteadOf = host.xz:/path/to/ insteadOf = work:
像“work:repo.git”或“host.xz:/path/to/repo.git”的URL将被重写为“git://git.host.xz/repo.git”
如果创建的URL只想用于push操作可以这么写:
[url "<actual url base>"] pushInsteadOf = <other url base>
例如:
[url "ssh://example.org/"] pushInsteadOf = git://example.org/
简单示例(EXAMPLES)
从上游克隆
$ git clone git://git.kernel.org/pub/scm/linux.git my-linux
$ cd my-linux
$ 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 my-linux
$ cd my-linux
创建一个裸仓库(proj.git),将更改发布给公众
$ git clone –bare -l /home/proj.git /pub/scm/proj.git
GIT
Part of the git(1) suite
参考文献
- man 1 git-clone, version Git 2.13.2
- Git Documentation: https://git-scm.com/docs/git-clone