我在~/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?


当前回答

主要有两种解决方案:

You need to specify the branch name with -b command switch. Here is the syntax of the command to clone the specific git branch. git clone -b <BRANCH_NAME> <GIT_REMOTE_URL> Example: git clone -b tahir https://github.com/Repository/Project.git The following command will clone the branch tahir from the git repository.The above command clones only the specific branch but fetches the details of other branches. You can view all branches details with command. git branch -a You can use --single-branch flag to prevent fetching details of other branches like below: git clone -b <BRANCH_NAME> --single-branch <GIT_REMOTE_URL> Example: git clone -b tahir --single-branch \ https://github.com/Repository/Project.git Now if you do a git branch -a, it will only show your current single branch that you have cloned and not all the branches. So it depends on you how you want it.

其他回答

使用Git版本1.7.3.1(在Windows上),下面是我所做的($BRANCH是我想要签出的分支的名称,$REMOTE_REPO是我想要克隆的远程存储库的URL):

mkdir $BRANCH
cd $BRANCH
git init
git remote add -t $BRANCH -f origin $REMOTE_REPO
git checkout $BRANCH

这种方法的优点是后续的git pull(或git fetch)调用也会下载所请求的分支。

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

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

可以在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

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

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

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