我在 ~/local_repo
中有一个本地 Git 存储库。它有几个分支:
$ git branch
* master
rails
c
c++
要克隆本地存储库,我这样做:
$ git clone ~/local_repo new_repo
Initialized empty Git repository in /home/username/new_repo/.git/
new_repo
master 分支指向 local_repo
master 分支,我可以推/拉。
但我无法克隆另一个分支。我只想拉出我想要的分支(例如 rails
),以便新存储库有一个 master
分支,默认情况下该分支会从 local_repo
的 rails
分支推入和拉出。我如何做到这一点,或者可能与 local_repo
跟踪主 local_repo
类似?
git branch -a
显示什么?
git checkout -b newbranch origin/branchiwant
会更好吗? (不带--track
选项)
注意:git1.7.10(2012 年 4 月) 实际上允许您只克隆一个分支:
# clone only the remote primary HEAD (default: origin/master)
git clone <url> --single-branch
# as in:
git clone <url> --branch <branch> --single-branch [<folder>]
(如果是远程存储库,<url>
是 URL,并且不引用自身克隆的分支)
您可以在 t5500-fetch-pack.sh
中看到它:
test_expect_success 'single branch clone' '
git clone --single-branch "file://$(pwd)/." singlebranch
'
这在进行浅层克隆时是隐含的。这使得 git clone --depth 1 成为节省带宽的最简单方法。
从 Git 1.9.0(2014 年 2 月)开始,浅层克隆支持数据传输(推/拉),因此该选项现在更加有用。
在“Is git clone --depth 1
(shallow clone) more useful than it makes out?”中查看更多信息。
“撤消”浅层克隆在“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
正如 Chris 评论:
让丢失的分支反转的神奇线 --single-branch 是(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
”现在在克隆子模块时使用相同的单分支选项。
请参阅 Emily Shaffer (nasamuffin
) 的 commit 132f600、commit 4731957(2020 年 2 月 21 日)。
(由 Junio C Hamano -- gitster
-- 于 commit b22db26 合并,2020 年 3 月 5 日)
克隆:在 --recurse-submodules 期间通过 --single-branch 签字人:Emily Shaffer 签字人:Jeff King 以前,执行“git clone --recurse-submodules --single-branch”会导致子模块克隆所有即使超级项目只克隆了一个分支。通过子模块帮助框架管道 --single-branch 使其稍后“克隆”。
一种方法是执行以下操作。
git clone user@git-server:project_name.git -b branch_name /your/folder
其中 branch_name
是您选择的分支,“/your/folder”是该分支的目标文件夹。确实,这会带来其他分支,让您有机会来回合并。
更新
现在,从 Git 1.7.10 开始,您现在可以执行此操作
git clone user@git-server:project_name.git -b branch_name --single-branch /your/folder
--single-branch
以仅获取与此分支关联的历史记录,而不是所有历史记录 &存储库包含的标签。
使用 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
这种方法的优点是后续的 git pull
(或 git fetch
)调用也只会下载请求的分支。
git clone -b $BRANCH $REMOTE_REPO $BRANCH
afik 他们是一样的吗?
git clone -b
为您提供所有远程引用(所有远程分支和标签),如 git branch -a
所示。使用我的脚本,您只会得到一个参考。
-f
,-t $BRANCH
也可以工作,它会立即获取。然后,git fetch origin
将获取,git checkout $BRANCH
将建立本地分支并结帐。当您需要在获取之前配置远程时,这很有用,例如 git config remote.origin.uploadpack=/bin/upload-pack
。
branch -a
显示 origin/$BRANCH
。执行 git checkout origin/$BRANCH
解决了这个问题。
您可以尝试冗长的方式:
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
这是做什么的:
创建并初始化一个空的 Git 存储库。
将原始存储库添加为名为 origin 的远程。
仅从名为 origin 的远程获取您需要的分支。
创建并签出一个新分支,该分支设置为跟踪您刚刚克隆的源分支。
希望这会是你所追求的。
git branch -r
的输出吗?
git branch -r
没有输出。
git clone <url> --branch <branch> --single-branch
只需输入 URL 和分支名称。
您可以使用以下命令执行此操作:
git clone -b branch_name --single-branch project_url local_folder_to_clone_in
--single-branch
是您在克隆期间的朋友,请记住与 --branch <branch name>
一起使用,否则只会克隆远程主 HEAD(默认为 master)
永远记得按 Ctrl + F5 来读取新的源代码,而不是来自缓存的源代码 :-) (我不知道这个选项很长时间了。)
只克隆一个分支。这是最简单的方法:
git clone -b BRANCH_NAME --single-branch git@bitbucket.org:___/PROJECTNAME.git
这里有很多答案提到:
下载 1 个分支(带有 --single-branch 部分): # (我们将在下面将此称为“第一个命令”。) # 1. 仅克隆 `branch_name`,然后检查它。 `--single-branch` 部分 # 表示只有 `branch_name` 被克隆、获取、拉取和 # 跟踪。 _没有其他远程分支将被克隆到您的 PC # 无论如何。_ git clone -b branch_name --single-branch \ https://github.com/some_project/some_project.git ...或某个版本,以及一些只提到:下载所有分支(不包括 --single-branch 部分):#(我们将在下面将此称为“第二个命令”。)# 2. 克隆所有远程分支,然后 `git checkout` `分支名称`#分支。 git clone -b branch_name \ https://github.com/some_project/some_project.git
但是,我想稍微解释一下这两件事,并展示一组更熟悉的等效命令,这样我们就可以看到每个底层发生了什么。
假设您在 GitHub 上的 https://github.com/micronucleus/micronucleus.git 上有一个远程存储库,带有远程分支 master
和 version_2.5
(这是一个您现在可以实际运行的真实示例)。
上面第二条命令的细分:
第二条命令 (git clone -b version_2.5 https://github.com/micronucleus/micronucleus.git
) 将所有远程分支克隆到您的本地 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
-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
我们还可以查看我们的 fetch
引用是什么。您可以打开 .git/config
文件直接查看它们,或者只运行 git config remote.origin.fetch
:
$ git config remote.origin.fetch
+refs/heads/*:refs/remotes/origin/*
您可以在上面看到,我们的 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"
默认情况下,-b version_2.5
部分导致签出 version_2.5
分支而不是 master
分支(如上所述),--single-branch
部分导致:
NONE 其他分支将被克隆到我们的 PC,并且 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
我们还可以查看我们的 fetch
引用是什么。您可以打开 .git/config
文件直接查看它们,或者只运行 git config remote.origin.fetch
:
$ git config remote.origin.fetch
+refs/heads/version_2.5:refs/remotes/origin/version_2.5
您可以在上面看到我们的 git fetch
命令只会从 origin/version_2.5
远程分支获取 version_2.5
分支头。而已!请注意,永远不会获取其他远程分支。
概括:
所以,现在您看到使用 -b branch_name
基本上只是确保在克隆之后签出 branch_name
分支,但仍然克隆所有远程分支,而添加 --single-branch
确保只有 branch_name
被克隆、获取,拉,并跟踪。 任何其他远程分支都不会克隆到您的 PC。
就个人而言,我更喜欢单独使用 -b branch_name
选项,因为我希望将 所有 分支克隆到我的本地 PC。一个例外可能是一个巨大的、共享的单一仓库,它有几十个,甚至成百上千个远程分支。在这种情况下,只需使用 -b branch_name --single-branch
克隆您关心的一个主分支并完成。例如,最好在一个巨大的单一存储库中为 master
分支下载 50 GiB 的数据,而不是下载 200 GiB 的数据,这样您就可以拥有他们正在处理的 2000 个同行的分支!
参考:
只克隆一个分支 在 Git 中如何停止跟踪远程分支?
要克隆特定分支,您可以执行以下操作:
git clone --branch yourBranchName git@yourRepository.git
要克隆您没有公钥的 Git 分支,请使用以下命令:
git clone -b <branch> <Git repository URL or clone URL you get from Git repository>
有点晚了,但我想添加我用来解决这个问题的解决方案。我找到了解决方案 here。
无论如何,问题似乎是在问“如何从另一个 repo 的分支开始一个新项目?”
对此,我使用的解决方案是首先在 github 或任何地方创建一个新的 repo。这将作为新项目的回购。
在您的本地计算机上,导航到具有您要用作新项目模板的分支的项目。
运行命令:
git push https://github.com/accountname/new-repo.git +old_branch:master
这将做的是将 old_branch 推送到 new-repo 并使其成为新 repo 的主分支。
然后,您只需将新的 repo 克隆到新项目的本地目录,并且您在旧分支上启动了一个新项目。
让我们以烧瓶回购为例。除了master之外,它还有3个分支。让我们检查一下 1.1.x 远程分支
克隆 git 仓库
git clone https://github.com/pallets/flask
cd 进入 repo。
cd flask
获取远程分支 1.1.x
git fetch origin 1.1.x
结帐分支
git checkout 1.1.x
您将切换到分支 1.1.x,它将跟踪远程 1.1.x 分支。
打开cmd。
cd folder_name # enter the path where to clone the branch
只有一个命令:
git clone url_of_projecturltoclone -b branch_name
主要有两种解决方案:
您需要使用 -b 命令开关指定分支名称。以下是克隆特定 git 分支的命令语法。 git clone -b
remote_name/branch_name
(例如:origin/branch1
)本地远程跟踪克隆。因此,当您说:“上述命令 [引用 git clone -b tahir https://github.com/Repository/Project.git
] 仅克隆特定分支但获取其他分支的详细信息。”时,这是错误的。实际上,它克隆了所有的分支。它只是在克隆了所有的分支之后签出 tahir
分支。
--single-branch
标志来防止获取其他分支的详细信息,如下所示:”)改为:“您可以使用 --single-branch
选项来防止克隆(获取)所有其他分支。相反,它只会克隆(获取)然后签出您指定的一个分支。请参见下面的示例:...“。
🏎️一些性能测量🏎️
我比较了两种建议的方法,发现一种比另一种更快(并且更小)(如果您的唯一目标是克隆,并且您不太关心其他内容,这是有利的)。
我发现最重要的性能指标是--depth
,也就是说VonC的answer是最快的。自己试试:
time bash -cl "git clone --single-branch --depth=1 --branch=$MYBRANCH $MYGITURL"
我用一个历史悠久、分支众多的大项目对此进行了测试,这种方法大约需要 6s。
请注意,与此线程中的几条评论所声称的相反(至少在最近的 git 版本中),这只会 checkout
目标分支。 git branch -a
仅列出该单个分支。
亚军
相比之下,frerich-rabe 的 approach 始终需要 26 秒:
time bash -cl "git init && git remote add -t $MYBRANCH -f origin $MYGITURL && git checkout $MYBRANCH"
在比较两者时,还需要注意的是,在我的例子中,目标分支是其父分支的精简版本。第一种方法的下载进度条反映了大小的减小(< 10MB),而第二种方法没有(> 90MB)。 (我敢肯定,有更成熟的方法来测量总下载量,但我还没有研究过。)
如果你想要一个浅克隆,你可以这样做:
git clone -b mybranch --depth=1 https://example.com/myproject.git localname
--depth=1
暗示 --single-branch
。
如果要保持分支独立,可以使用此命令 git 单个分支并重命名文件夹
git clone -b [branch-name] --single-branch [url] [folder_name]
例子
git clone -b mybranch --single-branch git://github/repository.git project_mybranch
这应该工作
git clone --single-branch <branchname> <remote-repo-url>
git clone --branch <branchname> <remote-repo-url>
我是这样做的
git clone -b
例子 :
git clone -b master https://gitlab.com/jhondoe/applicationproject.git
或者
git clone -b master git@gitlab.com:jhondoe/applicationproject.git
git clone --single-branch -b [branch name] [repository URL]
git clone --branch <branchname> <remote-repo-url>
或者
git clone -b <branchname> <remote-repo-url>
git clone --branch {branch-name} {repo-URI}
例子:
git clone --branch dev https://github.com/ann/cleaningmachine.git
开发人员:这是{分支名称}
https://github.com/ann/cleaningmachine.git:这是 {repo-URI}
类似于@nosaiba-darwish 在这里所说的:here
这是我们公司通常会做的事情:
git clone -b <name_of_branch> --single-branch <git_url> folder_to_clone_locally
对于像我这样的新手,只需运行下面的代码
git clone https://gitlab.com/repo/repo.git --branch <name of branch> --single-branch
可以分2步完成
克隆存储库 git clone
git clone <http url>
部分将所有分支克隆到您的本地计算机,而不仅仅是您想要克隆的一个分支。这是因为它将所有分支提取到您的本地远程跟踪 remote_name/branch_name
分支,这些分支是这些分支的真实本地克隆。执行 git branch -a
以查看系统上的所有本地和本地远程跟踪(获取)分支,以证明这一点。但是,使用 --single-branch
选项确实可以使那些其他分支远离您的系统,并且 git branch -a
只会显示您想要的一个分支。
不定期副业成功案例分享
git clone <url> --branch <branch> --single-branch <folder>
就像一个魅力。clone --depth 1
成为节省带宽的最简单方法。--depth 1
对用户来说是不安全的,因为他们可能希望实际推/拉新克隆。--single-branch
的神奇线是git config remote.origin.fetch +refs/heads/*:refs/remotes/origin/*
和git fetch --unshallow
(git v2.1.4)