首页·终端命令

git clone

git

用法

git clone [--depth <n>] [-b <branch>] <url> [<directory>]

把远端仓库连历史一起复制下来;--depth 1 的浅克隆没有旧提交和别的分支,之后得用 --unshallow 补齐。

常用选项

选项含义
--depth <n>shallow copy with only the last n commits
-b <branch>check out that branch instead of the default
--single-branchfetch one branch only
--recurse-submodulesclone submodules at the same time
--filter=blob:nonepartial clone: fetch file contents on demand
-o <name>call the remote something other than origin

示例

git clone https://github.com/user/repo.git

copies the repository into repo/ and names the remote origin

git clone --depth 1 https://github.com/user/repo.git

fast copy for CI with no old history

git clone -b dev --single-branch <url>

checks out dev and fetches nothing else

难的地方在于撤销有好几种:把分支的指向挪走,和补一个反向提交,不是一回事。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. git clone 是做什么的?

把远端仓库连历史一起复制下来;--depth 1 的浅克隆没有旧提交和别的分支,之后得用 --unshallow 补齐。

Q. 怎么写?

git clone [--depth <n>] [-b <branch>] <url> [<directory>] —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 6 个,完整列表在 man git。这条命令属于git。

相关命令

man 手册: man git-clone