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

Cas*_*mor 749 git branch git-clone

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

casey@agave [~/Projects/skeleton] git branch
* master
  rails
  c
  c++
Run Code Online (Sandbox Code Playgroud)

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

casey@agave [~/Projects] git clone skeleton new
Initialized empty Git repository in /Users/casey/Projects/new/.git/
Run Code Online (Sandbox Code Playgroud)

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

但是,如果我想要克隆另一个分支,那么什么不起作用.我无法得到它,所以我只拉动我想要的rails分支,例如分支,然后新的存储库有一个master分支,rails默认情况下推送到骨架存储库的分支并从中拉出.

这有什么好办法吗?或者,也许这不是Git想要我构建事物的方式,我当然对此持开放态度.也许我应该有多个存储库,Ruby on Rails骨架存储库跟踪主骨架存储库?任何克隆Ruby on Rails骨架存储库的单个项目.

Von*_*onC 803

注意: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>]
Run Code Online (Sandbox Code Playgroud)

你可以在t5500-fetch-pack.sh:

test_expect_success 'single branch clone' '
  git clone --single-branch "file://$(pwd)/." singlebranch
'
Run Code Online (Sandbox Code Playgroud)

东武 评论说:

这在做浅层克隆时是隐含的.
这是git clone --depth 1节省带宽的最简单方法.

从Git 1.9.0(2014年2月)开始,浅克隆支持数据传输(推/拉),因此该选项现在更有用.
请参阅" Is git clone --depth 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
Run Code Online (Sandbox Code Playgroud)

正如克里斯评论:

让错过的分支反转的神奇线--single-branch是(git v2.1.4):

git config remote.origin.fetch +refs/heads/*:refs/remotes/origin/*
git fetch --unshallow  
Run Code Online (Sandbox Code Playgroud)

  • `git clone <url> --branch <branch> --single-branch <folder>`就像一个魅力. (75认同)
  • 此外,这在执行浅克隆时是隐含的.这使得`clone --depth 1`成为节省带宽的最简单方法. (5认同)
  • 自从Git 1.9.0以来,@ nmr已不复存在:请参阅http://stackoverflow.com/a/21217267/6309 (4认同)
  • 是的,我认为是时候更新已接受的答案了 :) 这确实“很有魅力” (2认同)
  • 只是为了说明Peter Cordes的完整修正,让错过的分支反转`--single-branch`的神奇路线是`git config remote.origin.fetch + refs/heads/*:refs/remotes/origin/*`和`git fetch --unshallow`(git v2.1.4) (2认同)

Ale*_*sco 661

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

git clone user@git-server:project_name.git -b branch_name /your/folder
Run Code Online (Sandbox Code Playgroud)

branch_name您选择的分支在哪里,"/ your/folder"是该分支的目标文件夹.确实,这将使其他分支机构为您提供来回合并的机会.现在,从Git 1.7.10开始,您现在可以执行此操作

git clone user@git-server:project_name.git -b branch_name --single-branch /your/folder
Run Code Online (Sandbox Code Playgroud)

  • 这有效,但它取出*all*分支.请参阅下面的@ frerich = raabe的答案. (36认同)
  • 您还可以添加`--single-branch`来仅获取与此分支关联的历史记录,而不是存储库包含的所有历史记录和标记. (11认同)
  • 谢谢.在我的情况下只添加"-b branch_name".它为我节省了很多时间. (3认同)

Fre*_*abe 111

使用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
Run Code Online (Sandbox Code Playgroud)

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

  • @Ian:一个区别是`git clone -b`给你所有的远程引用(所有远程分支和标签),就像`git branch -a`所示.使用我的脚本,您只需获得一个参考. (12认同)
  • 请注意,即使没有`-f`,`-t $ BRANCH`也能正常工作,这会立即获取.然后,`git fetch origin`将获取,而`git checkout $ BRANCH`将建立本地分支和结账.当你需要在获取之前配置远程时,这很有用,例如`git config remote.origin.uploadpack =/bin/upload-pack`. (3认同)

jkp*_*jkp 29

你可以尝试冗长的方式:

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
Run Code Online (Sandbox Code Playgroud)

这样做是:

  • 创建并初始化一个空的Git存储库.
  • 将原始存储库添加为远程调用.
  • 仅从远程调用的原点获取所需的分支.
  • 创建并检出一个新分支,该分支设置为跟踪刚刚克隆的源分支.

希望这将是你所追求的.


小智 23

您可以使用以下命令执行此操作:

git clone -b branch_name --single-branch project_url local_folder_to_clone_in
Run Code Online (Sandbox Code Playgroud)

  • 工作完美,我实际上在源代码树中无法克隆单个分支,因此需要手动完成 (2认同)

Cas*_*sey 17

git-clone手册页:

--single-branch在克隆期间是你的朋友记得使用--branch <branch name>或只有远程主要HEAD将被克隆(默认为master)

永远记得做Ctrl+ F5读新鲜的来源,而不是缓存中的那个:-)(我不是很长时间都不知道这个选项.)


Jit*_*dra 15

git clone <url> --branch <branch> --single-branch
Run Code Online (Sandbox Code Playgroud)

只需输入URL和分支名称即可.


Cub*_*czx 7

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

$ git clone -b BRANCH_NAME --single-branch git@bitbucket.org:___/PROJECTNAME.git
Run Code Online (Sandbox Code Playgroud)


小智 7

git clone --branch <branchname> <remote-repo-url>
Run Code Online (Sandbox Code Playgroud)

或者

git clone -b <branchname> <remote-repo-url>
Run Code Online (Sandbox Code Playgroud)


Dom*_*omi 6

\xef\xb8\x8f 一些性能测量 \xef\xb8\x8f

\n

我比较了两种建议的方法,发现一种比另一种更快(而且更小)(如果您的唯一目标是克隆,并且您不关心其他太多,那么这是有利的)。

\n

我发现最重要的性能指标是--depth,也就是说VonC答案是最快的。\n你自己试试:

\n
time bash -cl "git clone --single-branch --depth=1 --branch=$MYBRANCH $MYGITURL"\n
Run Code Online (Sandbox Code Playgroud)\n

我用一个历史悠久、分支众多的大项目对此进行了测试,这种方法大约需要 6s。

\n

请注意,与该线程中的几条评论所声称的相反(至少在最近的 git 版本中),这只会是checkout目标分支。git branch -a只列出该单个分支。

\n

亚军

\n

相比之下,frerich-rabe方法始终需要 26 秒:

\n
time bash -cl "git init && git remote add -t $MYBRANCH -f origin $MYGITURL && git checkout $MYBRANCH"\n
Run Code Online (Sandbox Code Playgroud)\n

在比较两者时,还需要注意的是,在我的例子中,目标分支是其父分支的精简版。第一种方法的下载进度条反映了大小的减少(< 10MB),而第二种方法则没有(> 90MB)。(我确信,有更成熟的方法来测量总下载大小,但我还没有研究过。)

\n


nPc*_*omp 5

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

git clone --branch yourBranchName git@yourRepository.git
Run Code Online (Sandbox Code Playgroud)


Gab*_*les 5

这里有很多答案,其中提到:

  1. 下载1支(--single-branch部分):

    # (We'll refer to this as "the 1st command" below.)
    # 1. Clone ONLY `branch_name`, then check it out. The `--single-branch` part
    #    means that ONLY `branch_name` is cloned, fetched, pulled, and
    #    tracked. _No other remote branches will be cloned to your PC
    #    whatsoever._
    git clone -b branch_name --single-branch \
    https://github.com/some_project/some_project.git
    
    Run Code Online (Sandbox Code Playgroud)

    ...或某个版本,还有一些只提到:

  2. 下载所有分支(不含--single-branch部分):

    # (We'll refer to this as "the 2nd command" below.)
    # 2. Clone ALL remote branches, then `git checkout` the `branch_name`
    #    branch.
    git clone -b branch_name \
    https://github.com/some_project/some_project.git
    
    Run Code Online (Sandbox Code Playgroud)

但是,我想稍微阐述一下这两件事并展示一组更熟悉的等效命令,以便我们可以了解每个幕后发生的事情。

假设您在 GitHub 上有一个远程存储库,地址为https://github.com/micronucleus/micronucleus.git,带有远程分支masterversion_2.5(这是一个您现在可以实际运行的真实示例)。

上面第二条命令的分解:

第二个命令( git clone -b version_2.5 https://github.com/micronucleus/micronucleus.git) 将 ALL REMOTE BRANCHES 克隆到本地 PC,然后检出version_2.5分支而不是master分支。该命令相当于执行以下操作:

git clone https://github.com/micronucleus/micronucleus.git
cd micronucleus  # cd into the repo you just cloned
git checkout version_2.5
# To be pedantic, also delete the local `master` branch since
# technically it won't exist yet since you haven't yet checked
# it out with `git checkout master`, which would create it from
# your locally-stored remote-tracking branch, `origin/master`
git branch -d master
Run Code Online (Sandbox Code Playgroud)

-b version_2.5部件自动version_2.5为我们检出分支而不是master.

git branch -a然而,向我们展示了所有分支都被克隆到了我们的本地 PC。在这里你可以看到我们所在的本地分支version_2.5,加上本地存储的远程跟踪分支 origin/HEAD(指向origin/master),加上origin/master, 和origin/version_2.5

$ git branch -a
* version_2.5
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/version_2.5
Run Code Online (Sandbox Code Playgroud)

我们也可以看看我们的fetch引用是什么。您可以打开.git/config文件直接查看它们,也可以直接运行git config remote.origin.fetch

$ git config remote.origin.fetch
+refs/heads/*:refs/remotes/origin/*
Run Code Online (Sandbox Code Playgroud)

您可以在上面看到我们的git fetch命令(也由 触发,git pull因为它相当于git fetch && git merge)被配置为获取origin远程中所有分支的所有头。我不是这方面的专家,但我相信这就是+refs/heads/*:refs/remotes/origin/*意思。

上面第一个命令的分解:

第一个命令( git clone -b version_2.5 --single-branch https://github.com/micronucleus/micronucleus.git) 仅将version_2.5分支克隆到您的本地 PC,并且还会对其进行检查。该命令相当于执行此操作(至少在最终结果中,除了它在开始时下载的数据也少得多,因为它只克隆一个分支而不是所有分支):

git clone https://github.com/micronucleus/micronucleus.git
cd micronucleus  # cd into the repo you just cloned
git checkout version_2.5

# Delete ALL other branches, including remote-tracking ones, which are not the 
# `version_2.5` branch:
# One local branch
git branch -d master
# ALL other locally-stored remote-tracking branches
git branch -dr origin/HEAD 
git branch -dr origin/master

# Fix your `.git/config` file so that `git fetch` does the right thing, fetching
# ONLY the `version_2.5` branch head from the `origin/version_2.5` remote branch:
git config remote.origin.fetch \
"+refs/heads/version_2.5:refs/remotes/origin/version_2.5"
Run Code Online (Sandbox Code Playgroud)

-b version_2.5部分导致version_2.5分支被检出而不是master默认分支(如前文所述),该--single-branch部分导致:

  1. 没有其他分支要克隆到我们的 PC 上,并且
  2. git fetch进行配置,以便在我们调用git fetch或时不会获取任何其他分支git pull

这个命令真正克隆,只会获取我们想要的一个分支,就是这样!

git branch -a向我们展示了只有version_2.5分支被克隆和签出。在这里,我们通过*检出哪个分支看到,我们还看到我们有一个本地存储的远程跟踪分支origin/version_2.5

$ git branch -a
* version_2.5
  remotes/origin/version_2.5
Run Code Online (Sandbox Code Playgroud)

我们也可以看看我们的fetch引用是什么。您可以打开.git/config文件直接查看它们,也可以直接运行git config remote.origin.fetch

$ git config remote.origin.fetch
+refs/heads/version_2.5:refs/remotes/origin/version_2.5
Run Code Online (Sandbox Code Playgroud)

您可以在上面看到我们的git fetch命令只会version_2.5origin/version_2.5远程分支中获取分支头。就是这样!请注意,永远不会获取其他远程分支。

概括:

所以,现在你看到 using-b branch_name基本上只是确保在branch_name克隆后签出分支,但仍然克隆所有远程分支,而添加也--single-branch确保 ONLYbranch_name被克隆、获取、拉取和跟踪。任何其他远程分支都不会被克隆到您的 PC 上。

就个人而言,我更喜欢-b branch_name单独的选项,因为我希望所有分支都克隆到我的本地 PC。一个例外可能是一个巨大的、共享的单一存储库,它有几十个,甚至成百上千个远程分支。在这种情况下,只需使用-b branch_name --single-branch克隆您关心的一个主分支即可完成。例如,最好master在一个巨大的单一存储库中为分支下载 50 GiB 的数据,而不是下载 200 GiB 的数据,这样您就可以拥有 2000 个他们正在处理的同行分支!

参考:

  1. 只克隆一个分支
  2. 如何停止跟踪 Git 中的远程分支?


Ash*_*ini 5

我是这样做的

git clone -b <分支名称> url

例子 :

git clone -b master https://gitlab.com/jhondoe/applicationproject.git
Run Code Online (Sandbox Code Playgroud)

或者

git clone -b master git@gitlab.com:jhondoe/applicationproject.git
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

660474 次

最近记录:

6 年,2 月 前