我有一个名为'skeleton'的本地Git存储库,用于存储项目框架。它有一些分支,用于不同类型的项目:
casey@agave [~/Projects/skeleton] git branch
* master
rails
c
c++
如果我想查看一个新项目的主分支,我可以
casey@agave [~/Projects] git clone skeleton new
Initialized empty Git repository in /Users/casey/Projects/new/.git/
一切都是我想要的。具体来说,新的主分支指向骨架主分支,我可以通过推拉来移动对基本项目设置的更改。
但是,如果我想要克隆另一个分支,那么什么不起作用。我无法得到它以便我只拉动我想要的分支,例如rails
分支,然后新的存储库有一个master
分支,它推送到框架存储库{{{ 1}}分支,默认情况下。
有没有好办法做到这一点?或者,也许这不是Git想要我构建事物的方式,我当然对此持开放态度。也许我应该有多个存储库,Ruby on Rails骨架存储库跟踪主骨架存储库?任何单个项目都会克隆Ruby on Rails骨架存储库。
答案 0 :(得分:738)
注意: 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>]
您可以在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
答案 1 :(得分:650)
一种方法是执行以下操作。
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
答案 2 :(得分:106)
使用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
)调用也会下载所请求的分支。
答案 3 :(得分:26)
你可以尝试冗长的方式:
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
这是做什么的:
希望这将是你所追求的。
答案 4 :(得分:18)
您可以使用以下命令执行此操作:
git clone -b branch_name --single-branch project_url local_folder_to_clone_in
答案 5 :(得分:15)
--single-branch
是克隆期间的朋友
记得使用--branch <branch name>
或只克隆远程主要HEAD(默认为master)
永远记得做 Ctrl + F5 来读取新的来源,而不是缓存中的来源:-) (很长一段时间我都不知道这个选项。)
答案 6 :(得分:11)
git clone <url> --branch <branch> --single-branch
只需输入网址和分支名称即可。
答案 7 :(得分:5)
只克隆一个分支。这是最简单的方法:
$ git clone -b BRANCH_NAME --single-branch git@bitbucket.org:___/PROJECTNAME.git
答案 8 :(得分:2)
要克隆Git的一个分支,你没有公钥,请使用:
git clone -b <branch> <Git repository URL or clone URL you get from Git repository>
答案 9 :(得分:2)
要克隆特定分支,您可以这样做:
git clone --branch yourBranchName git@yourRepository.git
答案 10 :(得分:2)
让我们以烧瓶回购为例。除母版外,它还有3个分支。让我们检查一下1.1.x远程分支
克隆git repo
git clone https://github.com/pallets/flask
cd进入仓库。
cd flask
获取远程分支1.1.x
git fetch origin 1.1.x
签出分支机构
git checkout 1.1.x
您将切换到分支1.1.x,它将跟踪远程1.1.x分支。
答案 11 :(得分:2)
这里有很多答案提到:
# (We'll refer to this as "the 1st command" below.)
git clone -b branch_name --single-branch \
https://github.com/some_project/some_project.git
...或其中的一些版本,其中一些仅提及:
# (We'll refer to this as "the 2nd command" below.)
git clone -b branch_name https://github.com/some_project/some_project.git
...没有--single-branch
部分。
但是,我想稍微解释一下这两件事,并展示一套更熟悉的等效命令,以便我们可以了解每个幕后情况
。让我们假设您在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
)被配置为获取{{ 1}}远程。我不是这方面的专家,但是我相信这就是origin
的意思。
第一个命令(+refs/heads/*:refs/remotes/origin/*
)仅将git clone -b version_2.5 --single-branch https://github.com/micronucleus/micronucleus.git
分支克隆到本地PC,并且还会将其检出。该命令等效于执行此操作(至少在最终结果中是这样,除了它在开始时还下载少得多的数据,因为它仅克隆一个分支而不是全部克隆):
version_2.5
默认情况下,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
进行配置,以便在我们调用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
远程分支获取version_2.5
分支头。而已!请注意,永远不会提取其他远程分支。
因此,现在您看到使用origin/version_2.5
基本上只是确保克隆后检出-b branch_name
分支,但仍克隆所有远程分支,而同时添加branch_name
可确保仅--single-branch
被克隆,获取,拉取和跟踪。 任何其他远程分支都不会克隆到您的PC。
我个人更喜欢单独使用branch_name
选项,因为我希望将 all 分支克隆到本地PC。一个例外可能是在庞大的共享单仓库中,该仓库有数十个,甚至数百个或数千个远程分支。在这种情况下,只需使用-b branch_name
即可克隆您关心并完成的一个主要分支。例如,比下载200 GiB的数据要好于在庞大的Mono-repo中为-b branch_name --single-branch
分支下载50 GiB的数据,比下载200 GiB的数据要好得多,因为他们也可以在他们的2000个对等分支中工作! >
答案 12 :(得分:1)
有点晚了,但是我想添加我用来解决这个问题的解决方案。我找到了解决方法here。
无论如何,问题似乎是在问“如何从另一个存储库的一个分支开始一个新项目?”
为此,我使用的解决方案是首先在github或任何地方创建一个新的仓库。这将作为您新项目的仓库。
在本地计算机上,导航到具有要用作新项目模板的分支的项目。
运行命令:
git push https://github.com/accountname/new-repo.git +old_branch:master
这将把old_branch推送到新仓库,并使其成为新仓库的主分支。
然后,您只需要将新存储库克隆到新项目的本地目录中,并在旧分支上启动一个新项目即可。
答案 13 :(得分:1)
如果您想进行浅克隆,可以使用以下方法进行操作:
AddResourceOwnerValidator<>()
// register the IResourceOwnerPasswordValidator as an HTTP client
services.AddHttpClient<IResourceOwnerPasswordValidator, ResourceOwnerPasswordValidator>();
// do not register anther IResourceOwnerPasswordValidator
services.AddIdentityServer()
.AddInMemoryIdentityResources(ConfigurationData.IdentityResources)
.AddInMemoryApiResources(ConfigurationData.ApiResources)
.AddInMemoryClients(ConfigurationData.Clients)
.AddProfileService<ProfileService>();
表示git clone -b mybranch --depth=1 https://example.com/myproject.git localname
。
答案 14 :(得分:1)
如果您要保持分支独立,则可以使用此命令来混合单个分支并重命名文件夹
git clone -b [branch-name] --single-branch [url] [folder_name]
示例
git clone -b mybranch --single-branch git://github/repository.git project_mybranch
答案 15 :(得分:0)
打开cmd。
cd folder_name
(输入克隆分支的路径)
只需一个命令:
git clone url_of_projecturltoclone -b branch_name
答案 16 :(得分:0)
可以分2步完成
克隆存储库
git clone <http url>
检查您想要的分支
git checkout $BranchName
答案 17 :(得分:0)
与@ nosaiba-darwish在这里所说的类似:here
这是我们通常在公司中所做的:
reactivemongo.bson.exceptions.DocumentKeyNotFound: The key 'cursor' could not be found in this document or array
at reactivemongo.bson.BSONDocument.$anonfun$getTry$2(types.scala:1019)
at scala.Option.getOrElse(Option.scala:121)
at reactivemongo.bson.BSONDocument.getTry(types.scala:1019)
at reactivemongo.bson.BSONDocument.getAsTry(types.scala:1061)
at reactivemongo.api.commands.bson.BSONAggregationImplicits$AggregationResultReader$.readResult(aggregation.scala:98)
at reactivemongo.api.commands.bson.BSONAggregationImplicits$AggregationResultReader$.readResult(aggregation.scala:89)
at reactivemongo.api.commands.bson.DealingWithGenericCommandErrorsReader.read(bsoncommands.scala:52)
at reactivemongo.api.commands.bson.DealingWithGenericCommandErrorsReader.read$(bsoncommands.scala:41)
at reactivemongo.api.commands.bson.BSONAggregationImplicits$AggregationResultReader$.read(aggregation.scala:89)
at reactivemongo.api.commands.bson.BSONAggregationImplicits$AggregationResultReader$.read(aggregation.scala:89)
答案 18 :(得分:0)
主要有2种解决方案:
您需要使用-b命令开关指定分支名称。这是克隆特定git分支的命令的语法。
git clone -b <BRANCH_NAME> <GIT_REMOTE_URL>
示例:
git clone -b tahir https://github.com/Repository/Project.git
以下命令将从git存储库中克隆分支 tahir 。上面的命令仅克隆特定分支,但获取其他分支的详细信息。您可以使用命令查看所有分支的详细信息。
git branch -a
您可以使用--single-branch
标志来防止获取其他分支的详细信息,如下所示:
git clone -b <BRANCH_NAME> --single-branch <GIT_REMOTE_URL>
示例:
git clone -b tahir --single-branch \
https://github.com/Repository/Project.git
现在,如果您执行git branch -a
,它将仅显示您已克隆的当前单个分支,而不是所有分支。因此,这取决于您的需求。
答案 19 :(得分:0)
git clone --branch {branch-name} {repo-URI}
示例:
git clone --branch dev https://github.com/ann/cleaningmachine.git
{branch-name}
{repo-URI}
答案 20 :(得分:0)
对于像我这样的新手, 只需运行下面的代码
git clone https://gitlab.com/repo/repo.git --branch <name of branch> --single-branch
答案 21 :(得分:0)
这应该有效
git clone --single-branch <branchname> <remote-repo-url>
答案 22 :(得分:-6)
<Branch Name>