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

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

问题描述

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

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

如果我想为了检查一个新的项目的主分支,我可以做

$ pre code casey @ agave [〜/ Projects] git clone skeleton
在/Users/casey/Projects/new/.git/中初始化了空的Git仓库

而一切都是我想要的。具体来说,新的主分支指向骨架主分支,我可以推动和移动以改变基本的项目设置。



什么都行不通,但是,如果我想克隆另一个分支。我无法得到它,因此我只拖动了我想要的分支,例如rails分支,然后新的repo默认情况下有一个'master'分支,它会从骨架仓库的'rails'分支中推送和拉出。

有没有一个好的方法可以做到这一点?或者,也许这不是git想让我去组织结构的方式,我当然可以接受。也许我应该有多个回购,跟踪骨架回购跟踪主骨架回购?任何单独的项目都可以克隆Rails框架回购。



任何想法和建议都可以使用!

解决方案

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

 #only only远程主HEAD(默认:origin / master)
git clone --single-branch

作为:
git clone< url> - 分支<分支> --single-branch [< folder>]

你可以在 t5500-fetch-pack.sh

  test_expect_success'single branch clone''
git clone --single-branch文件:// $(PWD)/。 singlebranch
'

Tobu comment


在做浅层克隆时,这是隐含的。

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


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

更多信息,请参见 git clone --depth 1 (浅克隆)比它更有用吗? a>。






撤销浅层克隆详见将浅克隆转换为完整克隆(git 1.8.3 +)

 # unshallow当前分支
git fetch --unshallow

#用于取回所有分支(请参阅Peter Cordes的评论)
git config remote.origin.fetch refs / heads / *:refs / remotes / origin / *
git fetch --unshallow

As 克里斯评论:


魔线(git v2.1.4):



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


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++

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/

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

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 repo has a 'master' branch that pushes to and pulls from the skeleton repo's 'rails' branch, by default.

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 repos, with the rails skeleton repo tracking the master skeleton repo? And any individual project cloning the rails skeleton repo.

Any thoughts and suggestions are appreciated!

解决方案

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>]

You can see it in t5500-fetch-pack.sh:

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

Tobu comments that:

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

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?".


"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

As Chris comments:

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中克隆单个分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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