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

其他回答

注意:git1.7.10(2012年4月)实际上只允许你克隆一个分支:

# clone only the remote primary HEAD (default: origin/master)
git clone <url> --single-branch

# as in:
git clone <url> --branch <branch> --single-branch <folder>

注意:

<url>是远程存储库的url,不引用克隆的分支 <folder>是要克隆存储库的本地文件夹

你可以在t5500-fetch-pack.sh中看到:

test_expect_success 'single branch clone' '
  git clone --single-branch "file://$(pwd)/." singlebranch
'

Tobu评论说:

这在做浅克隆时是隐式的。 这使得git clone—depth 1成为节省带宽的最简单方法。

从Git 1.9.0(2014年2月)开始,浅克隆支持数据传输(推/拉),所以这个选项现在更有用了。 详见“git克隆——深度1(浅克隆)比它所做的更有用吗?”。


“撤销”浅克隆的详细信息见“将浅克隆转换为完整克隆”(git 1.8.3+)

# unshallow the current branch
git fetch --unshallow

# for getting back all the branches (see Peter Cordes' comment)
git config remote.origin.fetch refs/heads/*:refs/remotes/origin/*
git fetch --unshallow

正如克里斯评论的那样:

将缺失的分支反向转换为单个分支的魔法线是(git v2.1.4):

git config remote.origin.fetch +refs/heads/*:refs/remotes/origin/*
git fetch --unshallow  

在Git 2.26 (Q1 2020)中,“Git克隆-递归-子模块-单分支”现在在克隆子模块时使用相同的单分支选项。

参见Emily Shaffer (nasamuffin)的commit 132f600, commit 4731957(2020年2月21日)。 (由Junio C Hamano合并- gitster - in commit b22db26, 05 Mar 2020)

克隆:在——递归子模块期间传递——单分支 署名:Emily Shaffer 获奖嘉宾:杰夫·金 以前,执行“git克隆—递归—子模块—单分支”会导致子模块克隆所有分支,即使超级项目只克隆了一个分支。 管道——通过子模块帮助框架的单个分支,使其稍后“克隆”。

如果你想要一个浅克隆,你可以这样做:

git clone -b mybranch --depth=1 https://example.com/myproject.git localname

——depth=1表示——single-branch。

你可以使用下面的命令:

git clone -b branch_name --single-branch project_url local_folder_to_clone_in
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}

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

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