如何在Git中克隆单个分支? [英] How do I clone a single branch in Git?

查看:466
本文介绍了如何在Git中克隆单个分支?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 skeleton的本地Git存储库,用于存储项目框架。它有一些分支,可用于不同类型的项目:

I have a local Git repository called 'skeleton' that I use for storing project skeletons. It has a few branches, for different kinds of projects:

casey@agave [~/Projects/skeleton] git branch
* master
  rails
  c
  c++

如果我想要检查一个新项目的master分支,我可以做

If I want to check out the master branch for a new project, I can do

casey@agave [~/Projects] git clone skeleton new
Initialized empty Git repository in /Users/casey/Projects/new/.git/

中初始化了空的Git存储库一切都是我想要的。具体来说,新的master分支指向骨架master分支,我可以通过推拉操作来更改对基本项目设置的更改。

and everything is how I want it. Specifically, the new master branch points to the skeleton master branch, and I can push and pull to move around changes to the basic project setup.

什么是行不通的,但是,如果我想克隆另一个分支。我无法获取它,所以我只拉了我想要的分支,例如 rails 分支,然后新的存储库有了一个 master 分支,默认情况下可推入和拉出骨架存储库的 rails 分支。

What doesn't work, however, is if I want to clone another branch. I can't get it so that I only pull the branch I want, for instance the rails branch, and then the new repository has a master branch that pushes to and pulls from the skeleton repository's rails branch, by default.

有一个好的方法去做吗?或者,也许这不是Git想要我组织事物的方式,我当然对此持开放态度。也许我应该有多个存储库,而Ruby on Rails框架存储库跟踪主框架存储库?以及克隆Ruby on Rails骨架存储库的任何单个项目。

Is there a good way to go about doing this? Or, maybe this isn't the way that Git wants me to structure things, and I'm certainly open to that. Perhaps I should have multiple repositories, with the Ruby on Rails skeleton repository tracking the master skeleton repository? And any individual project cloning the Ruby on Rails skeleton repository.

推荐答案

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

Note: the git1.7.10 (April 2012) actually allows you to clone only one branch:

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

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

您可以在 t5500-fetch-pack.sh

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

东武(Tobu) 评论


在进行浅层克隆时这是隐式的。

这使得 git clone --depth 1 节省带宽的最简单方法。

This is implicit when doing a shallow clone.
This makes git clone --depth 1 the easiest way to save bandwidth.

自Git 1.9.0起(2014年2月),浅层克隆支持数据传输(推/拉),因此该选项现在更加有用。

更多信息,请参见<< a href = https://stackoverflow.com / a / 21217267/6309> git clone --depth 1 (浅层克隆)比它做的有用吗?。

And since Git 1.9.0 (February 2014), shallow clones support data transfer (push/pull), so that option is even more useful now.
See more at "Is git clone --depth 1 (shallow clone) more useful than it makes out?".

撤消浅克隆的详细信息请参见 将浅克隆转换为完整克隆。 (git 1.8.3 +)

"Undoing" a shallow clone is detailed at "Convert shallow clone to full clone" (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):

the magic line for getting missing branches to reverse --single-branch is (git v2.1.4):


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




使用Git 2.26(2020年第一季度)时, git clone --recurse-submodules --single-branch 现在在克隆子模块时使用相同的单分支选项

请参见提交132f600 >(2020年2月21日)通过艾米莉·谢弗( nasamuffin

(由 Junio C Hamano合并- gitster - commit b22db26 ,05中2020年3月)

See commit 132f600, commit 4731957 (21 Feb 2020) by Emily Shaffer (nasamuffin).
(Merged by Junio C Hamano -- gitster -- in commit b22db26, 05 Mar 2020)


clone :在--recurse-submodules

$期间传递--single-branch b $ b

签名人:Emily Shaffer

代理人:Jeff King

clone: pass --single-branch during --recurse-submodules

Signed-off-by: Emily Shaffer
Acked-by: Jeff King

以前,执行 git clone --recurse-submodules --single-branch 即使超级项目仅克隆了一个分支,结果还是导致子模块克隆了所有分支。

Previously, performing "git clone --recurse-submodules --single-branch" resulted in submodules cloning all branches even though the superproject cloned only one branch.

Pipe -单分支通过子模块帮助程序框架使其稍后再进行 克隆

Pipe --single-branch through the submodule helper framework to make it to 'clone' later on.

这篇关于如何在Git中克隆单个分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆