Git
简体中文 ▾ Topics ▾ Latest version ▾ git-clone last updated in 2.44.0

名称

git-clone - 将存储库克隆到新目录

概述

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] [--no-tags]
	  [--recurse-submodules[=<pathspec>]] [--[no-]shallow-submodules]
	  [--[no-]remote-submodules] [--jobs <n>] [--sparse]
	  [--filter=<filter>] [--] <repository>
	  [<directory>]

描述

将存储库克隆到新创建的目录中,为克隆存储库中的每个分支创建远程跟踪分支(使用`git branch --remotes`可见),并创建、签出从克隆存储库当前活动的分支派生的初始分支。

After the clone, a plain git fetch without arguments will update all the remote-tracking branches, and a git pull without arguments will in addition merge the remote master branch into the current master branch, if any (this is untrue when "--single-branch" is given; see below).

通过在 refs / remotes / origin 下创建对远程分支头的引用,并通过初始化 remote.origin.urlremote.origin.fetch 配置变量来创建此默认配置。

选项

-l
--local

当要从中进行克隆的存储库位于本地计算机上时,此标志会绕过常规的“Git感知”传输机制,并通过为 HEAD 以及对象和引用目录内的所有内容创建副本以克隆存储库。尽可能将 .git / objects / 目录下的文件进行硬链接以节省空间。

如果将存储库指定为本地路径(例如,/path/to/repo),--local 参数实质上是不起作用的。如果将存储库指定为 URL,则忽略此标志(不使用本地优化)。指定 --no-local 将在给定 /path/to/repo 时覆盖默认值,而使用常规 Git 传输方式。

从本地文件系统上的存储库进行克隆时,强制复制 .git / objects 目录下的文件,而不使用硬链接。如果您要备份存储库,则可能需要这样做。

-s
--shared

当要克隆的存储库位于本地计算机上时,不使用硬链接,而是自动设置 .git/objects/info/alternates 与源存储库共享对象。生成的存储库开始时没有其自己的任何对象。

注意:这可能是危险的操作;*不要*使用除非您了解它的功能。 如果您克隆您的存储库时使用此选项, 然后在源存储库中删除分支 (或使用任何其他使现有提交解除引用的 Git 命令), 某些对象可能会变得未引用(或悬空)。 这些对象可以通过常规的 Git 操作(例如 git commit)删除。 它会自动调用 git gc --auto。(请参阅linkgit:git-gc [1]。) 如果这些对象已删除并被克隆的存储库引用, 那么克隆的存储库将损坏。

请注意,在使用 --shared 选项克隆的存储库中运行不带 --local 选项的 git repack 命令会将源存储库中的对象复制到克隆存储库中的包中,从而消除使用 clone --shared 节省的磁盘空间。不过,运行 git-gc 是安全的,它默认使用 --local 选项。

如果要断开用 --shared 克隆的存储库对其源存储库的依赖关系,只需运行 git repack -a 即可将源存储库中的所有对象复制到克隆存储库中的一个包中。

--reference[-if-able] <存储库>

如果参考存储库位于本地计算机上,则自动设置 .git / objects / info / alternates 以从参考存储库获取对象。使用现有的存储库作为备用存储库,则将需要从要克隆的存储库中复制较少的对象,从而降低了网络和本地存储成本。当使用 --reference-if-able 时,不存在的目录会被警告跳过,而不是中止克隆。

注意:请参阅`--shared`选项的注释,以及 `--dissociate`选项。

--dissociate

从用 --reference 选项指定的参考存储库中借用对象仅是为了减少网络传输,并在克隆后通过制作借用对象的必要本地副本来停止从对象借用。当从已经从另一个存储库借用对象的存储库进行本地克隆时,也可以使用此选项:新存储库将从同一个存储库借用对象,并且该选项可用于停止借用。

-q
--quiet

Operate quietly. Progress is not reported to the standard error stream.

-v
--verbose

冗余运行。不影响向标准错误流报告进度状态。

--progress

默认情况下,附加到终端时,会将进度状态报告给标准错误流,除非指定了 --quiet 选项。即使标准错误流未定向到终端,该标志也会强制显示进度状态。

--server-option=<option>

使用协议版本2进行通信时,将给定的字符串传输到服务器。给定的字符串不得包含NUL或LF字符。服务器对服务器选项(包括未知选项)的处理是取决于服务器。当给出多个`--server-option = <option>`时,它们都按照命令行中列出的顺序发送到另一端。

-n
--no-checkout

No checkout of HEAD is performed after the clone is complete.

--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 --no-checkout 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.

--sparse

初始化稀疏检出文件,以便工作目录仅从存储库根目录中的文件开始。可以修改稀疏签出文件以根据需要扩展工作目录。

--filter=<过滤器定义>

Use the partial clone feature and request that the server sends a subset of reachable objects according to a given object filter. When using --filter, the supplied <filter-spec> is used for the partial clone filter. For example, --filter=blob:none will filter out all blobs (file contents) until needed by Git. Also, --filter=blob:limit=<size> will filter out all blobs of size at least <size>. For more details on filter specifications, see the --filter option in git-rev-list[1].

--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.

-o <名称>
--origin <名称>

不使用远程名称 origin 来跟踪上游存储库,而使用`<名称>`。

-b <名称>
--branch <名称>

不将新创建的 HEAD 指向克隆存储库的 HEAD 所指向的分支,而是指向`<名称>分支。在非裸仓库中,这是将被检出的分支。 `--branch 也可以使用标签并在生成的存储库中的提交时分离 HEAD。

-u <上传包>
--upload-pack <upload-pack>

如果指定了要克隆的存储库,且可以通过 ssh 访问该存储库,这将为另一端运行的命令指定非默认路径。

--template = <模板目录>

指定将使用模板的目录;(请参阅 git-init[1] 的“模板目录”部分。)

-c <键> = <值>
--config <键> = <值>

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.

由于当前实现方式的限制,某些配置变量要等到初始获取和签出后才能生效。已知不会生效的配置变量有:remote.<s名称> .mirror 和`remote.<名称> .tagOpt`。如要修改,应改用相应的 --mirror--no-tags 选项。

--depth <深度>

创建一个历史记录被截断为指定提交次数的“浅”克隆。暗含 --single-branch 选项,除非给出 --no-single-branch 来获取所有分支顶端附近的历史记录。如果要浅层克隆子模块,还可以使用 --shallow-submodules 选项。

--shallow-since=<date>

创建浅克隆,只包含指定的时间之后的历史记录。

--shallow-exclude=<revision>

创建一个有历史的浅克隆,不包含可以被指定远程分支或标签访问的提交。本选项可以使用多次。

--[no-]single-branch

仅克隆直到单一分支末尾的历史,该分支被 --branch 选项或主分支远程 HEAD 指定。被创建仓库之后的抓取将只更新在克隆时被指定的远程跟踪分支。如果在 --single-branch 克隆时,远程 HEAD 未指向任何分支,则不会创建远程跟踪分支。

--no-tags

不克隆任何标签,并在配置中设置 remote.<remote>.tagOpt=--no-tags ,确保将来的`git pull`和`git fetch`操作不会跟随任何标签。随后的显式标签抓取仍将起作用(参阅 linkgit:git-fetch [1])。

可以与 --single-branch 结合使用,以克隆和维护除单个克隆分支外没有其他引用的分支。这在例如维护某些仓库的默认分支的最小克隆以进行搜索索引时有用。

--recurse-submodules[=<pathspec>]

创建克隆后,根据提供的路径规范初始化并克隆其中的子模块。如果未提供路径规范,则将初始化并克隆所有子模块。对于由多个条目组成的路径规范,可以多次赋予此选项。生成的克隆将把 submodule.active 设为提供的路径规范。如果未提供路径规范,生成的克隆将把 submodule.active 设为 "." (表示所有子模块)。

子模块使用其默认设置进行初始化和克隆。这等效于克隆完成后立即运行 git submodule update --init --recursive <路径规范>。如果克隆的仓库没有工作区/检出(即给出了 --no-checkout/-n--bare 或 `--mirror`中的任何一个选项),则忽略此选项。

--[no-]shallow-submodules

所有克隆的子模块都将是浅克隆,深度为1。

--[no-]remote-submodules

克隆的所有子模块将使用子模块的远程跟踪分支的状态来更新子模块,而不是超级项目的已记录SHA-1。等同于将`--remote` 传递给 git submodule update

--separate-git-dir=<git目录>

不将克隆的存储库放置在原处,而是将克隆的存储库放置在指定的目录中,然后在此处建立与文件系统无关的Git符号链接。这可以将Git仓库与工作区分离。

-j <n>
--jobs <n>

同时抓取的子模块数。默认为 submodule.fetchJobs 选项。

<仓库>

要克隆的(可能是远程的)仓库。有关指定仓库的更多信息,参阅下面的 GIT URLS 部分。

<目录>

要克隆到的新目录的名称。如果未明确指定目录,则使用源仓库中的“人性化”部分(对于 /path/to/repo.git 而言,为 repo;对于 host.xz:foo/.git 而言,为 foo)。仅当目录为空时才允许克隆到现有目录。

GIT URLS

通常,URL 包含有关传输协议,远程服务器的地址以及存储库路径的信息。对于某些传输协议,一些信息可能会缺失。

Git 支持 ssh,git,http 和 https 协议(此外,可以使用 ftp 和 ftps 进行抓取,但这效率低下且不建议使用;请勿使用)。

本地传输(即 git:// 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 的语法:

  • [user@]host.xz:path/to/repo.git/

仅当第一个冒号之前没有斜杠时才能识别此语法。这有助于区分包含冒号的本地路径。例如,可以将本地路径 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 clonegit fetchgit pull,但不包括 git push,也将接受合适的捆绑文件。参见 git-bundle[1]

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

  • <传输方式>::<地址>

其中,<地址> 可以是路径,服务器与路径,也可以是可被调用的特定远程帮助程序识别的类似于 URL 的任意字符串。有关详细信息,请参阅 gitremote-helpers[7]

如果存在大量类似名称的远程存储库,并且您要为其使用不同的格式(这样,您使用的 URL 将被重写为有效的 URL),则可以创建以下形式的配置:

	[url "<actual url base>"]
		insteadOf = <other url base>

例如,有如下:

	[url "git://git.host.xz/"]
		insteadOf = host.xz:/path/to/
		insteadOf = work:

URL 诸如 "work:repo.git" 或 "host.xz:/path/to/repo.git" 会在任何 URL 类似于 "git://git.host.xz/repo" 的上下文中重写。

如果要重写仅用于推送的 URL,可以创建表单的配置部分:

	[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",用于推送。但拉取代码时仍然使用原始的 URL。

实例

  • 从上游克隆:

    $ 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
  • 创建一个裸仓库,以将您的更改发布给公众:

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

GIT

Part of the git[1] suite

scroll-to-top