問題描述
我們每次使用命令
git clone git@gitlab.xxx.com:xxxxx.git
默認 clone 的是這個倉庫的 master 分支。如果最新的代碼不在 master 分支上,該如何拿到呢?如下圖所示,最新的代碼可能在daily/1.4.1
分支上,我們希望拿到這個分支上的代碼。
或者直接從 分支clone
- git clone -b my-branch https://git@github.com/username/myproject.git
解決方法
剛剛開周會的時候,自己洋洋得意的分享我的解決方案,但是……經過與團隊成員的的討論,自己的方法弱爆了,現在把更優雅的方法寫一下。原來寫的方法並不太適合用在這個場景里。 我之前寫的方法在文章后面。
直接使用命令
git branch -r #查看遠程分支
或
git branch -a #查看所有分支
會顯示
origin/HEAD -> origin/master origin/daily/1.2.2 origin/daily/1.3.0 origin/daily/1.4.1 origin/develop origin/feature/daily-1.0.0 origin/master
然后直接
git checkout origin/daily/1.4.1
就好了。。。
參照:https://gaohaoyang.github.io/2016/07/07/git-clone-not-master-branch/