Git clone 会将远程分支克隆到本地。
有没有什么方法可以自己克隆一个特定的分支,而无需在远程存储库上切换分支?
git clone -b <branch> <remote_repo>
例子:
git clone -b my-branch git@github.com:user/myproject.git
对于 Git 1.7.10 及更高版本,添加--single-branch
以防止获取所有分支。例如,使用 OpenCV 2.4 分支:
git clone -b opencv-2.4 --single-branch https://github.com/Itseez/opencv.git
git clone --single-branch --branch <branchname> <remote-repo>
该--single-branch
选项从1.7.10及更高版本开始有效。
另请参阅许多人喜欢的其他答案。
您可能还想确保您了解其中的区别。不同之处在于:通过调用git clone --branch <branchname> url
,您将获取所有分支并签出一个。例如,这可能意味着您的存储库有一个 5kB 的文档或 wiki 分支和 5GB 的数据分支。每当你想编辑你的首页时,你最终可能会克隆 5GB 的数据。
同样,这并不是说不是实现这一目标的方法,只是当您询问克隆特定分支时git clone --branch
,它并不总是您想要完成的。
这是一个非常简单的方法:)
克隆存储库
git clone <repository_url>
列出所有分支
git branch -a
签出您想要的分支
git checkout <name_of_branch>
要克隆一个分支而不获取其他分支:
mkdir $BRANCH
cd $BRANCH
git init
git remote add -t $BRANCH -f origin $REMOTE_REPO
git checkout $BRANCH
git checkout -b <branch-name> <origin/branch_name>
例如在我的情况下:
git branch -a
* master
origin/HEAD
origin/enum-account-number
origin/master
origin/rel_table_play
origin/sugarfield_customer_number_show_c
因此,要根据我的 enum-account-number 分支创建一个新分支,我会这样做:
git checkout -b enum-account-number origin/enum-account-number
点击返回后会发生以下情况:
Branch enum-account-number set up to track remote branch refs/remotes/origin/enum-account-number.
Switched to a new branch "enum-account-number"
使用该名称在本地系统上创建一个分支。例如说你想让分支命名branch-05142011
git branch branch-05142011 origin/branch-05142011
它会给你一个消息:
$ git checkout --track origin/branch-05142011
Branch branch-05142011 set up to track remote branch refs/remotes/origin/branch-05142011.
Switched to a new branch "branch-05142011"
现在只需检查下面的分支,您就有了代码
git checkout branch-05142011
git --branch <branchname> <url>
但是 bash 完成没有得到这个键:--branch