如何仅克隆 Git 存储库的子目录?
我的 Git 存储库在根目录下有两个子目录:
/finisht
/static
当它位于 SVN 中时, /finisht
在一个地方签出,而 /static
在其他地方签出,如下所示:
svn co svn+ssh://[email protected]/home/admin/repos/finisht/static static
Is there a way to do this with Git?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
您尝试做的事情称为稀疏签出,该功能已在 Git 1.7.0(2012 年 2 月)中添加。 执行稀疏克隆的步骤如下:
这将使用您的遥控器创建一个空存储库,并获取所有对象,但不会将它们签出。 然后执行以下操作:
现在您需要定义要实际签出的文件/文件夹。 这是通过在
.git/info/sparse-checkout
中列出它们来完成的,例如:最后但并非最不重要的一点是,使用远程状态更新您的空存储库:
您现在将有文件“签出”对于文件系统上的
some/dir
和another/sub/tree
(这些路径仍然存在),并且不存在其他路径。您可能想查看扩展教程并且您可能应该阅读官方 稀疏结账文档 和 读树。
作为函数:
用法:
请注意,这仍然会从服务器下载整个存储库 - 只是结帐的大小会减小。 目前无法仅克隆单个目录。 但如果您不需要存储库的历史记录,您至少可以通过创建浅克隆来节省带宽。 有关如何结合浅层 udondan 的回答 docs/git-clone" rel="noreferrer">克隆 和稀疏结账。
从 Git 2.25.0(2020 年 1 月)开始,实验性 sparse-checkout Git 中添加命令:
What you are trying to do is called a sparse checkout, and that feature was added in Git 1.7.0 (Feb. 2012). The steps to do a sparse clone are as follows:
This creates an empty repository with your remote, and fetches all objects but doesn't check them out. Then do:
Now you need to define which files/folders you want to actually check out. This is done by listing them in
.git/info/sparse-checkout
, eg:Last but not least, update your empty repo with the state from the remote:
You will now have files "checked out" for
some/dir
andanother/sub/tree
on your file system (with those paths still), and no other paths present.You might want to have a look at the extended tutorial and you should probably read the official documentation for sparse checkout and read-tree.
As a function:
Usage:
Note that this will still download the whole repository from the server – only the checkout is reduced in size. At the moment it is not possible to clone only a single directory. But if you don't need the history of the repository, you can at least save on bandwidth by creating a shallow clone. See udondan's answer below for information on how to combine shallow clone and sparse checkout.
As of Git 2.25.0 (Jan 2020) an experimental sparse-checkout command is added in Git:
git clone --filter
+git稀疏-checkout
仅下载所需的文件例如,仅克隆子目录
small/< 中的文件/code> 在此测试存储库中: https://github .com/cirosantilli/test-git-partial-clone-big-small-no-bigtree
您还可以选择多个目录进行下载:
但是,此方法不适用于单个文件,但这是另一种方法这样做: 如何从 git 存储库中稀疏地只签出一个文件?
在这个测试中,克隆基本上是瞬时的,我们可以确认克隆的存储库非常小,如所期望的:
给出:
该测试存储库包含:
big/
子目录,包含 10x 10MB 文件0
,1
, ...9
在顶层(这是因为某些之前的尝试会下载顶级文件)small/
和small2/
子目录,其中包含 1000 个文件,每个文件大小为一个字节所有内容都是伪随机的,因此不可压缩,因此我们可以轻松地注意是否下载了任何大文件,例如 与
ncdu
。因此,如果您下载了不需要的任何内容,您将获得 100 MB 的额外空间,而且这是非常明显的。
在上面,
git clone
下载单个对象,大概是提交:然后最终签出下载我们请求的文件:
于 2023 年 1 月在 git 2.37.2、Ubuntu 22.10 上测试。
TODO 还可以防止下载不需要的树对象
上述方法下载所有 Git 树对象(即目录列表,但不是实际文件内容)。 我们可以通过运行来确认:
并看到它包含大文件目录,例如:
在大多数项目中,这不会成为问题,因为与实际文件内容相比,这些文件应该很小,但我的完美主义者希望避开他们。
我还在目录
big_tree
下创建了一个非常极端的存储库,其中包含一些非常大的树对象(100 MB):https://github.com/cirosantilli/test-git-partial-clone-big-small如果有人找到克隆的方法,请告诉我其中的
small/
目录!关于命令
--filter
选项是与远程协议的更新一起添加的,它确实可以防止从服务器下载对象。不幸的是,还需要稀疏结帐部分。 您也可以只下载更容易理解的某些文件:
但由于某种原因,该方法 逐个下载文件一个非常慢,使其无法使用,除非目录中的文件很少。
另一个不太详细但失败的尝试是:
但是会下载顶级目录中的所有文件: 如何阻止 git clone --filter=blob:none --sparse 下载根目录下的文件?
梦想:任何目录都可以有 web接口元数据
此功能可能会彻底改变 Git。
想象一下,将企业的所有代码库放在一个单一的 monorepo 中,而无需 丑陋的第三方工具,例如 <代码>存储库。
想象一下直接在存储库中存储巨大的blob,而不需要任何丑陋的第三方扩展。
想象一下,如果 GitHub 允许每个文件/目录元数据,例如星号和权限,这样您就可以存储您的所有个人资料都位于一个存储库中。
想象一下,如果子模块被完全像常规目录一样对待:只需请求一个树 SHA,类似 DNS 的机制就会解析您的请求,首先查看您的 < a href="https://stackoverflow.com/questions/2350996/how-can-one-safely-use-a-shared-object-database-in-git">本地
~/.git
,然后首先到更近的服务器(您企业的镜像/缓存)并最终到达 GitHub。我有一个梦想。
测试锥 monorepo 理念
这是一种无需子模块的 monorepo 维护的可能理念。
我们希望避免使用子模块,因为每次进行具有子模块和非子模块组件的更改时都必须提交到两个单独的存储库,这很烦人。
每个带有 Makefile 或类似文件的目录都应该自行构建和测试。
此类目录可以依赖于以下任一者:
在 git 开始原生支持此功能(即只能跟踪子目录的子模块)之前,我们可以使用一些支持此功能git 跟踪文件中的元数据:
monorepo.json
,其中
sha
指整个存储库的常用 SHA。 然后我们需要脚本来检查这些目录,例如在 gitignoredmonorepo
文件夹下:每当您更改文件时,您都必须沿着树向上并测试具有 Makefile 的所有目录。 这是因为目录可能依赖于最新版本的子目录,因此您总是可能会破坏上面的内容。
相关:
git clone --filter
+git sparse-checkout
downloads only the required filesE.g., to clone only files in subdirectory
small/
in this test repository: https://github.com/cirosantilli/test-git-partial-clone-big-small-no-bigtreeYou could also select multiple directories for download with:
This method doesn't work for individual files however, but here is another method that does: How to sparsely checkout only one single file from a git repository?
In this test, clone is basically instantaneous, and we can confirm that the cloned repository is very small as desired:
giving:
That test repository contains:
big/
subdirectory with 10x 10MB files0
,1
, ...9
on toplevel (this is because certain previous attempts would download toplevel files)small/
andsmall2/
subdirectories with 1000 files of size one byte eachAll contents are pseudo-random and therefore incompressible, so we can easily notice if any of the big files were downloaded, e.g. with
ncdu
.So if you download anything you didn't want, you would get 100 MB extra, and it would be very noticeable.
On the above,
git clone
downloads a single object, presumably the commit:and then the final checkout downloads the files we requested:
Tested on git 2.37.2, Ubuntu 22.10, on January 2023.
TODO also prevent download of unneeded tree objects
The above method downloads all Git tree objects (i.e. directory listings, but not actual file contents). We can confirm that by running:
and seeing that it contains the directories large files such as:
In most projects this won't be an issue, as these should be small compared to the actual file contents, but the perfectionist in me would like to avoid them.
I've also created a very extreme repository with some very large tree objects (100 MB) under the directory
big_tree
: https://github.com/cirosantilli/test-git-partial-clone-big-smallLet me know if anyone finds a way to clone just the
small/
directory from it!About the commands
The
--filter
option was added together with an update to the remote protocol, and it truly prevents objects from being downloaded from the server.The
sparse-checkout
part is also needed unfortunately. You can also only download certain files with the much more understandable:but that method for some reason downloads files one by one very slowly, making it unusable unless you have very few files in the directory.
Another less verbose but failed attempt was:
but that downloads all files in the toplevel directory: How to prevent git clone --filter=blob:none --sparse from downloading files on the root directory?
The dream: any directory can have web interface metadata
This feature could revolutionize Git.
Imagine having all the code base of your enterprise in a single monorepo without ugly third-party tools like
repo
.Imagine storing huge blobs directly in the repo without any ugly third party extensions.
Imagine if GitHub would allow per file / directory metadata like stars and permissions, so you can store all your personal stuff under a single repo.
Imagine if submodules were treated exactly like regular directories: just request a tree SHA, and a DNS-like mechanism resolves your request, first looking on your local
~/.git
, then first to closer servers (your enterprise's mirror / cache) and ending up on GitHub.I have a dream.
The test cone monorepo philosophy
This is a possible philosophy for monorepo maintenance without submodules.
We want to avoid submodules because it is annoying to have to commit to two separate repositories every time you make a change that has a submodule and non-submodule component.
Every directory with a Makefile or analogous should build and test itself.
Such directories can depend on either:
Until git starts supporting this natively (i.e. submodules that can track only subdirectories), we can support this with some metadata in a git tracked file:
monorepo.json
where
sha
refers to the usual SHA of the entire repository. Then we need scripts that will checkout such directories e.g. under a gitignoredmonorepo
folder:Whenever you change a file, you have to go up the tree and test all directories that have Makefile. This is because directories can depend on subdirectories at their latest versions, so you could always break something above you.
Related:
从 Git 2.19 开始,这终于成为可能,如此答案中所示。
考虑投票赞成该答案。
注意:在 Git 2.19 中,仅实现了客户端支持,仍然缺少服务器端支持,因此仅在克隆本地存储库时有效。 另请注意,大型 Git 托管商(例如 GitHub)实际上并不使用 Git 服务器,而是使用自己的实现,因此即使 Git 服务器中出现支持,也并不自动意味着它可以在 Git 托管商上运行。 (另一方面,由于他们不使用 Git 服务器,因此他们可以在自己的实现中更快地实现它,然后再出现在 Git 服务器中。)
不,这在 Git 中是不可能的。
在 Git 中实现这样的事情将是一项巨大的工作,这意味着客户端存储库的完整性将无法再得到保证。 如果您有兴趣,请在 Git 邮件列表上搜索有关“稀疏克隆”和“稀疏获取”的讨论。
一般来说,Git 社区的共识是,如果您有几个始终独立检出的目录,那么它们实际上是两个不同的项目,并且应该位于两个不同的存储库中。 您可以使用 Git 子模块 将它们重新粘合在一起。
As of Git 2.19, this is finally possible, as can be seen in this answer.
Consider upvoting that answer.
Note: in Git 2.19, only client-side support is implemented, server-side support is still missing, so it only works when cloning local repositories. Also note that large Git hosters, e.g. GitHub, don't actually use the Git server, they use their own implementation, so even if support shows up in the Git server, it does not automatically mean that it works on Git hosters. (On the other hand, since they don't use the Git server, they could implement it faster in their own implementations before it shows up in Git server.)
No, that's not possible in Git.
Implementing something like this in Git would be a substantial effort and it would mean that the integrity of the client-side repository could no longer be guaranteed. If you are interested, search for discussions on "sparse clone" and "sparse fetch" on the Git mailing list.
In general, the consensus in the Git community is that if you have several directories that are always checked out independently, then these are really two different projects and should live in two different repositories. You can glue them back together using Git Submodules.
您可以将稀疏结帐和浅克隆功能结合起来。 浅克隆会切断历史记录,而稀疏签出只会提取与您的模式匹配的文件。
您至少需要 git 1.9 才能正常工作。 我自己只用2.2.0和2.2.2测试过。
这样您仍然能够推送,这是
git archive
无法实现的。You can combine the sparse checkout and the shallow clone features. The shallow clone cuts off the history and the sparse checkout only pulls the files matching your patterns.
You'll need minimum git 1.9 for this to work. Tested it myself only with 2.2.0 and 2.2.2.
This way you'll be still able to push, which is not possible with
git archive
.对于其他只想从 github 下载文件/文件夹的用户,只需使用:
eg
(是的,这里就是 svn。显然在 2016 年你仍然需要 svn 来简单下载一些 github 文件)
礼貌: 下载单个文件夹或目录来自 GitHub 存储库
重要 - 确保更新 github URL 并将
/tree/master/
替换为“/trunk/”。作为 bash 脚本:
注意
此方法下载文件夹,而不克隆/签出它。 您无法将更改推回存储库。 另一方面,与稀疏结帐或浅结帐相比,这会导致较小的下载。
For other users who just want to download a file/folder from github, simply use:
e.g.
(yes, that's svn here. apparently in 2016 you still need svn to simply download some github files)
Courtesy: Download a single folder or directory from a GitHub repo
Important - Make sure you update the github URL and replace
/tree/master/
with '/trunk/'.As bash script:
Note
This method downloads a folder, does not clone/checkout it. You can't push changes back to the repository. On the other hand - this results in smaller download compared to sparse checkout or shallow checkout.
2022年答案
我不知道为什么这个问题有这么多复杂的答案。 通过将存储库稀疏克隆到您想要的文件夹,可以轻松完成此操作。
git clone --filter=blob:none --sparse %your-git-repo-url%
cd %存储库目录%
git稀疏-checkout add %要克隆的子目录%
cd %your-subdirectory%
瞧! 现在您仅克隆了所需的子目录!
解释 - 这些命令到底在做什么?
git clone --filter=blob:none --sparse %your-git-repo-url%
在上面的命令中,
--稀疏
=> 告诉 git 这是一个稀疏克隆。 在这种情况下,Git 将仅检出根目录。现在 git 已获知元数据,并准备好签出您想要使用的任何子目录/文件。
稀疏克隆在有一个包含多个子目录的大型存储库并且您并不总是处理所有子目录时特别有用。 当您在大型存储库上进行稀疏克隆时,节省大量时间和带宽。
此外,
现在,在这个部分克隆的存储库中,您可以像平常一样继续结账和工作。 所有这些命令都可以完美运行。
2022 Answer
I'm not sure why there are so many complicated answers to this question. It can be done easily by doing a sparse cloning of the repo, to the folder that you want.
cmd
and run the following commands.git clone --filter=blob:none --sparse %your-git-repo-url%
cd %the repository directory%
git sparse-checkout add %subdirectory-to-be-cloned%
cd %your-subdirectory%
Voila! Now you have cloned only the subdirectory that you want!
Explanation - What are these commands doing really?
git clone --filter=blob:none --sparse %your-git-repo-url%
In the above command,
--filter=blob:none
=> You tell git that you only want to clone the metadata files. This way git collects the basic branch details and other meta from remote, which will ensure that your future checkouts from origin are smooth.--sparse
=> Tell git that this is a sparse clone. Git will checkout only the root directory in this case.Now git is informed with the metadata and ready to checkout any subdirectories/files that you want to work with.
Sparse clone is particularly useful when there is a large repo with several subdirectories and you're not always working on them all. Saves a lot of time and bandwidth when you do a sparse clone on a large repo.
Additionally,
now in this partially cloned repo you can continue to checkout and work like you normally would. All these commands work perfectly.
如果您从不打算与克隆的存储库进行交互,则可以执行完整的 git 克隆并使用
这种方式重写您的存储库,至少可以保留历史记录。
If you never plan to interact with the repository from which you cloned, you can do a full
git clone
and rewrite your repository usingThis way, at least the history will be preserved.
这看起来要简单得多:
This looks far simpler:
Git 1.7.0 有“稀疏签出”。 看
git config 手册页中的“core.sparseCheckout”,
中的“稀疏结帐” git read-tree 手册页,以及
< 中的“Skip-worktree 位” em>git update-index 手册页。
该界面不如 SVN 方便(例如,无法在初始克隆时进行稀疏检出),但现在可以构建更简单的界面的基本功能。
Git 1.7.0 has “sparse checkouts”. See
“core.sparseCheckout” in the git config manpage,
“Sparse checkout” in the git read-tree manpage, and
“Skip-worktree bit” in the git update-index manpage.
The interface is not as convenient as SVN’s (e.g. there is no way to make a sparse checkout at the time of an initial clone), but the base functionality upon which simpler interfaces could be built is now available.
仅使用 Git 不可能克隆子目录,但下面有一些解决方法。
过滤分支
您可能希望重写存储库,使其看起来好像
trunk/public_html/
是其项目根目录,并丢弃所有其他历史记录(使用filter-branch
),尝试在已经签出的分支上:注释:分隔的
--
修订选项中的过滤分支选项,以及重写所有分支和标签的--all
。 所有信息(包括原始提交时间或合并信息)都将保留。 此命令尊重.git/info/grafts
文件和refs/replace/
命名空间中的引用,因此如果您有任何移植或替换refs
已定义,运行此命令将使它们永久化。稀疏签出
以下是稀疏签出方法的简单步骤,该方法将稀疏地填充工作目录,因此您可以告诉Git 工作目录中的哪些文件夹或文件值得检查。
照常克隆存储库(
--no-checkout
是可选的):如果您已经克隆了存储库,则可以跳过此步骤。
提示:对于大型存储库,请考虑浅克隆(
--深度1) 仅签出最新版本或/和仅
--single-branch
。启用
sparseCheckout
选项:指定用于稀疏签出的文件夹(末尾没有空格):
或编辑
.git/info/sparse-checkout
。签出分支(例如
master
):现在您应该已经在当前目录中选择了文件夹。
如果您有太多级别的目录或过滤分支,您可以考虑使用符号链接。
It's not possible to clone subdirectory only with Git, but below are few workarounds.
Filter branch
You may want to rewrite the repository to look as if
trunk/public_html/
had been its project root, and discard all other history (usingfilter-branch
), try on already checkout branch:Notes: The
--
that separates filter-branch options from revision options, and the--all
to rewrite all branches and tags. All information including original commit times or merge information will be preserved. This command honors.git/info/grafts
file and refs in therefs/replace/
namespace, so if you have any grafts or replacementrefs
defined, running this command will make them permanent.Sparse checkout
Here are simple steps with sparse checkout approach which will populate the working directory sparsely, so you can tell Git which folder(s) or file(s) in the working directory are worth checking out.
Clone repository as usual (
--no-checkout
is optional):You may skip this step, if you've your repository already cloned.
Hint: For large repos, consider shallow clone (
--depth 1
) to checkout only latest revision or/and--single-branch
only.Enable
sparseCheckout
option:Specify folder(s) for sparse checkout (without space at the end):
or edit
.git/info/sparse-checkout
.Checkout the branch (e.g.
master
):Now you should have selected folders in your current directory.
You may consider symbolic links if you've too many levels of directories or filtering branch instead.
这将克隆特定文件夹并删除与其不相关的所有历史记录。
This will clone a specific folder and remove all history not related to it.
它对我有用-(git版本2.35.1)
It worked for me- (git version 2.35.1)
这就是我所做的
简单注释
sparse-checkout
git稀疏结帐init
许多文章会告诉你设置git稀疏-checkout init --cone
如果我添加--cone
会得到一些我不想要的文件.git稀疏-checkout set“...”
会将.git\info\sparse-checkout
文件内容设置为...
假设您不想使用此命令。 相反,您可以打开
git\info\sparse-checkout
然后进行编辑。示例
假设我想获取 2 文件夹完整repo 大小>10GB ↑(包含git),如下总大小 2MB
here is what I do
Simple note
sparse-checkout
git sparse-checkout init
many articles will tell you to setgit sparse-checkout init --cone
If I add--cone
will get some files that I don't want.git sparse-checkout set "..."
will set the.git\info\sparse-checkout
file contents as...
Suppose you don't want to use this command. Instead, you can open the
git\info\sparse-checkout
and then edit.Example
Suppose I want to get 2 folderfull repo size>10GB↑ (include git), as below total size < 2MB
partialclonefilter = blob:none
I know to add this line because I know from:
git clone --filter=blob:none
it will write this line. so I imitate it.git version:
git version 2.29.2.windows.3
我编写了一个 Python 2 脚本用于从 GitHub 下载子目录。
用法:
Python 3 fork https://gitlab.com/vitaly-zdanevich/github-download -dir
I wrote a Python 2 script for downloading a subdirectory from GitHub.
Usage:
Python 3 fork https://gitlab.com/vitaly-zdanevich/github-download-dir
使用Linux? 只想轻松访问和清理工作树? 而不打扰机器上的其余代码。 尝试符号链接!
测试
Using Linux? And only want easy to access and clean working tree ? without bothering rest of code on your machine. try symlinks!
Test
只是为了澄清这里的一些很好的答案,许多答案中概述的步骤假设您已经在某个地方有一个远程存储库。
给定:现有的 git 存储库,例如
[email protected]:some-user/full-repo.git
,包含一个或多个您希望独立提取存储库其余部分的目录,例如名为 < code>app1 和app2
假设您有一个如上所述的 git 存储库...
然后:您可以运行如下步骤来仅拉取 来自较大存储库的特定目录:
我错误地认为必须在原始存储库上设置稀疏签出选项,但事实并非如此:您在从本地存储库中提取之前定义了本地所需的目录偏僻的。 远程存储库不知道也不关心您只想跟踪存储库的一部分。
希望这个澄清对其他人有帮助。
Just to clarify some of the great answers here, the steps outlined in many of the answers assume that you already have a remote repository somewhere.
Given: an existing git repository, e.g.
[email protected]:some-user/full-repo.git
, with one or more directories that you wish to pull independently of the rest of the repo, e.g. directories namedapp1
andapp2
Assuming you have a git repository as the above...
Then: you can run steps like the following to pull only specific directories from that larger repo:
I had mistakenly thought that the sparse-checkout options had to be set on the original repository, but this is not the case: you define which directories you want locally, prior to pulling from the remote. The remote repo doesn't know or care about your only wanting to track a part of the repo.
Hope this clarification helps someone else.
这是我为单个子目录稀疏签出
coSubDir.sh的用例编写的 shell 脚本
Here's a shell script I wrote for the use case of a single subdirectory sparse checkout
coSubDir.sh
仅克隆来自此 repo
Example for cloning only Jetsurvey Folder from this repo
@Chronial 的答案不再适用于最新版本,但它是一个有用的答案,因为它提出了一个脚本。
为了仅检出分支的一个或多个子目录,我创建了以下 shell 函数。 它仅获取所提供目录的分支中最新版本的浅表副本。
示例使用:
在我的例子中,克隆“仅”23MB,而完整克隆则为 385MB。
使用 git 版本 2.36.1 进行测试。
@Chronial 's answer is no longer applicable to recent versions, but it was a useful answer as it proposed a script.
To checkout only one or more subdirectories of a branch, I created the following shell function. It gets a shallow copy of only the most recent version in the branch for the provided directories.
So example use:
In my case the clone was "only" 23MB versus 385MB for the full clone.
Tested with git version 2.36.1 .
我编写了一个
.gitconfig
[alias]
来执行“稀疏结帐”。 检查一下(没有双关语):在 Windows 上运行
cmd.exe
否则:
用法:
git config
命令被“缩小”为方便和存储,但这里是扩展的别名:I wrote a
.gitconfig
[alias]
for performing a "sparse checkout". Check it out (no pun intended):On Windows run in
cmd.exe
Otherwise:
Usage:
The
git config
commands are 'minified' for convenience and storage, but here is the alias expanded:这里有很多很好的回复,但我想补充一点,在 Windows Sever 2016 上使用目录名称周围的引号对我来说是失败的。文件根本没有被下载。
而不是
我必须使用
另外,如果您想简单地下载所有子目录,只需使用
Lots of great responses here, but I wanted to add that using the quotations around the directory names was failing for me on Windows Sever 2016. The files simply were not being downloaded.
Instead of
I had to use
Also, if you want to simply download all sub directories just use
如果您实际上只对目录的最新版本文件感兴趣,Github 允许您将存储库下载为 Zip 文件,该文件不包含历史记录。 所以下载速度要快得多。
If you're actually ony interested in the latest revision files of a directory, Github lets you download a repository as Zip file, which does not contain history. So downloading is very much faster.
(扩展此答案)
克隆特定标签中的子目录
如果您想克隆特定标签的特定子目录,您可以按照步骤如下。
我克隆了 cxfdistribution/src/main/release/samples/ 子目录> github 仓库
cxf -3.5.4
标签。注意:如果您尝试克隆上述存储库,您会发现它非常大。 下面的命令仅克隆需要的内容。
克隆特定分支中的子目录
我克隆了 distribution/src/main/release/samples/ 子目录>cxf github 仓库
2.6.x-fixes
分支。(extending this answer )
Cloning subdirectory in specific tag
If you want to clone a specific subdirectory of a specific tag you can follow the steps below.
I clone the
distribution/src/main/release/samples/
subdirectory of cxf github repo incxf-3.5.4
tag.Note: if you attempt to just clone the above repo you will see that it is very big. The commands below clones only what is needed.
Cloning subdirectory in specific branch
I clone the
distribution/src/main/release/samples/
subdirectory of cxf github repo in2.6.x-fixes
branch.虽然我讨厌在处理 git repos 时实际上必须使用 svn :/我一直使用它;
这允许您从 github url 复制出来而无需修改。 用法;
While I hate actually having to use svn when dealing with git repos :/ I use this all the time;
This allows you to copy out from the github url without modification. Usage;
上面有很多好的想法和脚本。 我忍不住将它们组合成一个带有帮助和错误检查的 bash 脚本:
Lots of good ideas and scripts above. I could not help myself and combined them into a bash script with help and error checking:
了解更多信息 https://www.npmjs.com /包/数字
Find out more https://www.npmjs.com/package/degit
您仍然可以使用
svn
:到“
git clone
”子目录,然后到“git pull
”这个子目录。(这并不是为了提交和推送。)
You can still use
svn
:to "
git clone
" a subdirectory and then to "git pull
" this subdirectory.(It is not intended to commit & push.)
如果您想
克隆
<块引用>
例如,我们将其重置为默认的
origin/master
的 HEAD 提交。如果你想
git init
然后远程添加
<前><代码>git init;
git remote add origin
如果需要,您可以通过运行以下命令来查看指定的跟踪文件的状态:
If如果你想退出稀疏模式并克隆所有存储库,你应该运行:
If you want to
clone
If you want to
git init
and thenremote add
If you want, you can view the status of the tracked files you have specified, by running:
If you want to exit the sparse mode and clone all the repository, you should run:
我不知道是否有人成功拉取特定目录,这是我的经验: git clone --filter=blob:none --single-branch,下载对象时立即取消,输入 repo,然后 git checkout origin/ master
I don't know if anyone succeeded pulling specific directory, here is my experience: git clone --filter=blob:none --single-branch <repo>, cancel immediately while downloading objects, enter repo, then git checkout origin/master <dir>, ignore errors (sha1), enter dir, repeat checkout (using new dir) for every sub-directory. I managed to quickly get source files in this way
对于 macOS 用户
对于 zsh 用户(特别是 macOS 用户)使用 ssh 克隆 Repos,我只是根据 @Ciro Santilli 的答案创建一个 zsh 命令:
要求:git 的版本很重要。 由于
--sparse
选项,它不适用于2.25.1
。 尝试将您的 git 升级到最新版本。 (例如测试的2.36.1
)示例用法:
代码:
For macOS users
For zsh users (macOS users, specifically) cloning Repos with ssh, I just create a zsh command based on the answer by @Ciro Santilli:
requirement: The version of git matters. It doesn't work on
2.25.1
because of the--sparse
option. Try upgrade your git to the latest version. (e.g. tested2.36.1
)example usage:
code: