2021-05-12 14:32:11
Git克隆GitHub或GitLab上非Master分支程式碼的方法
今天想使用一下之前開源專案Blog_mini一個分支的程式碼,於是把整個專案克隆下來:
linuxidc@linuxidc:~/test$ git clone https://github.com/linuxidc/Blog_mini
正克隆到 'Blog_mini'...
remote: Counting objects: 1153, done.
remote: Total 1153 (delta 0), reused 0 (delta 0), pack-reused 1153
接收物件中: 100% (1153/1153), 2.18 MiB | 238.00 KiB/s, 完成.
處理 delta 中: 100% (673/673), 完成.
檢查連線... 完成。
然後檢視分支,發現竟然沒有之前建立的分支:
linuxidc@linuxidc:~/test/Blog_mini$ git branch
* master
這是因為git clone命令預設就是克隆倉庫中master的程式碼的,但是可以檢視遠端倉庫的分支:
linuxidc@linuxidc:~/test/Blog_mini$ git branch -r
origin/HEAD -> origin/master
origin/master
origin/version_1.0
linuxidc@linuxidc:~/test/Blog_mini$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master
remotes/origin/version_1.0
然後再使用git checkout命令切換到自己期望的分支上:
linuxidc@linuxidc:~/test/Blog_mini$ git checkout remotes/origin/version_1.0
Note: checking out 'remotes/origin/version_1.0'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b <new-branch-name>
HEAD 目前位於 2ff197d... update README.md
於是就可以看到目前已經在分支version_1.0上了,相應的程式碼也會克隆下來:
linuxidc@linuxidc:~/test/Blog_mini$ git branch
* (頭指標分離於 origin/version_1.0)
master
linuxidc@linuxidc:~/test/Blog_mini$ ls
app LICENSE migrations README.md requirements.txt
config.py manage.py Procfile requirements
當然上面是用Github的例子來說明,實際上使用Gitlab或者其它自己搭的Git伺服器也是可以的,因為使用的都是Git版本控制系統。
更多GitLab相關教學見以下內容:
Ubuntu 14.04下安裝GitLab指南 http://www.linuxidc.com/Linux/2015-12/126876.htm
如何在Ubuntu Server 14.04下安裝Gitlab中文版 http://www.linuxidc.com/Linux/2015-12/126875.htm
CentOS原始碼安裝GitLab漢化版 http://www.linuxidc.com/Linux/2015-10/124648.htm
CentOS 7下GitLab 9.1.0 安裝及漢化 http://www.linuxidc.com/Linux/2017-04/143240.htm
搭建屬於自己的GitLab http://www.linuxidc.com/Linux/2017-04/142665.htm
Ubuntu 14.04搭建GitLab伺服器 http://www.linuxidc.com/Linux/2017-02/140959.htm
CentOS 6.5安裝GitLab教學及相關問題解決 http://www.linuxidc.com/Linux/2014-05/101526.htm
升級GitLab到8.2.0 http://www.linuxidc.com/Linux/2015-12/126220.htm
相關文章