如何在 Git 中克隆单个分支?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1778088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 07:23:04  来源:igfitidea点击:

How do I clone a single branch in Git?

gitbranchgit-clone

提问by Casey Rodarmor

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:

我有一个名为“骨架”的本地 Git 存储库,用于存储项目骨架。它有几个分支,用于不同类型的项目:

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

如果我想查看新项目的 master 分支,我可以这样做

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 the skeleton master branch, and I can push and pull to move around changes to the basic project setup.

一切都是我想要的。具体来说,新的 master 分支指向主干分支,我可以推拉来移动对基本项目设置的更改。

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 railsbranch, and then the new repository has a masterbranch that pushes to and pulls from the skeleton repository's railsbranch, by default.

但是,不起作用的是,如果我想克隆另一个分支。我无法得到它,所以我只拉取我想要的rails分支,例如分支,然后新存储库有一个master分支,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.

有什么好的方法可以做到这一点吗?或者,也许这不是 Git 希望我构建事物的方式,我当然对此持开放态度。也许我应该有多个存储库,用 Ruby on Rails 骨架存储库跟踪主骨架存储库?以及克隆 Ruby on Rails 骨架存储库的任何单个项目。

回答by VonC

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

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

# 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:

你可以在t5500-fetch-pack.sh

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

Tobucommentsthat:

东武评论说:

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

这在进行浅克隆时是隐含的。
这是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.9.0(2014 年 2 月)开始,浅克隆支持数据传输(推/拉),因此该选项现在更加有用。
请参阅“ (浅层克隆)是否git clone --depth 1比它看起来更有用?”。



"Undoing" a shallow clone is detailed at "Convert shallow clone to full clone" (git 1.8.3+)

“撤消”浅克隆在“将浅克隆转换为完整克隆”(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 Chriscomments:

正如克里斯评论的那样:

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

使丢失的分支反转的魔术线--single-branch是(git v2.1.4):

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


With Git 2.26 (Q1 2020), "git clone --recurse-submodules --single-branch" now uses the same single-branch option when cloning the submodules.

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

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

请参阅Emily Shaffer ( ) 的commit 132f600commit 4731957(2020 年 2 月 21 日(由Junio C Hamano合并-- --提交 b22db26 中,2020 年 3 月 5 日)nasamuffin
gitster

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

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

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

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

clone: 在 --recurse-submodules 期间通过 --single-branch

签字人:Emily Shaffer
认可人:Jeff King

以前,git clone --recurse-submodules --single-branch即使超级项目只克隆了一个分支,执行 " " 也会导致子模块克隆所有分支。

--single-branch通过子模块帮助程序框架进行管道传输,以使其clone稍后成为“ ”。

回答by Alex Nolasco

One way is to execute the following.

一种方法是执行以下操作。

git clone user@git-server:project_name.git -b branch_name /your/folder

Where branch_nameis the branch of your choice and "/your/folder" is the destination folder for that branch. It's true that this will bring other branches giving you the opportunity to merge back and forth.

branch_name您选择的分支在哪里,“/your/folder”是该分支的目标文件夹。确实,这会带来其他分支,让您有机会来回合并。

Update

更新

Now, starting with Git 1.7.10, you can now do this

现在,从 Git 1.7.10 开始,您现在可以执行此操作

git clone user@git-server:project_name.git -b branch_name --single-branch /your/folder

回答by Frerich Raabe

Using Git version 1.7.3.1 (on Windows), here's what I do ($BRANCHis the name of the branch I want to checkout and $REMOTE_REPOis the URL of the remote repository I want to clone from):

使用 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

The advantage of this approach is that subsequent git pull(or git fetch) calls will also just download the requested branch.

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

回答by jkp

You can try the long-winded way:

您可以尝试冗长的方式:

mkdir newrepo.git
cd newrepo.git
git init
git remote add origin file:///path/to/original
git fetch origin branchiwant:refs/remotes/origin/branchiwant
git checkout -b branchiwant --track origin/branchiwant

What this does is:

它的作用是:

  • Create and init an empty Git repository.
  • Adds the original repository as a remote called origin.
  • Fetches only the branch you require from the remote called origin.
  • Creates and checks out a new branch that is set up to track the source branch you just cloned.
  • 创建并初始化一个空的 Git 存储库。
  • 将原始存储库添加为名为origin的远程存储库。
  • 仅从名为origin的远程获取您需要的分支。
  • 创建并签出一个新分支,该分支被设置为跟踪您刚刚克隆的源分支。

Hopefully that will be something like what you are after.

希望这会像你所追求的那样。

回答by nosaiba darwish

You can do it by using the below command:

您可以使用以下命令来完成:

git clone -b branch_name --single-branch project_url local_folder_to_clone_in

回答by Casey

From git-clone man page:

git-clone 手册页

--single-branchis your friend during clone remember to use with --branch <branch name>or only remote primary HEAD will be cloned (master by default)

--single-branch在克隆期间是你的朋友记得使用--branch <branch name>或者只克隆远程主 HEAD(默认情况下是主)

Always remember to do Ctrl+ F5to read fresh source, not the one from cache :-) (I didn't so didn't know about this option for long time.)

永远记得做Ctrl+F5读取新的源代码,而不是来自缓存的源代码:-)(我很长一段时间都不知道这个选项。)

回答by Jitendra

git clone <url> --branch <branch> --single-branch

Just put in URL and branch name.

只需输入 URL 和分支名称。

回答by Cubiczx

Clone only one branch. This is the easiest way:

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

$ git clone -b BRANCH_NAME --single-branch [email protected]:___/PROJECTNAME.git

回答by nPcomp

For cloning a specific branch you can do :

要克隆特定分支,您可以执行以下操作:

git clone --branch yourBranchName [email protected]

git clone --branch yourBranchName [email protected]

回答by Prem S

For cloning a branch of Git you don't have the public key to, use this:

要克隆没有公钥的 Git 分支,请使用以下命令:

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