我在~/local_repo有一个本地Git存储库。它有几个分支:

$ git branch
* master
  rails
  c
  c++

要克隆本地存储库,我做:

$ git clone ~/local_repo new_repo
Initialized empty Git repository in /home/username/new_repo/.git/

new_repo master分支指向local_repo master分支,我可以推/拉。

但我无法克隆另一个分支。我只想拉我想要的分支(例如rails),这样新的存储库就有一个主分支,默认情况下,它可以推入和拉出local_repo的rails分支。我如何实现这一点,或者也许类似于local_repo跟踪主local_repo?


当前回答

git clone --branch {branch-name} {repo-URI}

例子:

git clone --branch dev https://github.com/ann/cleaningmachine.git

dev:这是{branch-name} https://github.com/ann/cleaningmachine.git:这是{repo-URI}

其他回答

可以在2步内完成吗

克隆存储库 Git克隆<http url> 签出您想要的分支 git checkout $BranchName

打开cmd。

cd folder_name  # enter the path where to clone the branch

只有一个命令:

git clone url_of_projecturltoclone -b branch_name

对于像我这样的新手来说, 只需运行下面的代码

git clone https://gitlab.com/repo/repo.git --branch <name of branch> --single-branch

为了克隆一个特定的分支,你可以做:

git clone --branch yourBranchName git@yourRepository.git

类似于@nosaiba-darwish在这里说的:这里

我们公司通常是这样做的:

git clone -b <name_of_branch> --single-branch <git_url> folder_to_clone_locally