git clone是git中常用的命令,其作用是將存儲庫克隆到新目錄中。那么在git中,git clone具體該如何用呢?
git clone命令的作用是將存儲庫克隆到新目錄中,為克隆的存儲庫中的每個分支創建遠程跟蹤分支(使用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會把"Git URL"里最后一級目錄名的'.git'的后輟去掉,做為新克隆(clone)項目的目錄名: (例如. git clone http://git.kernel.org/linux/kernel/git/torvalds/linux-2.6.git 會建立一個目錄叫'linux-2.6')
$ git clone http[s]://example.com/path/to/repo.git
$ git clone http://git.oschina.net/yiibai/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 clone命令的一些常見用法,希望對大家有所幫助。想要了解更多Git命令的用法,可點擊:Git常用命令速查表。