「git-clone」

  LINUX MANUAL PAGES

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.

–quiet, -q
Operate quietly. Progress is not reported to the standard error stream.

–verbose, -v
Run verbosely. Does not affect the reporting of progress status to the standard error stream.

–progress
Progress status is reported on the standard error stream by default when it is attached to a terminal,
unless -q is specified. This flag forces progress status even if the standard error stream is not
directed to a terminal.

–no-checkout, -n
当clone完成后,不执行HEAD的检出。

–bare
Make a bare Git repository. That is, instead of creating <directory> and placing the administrative files
in <directory>/.git, make the <directory> itself the $GIT_DIR. This obviously implies the -n because
there is nowhere to check out the working tree. Also the branch heads at the remote are copied directly
to corresponding local branch heads, without mapping them to refs/remotes/origin/. When this option is
used, neither remote-tracking branches nor the related configuration variables are created.

–mirror
Set up a mirror of the source repository. This implies –bare. Compared to –bare, –mirror not only maps
local branches of the source to local branches of the target, it maps all refs (including remote-tracking
branches, notes etc.) and sets up a refspec configuration such that all these refs are overwritten by a
git remote update in the target repository.

–origin <name>, -o <name>
Instead of using the remote name origin to keep track of the upstream repository, use <name>.

–branch <name>, -b <name>
Instead of pointing the newly created HEAD to the branch pointed to by the cloned repository’s HEAD,
point to <name> branch instead. In a non-bare repository, this is the branch that will be checked out.
–branch can also take tags and detaches the HEAD at that commit in the resulting repository.

–upload-pack <upload-pack>, -u <upload-pack>
When given, and the repository to clone from is accessed via ssh, this specifies a non-default path for
the command run on the other end.

–template=<template_directory>
Specify the directory from which templates will be used; (See the “TEMPLATE DIRECTORY” section of git-
init(1).)

–config <key>=<value>, -c <key>=<value>
Set a configuration variable in the newly-created repository; this takes effect immediately after the
repository is initialized, but before the remote history is fetched or any files checked out. The key is
in the same format as expected by git-config(1) (e.g., core.eol=true). If multiple values are given for
the same key, each value will be written to the config file. This makes it safe, for example, to add
additional fetch refspecs to the origin remote.

–depth <depth>
Create a shallow clone with a history truncated to the specified number of commits. Implies
–single-branch unless –no-single-branch is given to fetch the histories near the tips of all branches.
If you want to clone submodules shallowly, also pass –shallow-submodules.

–shallow-since=<date>
Create a shallow clone with a history after the specified time.

–shallow-exclude=<revision>
Create a shallow clone with a history, excluding commits reachable from a specified remote branch or tag.
This option can be specified multiple times.

–[no-]single-branch
Clone only the history leading to the tip of a single branch, either specified by the –branch option or
the primary branch remote’s HEAD points at. Further fetches into the resulting repository will only
update the remote-tracking branch for the branch this option was used for the initial cloning. If the
HEAD at the remote did not point at any branch when –single-branch clone was made, no remote-tracking
branch is created.

–recurse-submodules[=<pathspec]
After the clone is created, initialize and clone submodules within based on the provided pathspec. If no
pathspec is provided, all submodules are initialized and cloned. Submodules are initialized and cloned
using their default settings. The resulting clone has submodule.active set to the provided pathspec, or
“.” (meaning all submodules) if no pathspec is provided. This is equivalent to running git submodule
update –init –recursive immediately after the clone is finished. This option is ignored if the cloned
repository does not have a worktree/checkout (i.e. if any of –no-checkout/-n, –bare, or –mirror is
given)

–[no-]shallow-submodules
All submodules which are cloned will be shallow with a depth of 1.

–separate-git-dir=<git dir>
Instead of placing the cloned repository where it is supposed to be, place the cloned repository at the
specified directory, then make a filesystem-agnostic Git symbolic link to there. The result is Git
repository can be separated from working tree.

-j <n>, –jobs <n>
The number of submodules fetched at the same time. Defaults to the submodule.fetchJobs option.

<repository>
The (possibly remote) repository to clone from. See the URLS section below for more information on
specifying repositories.

<directory>
The name of a new directory to clone into. The “humanish” part of the source repository is used if no
directory is explicitly given (repo for /path/to/repo.git and foo for host.xz:foo/.git). Cloning into an
existing directory is only allowed if the directory is empty.

GIT中的URL(GIT URLS)

一般来说,URL包含有关传输协议、远程服务器的地址、仓库的路径的信息。根据传输协议,有些信息可能不存在。

Git支持ssh,git,http和https协议(此外,ftp和ftps也可以,但是效率低,不推荐使用)

原始的传输(即git://URL)不进行身份验证,在不安全的网络上应谨慎使用。

支持的几种URL格式如下:

对于SSH协议,还有一种替代的SCP风格的URL:

只有在第一个冒号之前没有斜杠,才会识别此语法。这有助于区分包含冒号的本地路径。例如,可以将本地路径foo:bar指定为绝对路径或./foo:bar,以避免被误解为ssh URL。

ssh和git协议还支持~username扩展:

对于本地仓库,Git也支持,可以使用以下语法:

这两个语法大多是等价的,前者还暗含了–local选项。

当Git不知道如何处理某个传输协议时,它会尝试使用remote-<transport>远程帮助程序(如果存在)。要显式请求远程助手,可以使用以下语法:

<transport>::<address>

其中<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/

像”git://example.org/path/to/repo.git“的URL将被重写为”ssh://example.org/path/to/repo.git“,仅用于push,而pull依旧使用原有的URL。

简单示例(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

参考文献