我在~/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 <branchname> <remote-repo-url>

or

git clone -b <branchname> <remote-repo-url>

其他回答

要克隆一个你没有公钥的Git分支,使用这个:

git clone -b <branch> <Git repository URL or clone URL you get from Git repository>

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

git clone --branch yourBranchName git@yourRepository.git

只克隆一个分支。这是最简单的方法:

git clone -b BRANCH_NAME --single-branch git@bitbucket.org:___/PROJECTNAME.git
git clone <url> --branch <branch> --single-branch

只需输入URL和分支名称。

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

or

git clone -b <branchname> <remote-repo-url>