如何克隆特定的 Git 分支?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1911109/
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:31:06  来源:igfitidea点击:

How do I clone a specific Git branch?

gitgit-branchgit-clone

提问by Scud

Git clone will behave copying remote current working branch into local.

Git clone 会将远程当前工作分支复制到本地。

Is there any way to clone a specific branch by myself without switching branches on the remote repository?

有没有办法自己克隆一个特定的分支而无需在远程存储库上切换分支?

采纳答案by Michael Krelin - hacker

git clone --single-branch --branch <branchname> <remote-repo>

The --single-branchoption is valid from version 1.7.10and later.

--single-branch选项从1.7.10及更高版本开始有效。

Please see also the other answerwhich many people prefer.

请参阅许多人喜欢的其他答案

You may also want to make sure you understand the difference. And the difference is: by invoking git clone --branch <branchname> urlyou're fetching allthe branches and checking out one. That may, for instance, mean that your repository has a 5kB documentation or wiki branch and 5GB data branch. And whenever you want to edit your frontpage, you may end up cloning 5GB of data.

您可能还想确保您了解差异。区别在于:通过调用,git clone --branch <branchname> url您将获取所有分支并签出一个。例如,这可能意味着您的存储库有一个 5kB 的文档或 wiki 分支和 5GB 的数据分支。每当您想编辑首页时,最终可能会克隆 5GB 的数据。

Again, that is not to say git clone --branchis not the way to accomplish that, it's just that it's not alwayswhat you want to accomplish, when you're asking about cloning a specific branch.

同样,这并不是说git clone --branch这不是实现这一目标的方法,只是当您询问克隆特定分支时,它并不总是您想要完成的。

At the time of writing the original answer below, git had no --single-branchoption, but let's preserve it for full satisfaction of angry bees.

在编写下面的原始答案时,git 没有--single-branch选择,但让我们保留它,让愤怒的蜜蜂完全满意。

The answer so badly disliked by copypasters was this:

抄袭者非常不喜欢的答案是:

git init
git remote add -t refspec remotename host:/dir.git
git fetch

回答by Jorge E. Cardona

git clone -b <branch> <remote_repo>

Example:

例子:

git clone -b my-branch [email protected]:user/myproject.git

With Git 1.7.10 and later, add --single-branchto prevent fetching of all branches. Example, with OpenCV 2.4 branch:

对于 Git 1.7.10 及更高版本,添加--single-branch以防止获取所有分支。示例,使用 OpenCV 2.4 分支:

git clone -b opencv-2.4 --single-branch https://github.com/Itseez/opencv.git

回答by superlogical

Here is a really simple way to do it :)

这是一个非常简单的方法:)

Clone the repository

克隆存储库

git clone <repository_url>

List all branches

列出所有分支

git branch -a 

Checkout the branch that you want

签出你想要的分支

git checkout <name_of_branch>

回答by Edmar Miyake

To clone a branch withoutfetching other branches:

要克隆一个分支而不获取其他分支:

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

回答by dkinzer

git checkout -b <branch-name> <origin/branch_name>

for example in my case:

例如在我的情况下:

 git branch -a
* master
  origin/HEAD
  origin/enum-account-number
  origin/master
  origin/rel_table_play
  origin/sugarfield_customer_number_show_c

So to create a new branch based on my enum-account-number branch I do:

因此,要根据我的 enum-account-number 分支创建一个新分支,我会执行以下操作:

git checkout -b enum-account-number origin/enum-account-number

After you hit return the following happens:

点击回车后,会发生以下情况:

Branch enum-account-number set up to track remote branch refs/remotes/origin/enum-account-number.
Switched to a new branch "enum-account-number

"

回答by PlanetUnknown

Create a branch on the local system with that name. e.g. say you want to get the branch named "branch-05142011"

使用该名称在本地系统上创建一个分支。例如说你想得到名为“branch-05142011”的分支

git branch branch-05142011 origin/branch-05142011

git branch branch-05142011 origin/branch-05142011

It'll give you a message:

它会给你一条信息:

$ git checkout --track origin/branch-05142011
Branch branch-05142011 set up to track remote branch refs/remotes/origin/branch-05142011.
Switched to a new branch "branch-05142011"

Now just checkout the branch like below and you have the code
git checkout branch-05142011

现在只需像下面这样结帐分支,你就有了代码
git checkout branch-05142011

回答by savgur

git --branch <branchname> <url>

But bash completion don't get this key: --branch

但是 bash 完成没有得到这个键: --branch

Enjoy.

享受。