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]
[--recurse-submodules] [--[no-]shallow-submodules]
[--jobs <n>] [--] <repository> [<directory>]
描述
將存儲庫克隆到新創建的目錄中,為克隆的存儲庫中的每個分支創建遠程跟蹤分支(使用git branch -r
可見),並從克隆檢出的存儲庫作為當前活動分支的初始分支。
在克隆之後,沒有參數的普通git提取將更新所有遠程跟蹤分支,並且沒有參數的git pull
將另外將遠程主分支合併到當前主分支(如果有的話)。
此默認配置通過在refs/remotes/origin
下創建對遠程分支頭的引用,並通過初始化remote.origin.url
和remote.origin.fetch
配置變數來實現。
執行遠程操作的第一步,通常是從遠程主機克隆一個版本庫,這時就要用到git clone
命令。
$ git clone <版本庫的網址>
比如,克隆jQuery的版本庫。
$ git clone http://github.com/jquery/jquery.git
該命令會在本地主機生成一個目錄,與遠程主機的版本庫同名。如果要指定不同的目錄名,可以將目錄名作為git clone
命令的第二個參數。
$ git clone <版本庫的網址> <本地目錄名>
git clone
支持多種協議,除了HTTP(s)以外,還支持SSH、Git、本地檔協議等,下麵是一些例子。
示例
以下是所支持協議的一些示例 -
$ git clone http[s]://example.com/path/to/repo.git
$ git clone http://git.oschina.net/zaixian/sample.git
$ git clone ssh://example.com/path/to/repo.git
$ git clone git://example.com/path/to/repo.git
$ git clone /opt/git/project.git
$ git clone file:///opt/git/project.git
$ git clone ftp[s]://example.com/path/to/repo.git
$ git clone rsync://example.com/path/to/repo.git
SSH協議還有另一種寫法。
$ git clone [user@]example.com:path/to/repo.git
通常來說,Git協議下載速度最快,SSH協議用於需要用戶認證的場合。
應用場景示例
從上游克隆下來:
$ git clone git://git.kernel.org/pub/scm/.../linux.git mydir
$ cd mydir
$ 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
mydir
$ cd mydir
創建一個裸存儲庫以將您的更改發佈給公眾:
$ git clone --bare -l /home/proj/.git /pub/scm/proj.git
上一篇:
git add命令
下一篇:
git status命令