首頁·終端機命令

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