我在~/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单个分支和重命名文件夹,如果你想保持分支独立

git clone -b [branch-name] --single-branch [url]  [folder_name]

例子

git clone -b mybranch --single-branch git://github/repository.git  project_mybranch

其他回答

这应该可以

git clone --single-branch <branchname> <remote-repo-url>
git clone --branch <branchname> <remote-repo-url>

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

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

一种方法是执行以下操作。

git clone user@git-server:project_name.git -b branch_name /your/folder

其中branch_name是您选择的分支,“/your/folder”是该分支的目标文件夹。这确实会带来其他分支,让你有机会来回合并。

更新

现在,从Git 1.7.10开始,您就可以这样做了

git clone user@git-server:project_name.git -b branch_name --single-branch /your/folder
git clone <url> --branch <branch> --single-branch

只需输入URL和分支名称。

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

git clone --branch yourBranchName git@yourRepository.git