git 从 GitHub 存储库下载单个文件夹或目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7106012/
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
Download a single folder or directory from a GitHub repo
提问by g_inherit
How can I download only a specific folder or directory from a remote Git repo hosted on GitHub?
如何从托管在 GitHub 上的远程 Git 存储库中仅下载特定文件夹或目录?
Say the example GitHub repo lives here:
假设示例 GitHub 存储库位于此处:
[email protected]:foobar/Test.git
Its directory structure:
其目录结构:
Test/
├── foo/
│ ├── a.py
│ └── b.py
└── bar/
├── c.py
└── d.py
I want to download only the foofolder and not clone the whole Test project.
我只想下载foo文件夹而不是克隆整个测试项目。
回答by nick
Update Sep. 2016: there are a few tools created by the community that can do this for you:
2016 年 9 月更新:社区创建了一些工具可以为您执行此操作:
- GitZip(Credits to Kino- upvote his answer right here!)
- DownGit(Credits to Minhas Kamal- upvote his answer right here!)
Git doesn't support this, but Github does via SVN. If you checkout your code with subversion, Github will essentially convert the repo from git to subversion on the backend, then serve up the requested directory.
Git 不支持,但 Github 通过 SVN 支持。如果您使用 subversion 签出代码,Github 基本上会将 repo 从 git 转换为后端的 subversion,然后提供请求的目录。
Here's how you can use this feature to download a specific folder. I'll use the popular javascript library lodash
as an example.
以下是如何使用此功能下载特定文件夹。我将使用流行的 javascript 库lodash
作为示例。
Navigate to the folder you want to download. Let's download
/test
frommaster
branch.Modify the URL for subversion. Replace
tree/master
withtrunk
.https://github.com/lodash/lodash/tree/master/test
?https://github.com/lodash/lodash/trunk/test
Download the folder. Go to the command line and grab the folder with SVN.
修改 subversion 的 URL。替换
tree/master
为trunk
。https://github.com/lodash/lodash/tree/master/test
?https://github.com/lodash/lodash/trunk/test
下载文件夹。转到命令行并使用 SVN 抓取文件夹。
svn checkout https://github.com/lodash/lodash/trunk/test
You might not see any activity immediately because Github takes up to 30 seconds to convert larger repositories, so be patient.
您可能不会立即看到任何活动,因为 Github 最多需要 30 秒来转换较大的存储库,因此请耐心等待。
Full URL format explanation:
- If you're interested in
master
branch, usetrunk
instead. So the full path istrunk/foldername
- If you're interested in
foo
branch, usebranches/foo
instead. The full path looks likebranches/foo/foldername
- Protip: You can use
svn ls
to see available tags and branches before downloading if you wish
完整的 URL 格式说明:
- 如果您对
master
分支感兴趣,请trunk
改用。所以完整的路径是trunk/foldername
- 如果您对
foo
分支感兴趣,请branches/foo
改用。完整路径看起来像branches/foo/foldername
- Protip:
svn ls
如果您愿意,您可以在下载前查看可用的标签和分支
That's all! Github supports more subversion featuresas well, including support for committing and pushing changes.
就这样!Github 还支持更多的颠覆功能,包括支持提交和推送更改。
回答by Kino
Two options for this feature:
此功能的两个选项:
Option 1: GitZip Browser Extension
选项 1:GitZip 浏览器扩展
Usage:
用法:
- In any GitHub repos page.
- Just double click on the blank part of the items you need.
- Click download button at bottom-right.
- See the progress dashboard and wait for browser trigger download.
- Get the ZIP file.
- 在任何 GitHub 存储库页面中。
- 只需双击您需要的项目的空白部分。
- 点击右下角的下载按钮。
- 查看进度仪表板并等待浏览器触发器下载。
- 获取 ZIP 文件。
Get Token:
获取令牌:
- Click GitZip Extension icon on your browser.
- Click "Normal" or "Private" link besides "Get Token".
- Authorize GitZip permission on Github auth page.
- Back to repo page of the beginning.
- Continue to use.
- 单击浏览器上的 GitZip 扩展图标。
- 点击“获取令牌”旁边的“普通”或“私人”链接。
- 在 Github auth 页面上授权 GitZip 权限。
- 回到开头的 repo 页面。
- 继续使用。
Option 2: Github gh-page
选项 2:Github gh-page
http://kinolien.github.io/gitzipby using GitHub API, and JSZip, FileSaver.js libraries.
http://kinolien.github.io/gitzip使用 GitHub API 和 JSZip、FileSaver.js 库。
Step1: Input github url to the field at the top-right.
Step2: Press enter or click download for download zip directly or click search for view the list of sub-folders and files.
Step3: Click "Download Zip File" or "Get File" button to get files.
Step1:在右上角的字段中输入github url。
Step2:按回车键或者直接点击下载下载zip或者点击搜索查看子文件夹和文件列表。
第三步:点击“下载 Zip 文件”或“获取文件”按钮来获取文件。
In most cases, it works fine, except that the folder contains more than 1,000 files, because of the Github Trees API limitation. (refers to Github API#Contents)
在大多数情况下,它工作正常,除了文件夹包含 1,000 多个文件,因为 Github Trees API 限制。(参考Github API#Contents)
And it also can support private/public repos and upgrade the rate limit, if you have GitHub account and use "get token" link in this site.
如果您有 GitHub 帐户并在本站使用“获取令牌”链接,它还可以支持私有/公共 repos 和升级速率限制。
回答by Minhas Kamal
Go to DownGit> Enter Your URL > Download!
转到DownGit> 输入您的 URL > 下载!
Now, you can DIRECTLY DOWNLOADor create DOWNLOAD LINKfor any GitHub public directory or file (specially large file) from DownGit! Here is a simple demonstration-
现在,您可以从DownGit直接下载或创建任何 GitHub 公共目录或文件(特别是大文件)的下载链接!这是一个简单的演示-
You may also configure the downloaded file's property- detailed usage.
您还可以配置下载文件的属性-详细用法。
回答by janos
If you have svn
, you can use svn export
to do this:
如果你有svn
,你可以用它svn export
来做到这一点:
svn export https://github.com/foobar/Test.git/trunk/foo
Notice the URL format:
注意 URL 格式:
- The base URL is
https://github.com/
/trunk
appended at the end
- 基本网址是
https://github.com/
/trunk
附加在最后
Before you run svn export
, it's good to first verify the content of the directory with:
在运行之前svn export
,最好先验证目录的内容:
svn ls https://github.com/foobar/Test.git/trunk/foo
回答by RobW
For a Generic git Repo:
对于通用 git 仓库:
If you want to download files, not clone the repository with history, you can do this with git-archive
.
如果您想下载文件,而不是使用历史记录克隆存储库,您可以使用git-archive
.
git-archive
makes a compressed zip or tar archive of a git repository. Some things that make it special:
git-archive
制作 git 存储库的压缩 zip 或 tar 存档。一些使它特别的事情:
- You can choose which files or directories in the git repository to archive.
- It doesn't archive the
.git/
folder, or any untracked files in the repository it's run on. - You can archive a specific branch, tag, or commit. Projects managed with git often use this to generate archives of versions of the project (beta, release, 2.0, etc.) for users to download.
- 您可以选择要归档 git 存储库中的哪些文件或目录。
- 它不会存档
.git/
文件夹或它运行的存储库中的任何未跟踪文件。 - 您可以归档特定的分支、标记或提交。使用 git 管理的项目通常使用它来生成项目版本(测试版、发行版、2.0 等)的存档供用户下载。
An example of creating an archive of the docs/usage
directory from a remote repo you're connected to with ssh:
docs/usage
从使用 ssh 连接到的远程存储库创建目录存档的示例:
# in terminal
$ git archive --format tar --remote ssh://server.org/path/to/git HEAD docs/usage > /tmp/usage_docs.tar
More information in this blog postand the git documentation.
Note on GitHub Repos:
GitHub 存储库注意事项:
回答by VahidN
I've created an open source project, called GitHubFolderDownloader. It lets you to download a single folder of a repository without cloning or downloading the whole repository.
我创建了一个名为GitHubFolderDownloader 的开源项目。它允许您下载存储库的单个文件夹,而无需克隆或下载整个存储库。
回答by Tommie C.
Nothing wrong with other answers but I just thought I'd share step-by-step instructions for those wandering through this process for the first time.
其他答案没有错,但我只是想为那些第一次在这个过程中徘徊的人分享分步说明。
How to download a single folder from a github repository (Mac OS X):
如何从 github 存储库 (Mac OS X) 下载单个文件夹:
~ To open Terminal just click spotlight and type terminal then hit enter
~ 要打开终端,只需单击聚光灯并输入终端,然后按 Enter
- On a Mac you likely already have SVN (to test just open terminal and type "svn"or "which svn"~ without the quote marks)
- On Github: Locate the Github path to your git folder (not the repo) by clicking the specific folder name within a repo
- Copy the path from the address bar of the browser
- Open Terminaland type: svn export
- Next paste in the address (eg.): https://github.com/mingsai/Sample-Code/tree/master/HeadsUpUI
- Replace the words: tree/master
- with the word: trunk
- Type in the destination folder for the files (in this example, I store the target folder inside of the Downloads folder for the current user)
- Here space is just the spacebar not the word (space) ~/Downloads/HeadsUpUI
- The final terminal command shows the full command to download the folder (compare the address to step 5) svn export https://github.com/mingsai/Sample-Code/trunk/HeadsUpUI~/Downloads/HeadsUpUI
- 在 Mac 上,您可能已经拥有 SVN(测试只需打开终端并键入“svn”或“which svn”~不带引号)
- 在 Github 上:通过单击存储库中的特定文件夹名称,找到您的 git 文件夹(而不是存储库)的 Github 路径
- 从浏览器地址栏复制路径
- 打开终端并输入:svn export
- 接下来粘贴地址(例如):https: //github.com/mingsai/Sample-Code/tree/master/HeadsUpUI
- 替换单词:tree/master
- 用词:trunk
- 输入文件的目标文件夹(在本例中,我将目标文件夹存储在当前用户的下载文件夹中)
- 这里的空格只是空格键而不是单词(空格)~/Downloads/HeadsUpUI
- 最后的终端命令显示了下载文件夹的完整命令(地址与步骤 5 比较)svn export https://github.com/mingsai/Sample-Code/trunk/HeadsUpUI~/Downloads/HeadsUpUI
BTW - If you are on Windows or some other platform you can find a binary download of subversion (svn) at http://subversion.apache.org
顺便说一句 - 如果您使用的是 Windows 或其他平台,您可以在http://subversion.apache.org 上找到 subversion (svn) 的二进制下载
~ If you want to checkout the folder rather than simply download it try using the svn help (tldr: replace exportwith checkout)
〜如果您想签出文件夹而不是简单地下载它,请尝试使用 svn 帮助(tldr:用checkout替换export)
Update
更新
Regarding the comment on resuming an interrupted download/checkout. I would try running svn cleanup
followed by svn update
. Please search SO for additional options.
关于恢复中断下载/结帐的评论。我会尝试跑步,svn cleanup
然后是svn update
. 请搜索 SO 以获取其他选项。
回答by Mohammed Jafar
Whoever is working on specific folder he needs to clone that particular folder itself, to do so please follow below steps by using sparse checkout.
无论谁正在处理特定文件夹,他都需要克隆该特定文件夹本身,为此请使用稀疏结帐按照以下步骤操作。
Create a directory.
Initialize a Git repository. (
git init
)Enable Sparse Checkouts. (
git config core.sparsecheckout true
)Tell Git which directories you want (echo 2015/brand/May( refer to folder you want to work on) >>
.git/info/sparse-checkout
)Add the remote (
git remote add -f origin https://jafartke.com/mkt-imdev/DVM.git
)Fetch the files (
git pull origin master
)
创建目录。
初始化 Git 存储库。(
git init
)启用稀疏结帐。(
git config core.sparsecheckout true
)告诉 Git 你想要的目录(echo 2015/brand/May(指你想要处理的文件夹)>>
.git/info/sparse-checkout
)添加遥控器 (
git remote add -f origin https://jafartke.com/mkt-imdev/DVM.git
)获取文件 (
git pull origin master
)
回答by michel-slm
You cannot; unlike Subversion, where each subdirectory can be checked out individually, Git operates on a whole-repository basis.
你不能; 与 Subversion 不同,每个子目录都可以单独检出,Git 在整个存储库的基础上运行。
For projects where finer-grained access is necessary, you can use submodules -- each submodule is a separate Git project, and thus can be cloned individually.
对于需要更细粒度访问的项目,您可以使用子模块——每个子模块都是一个单独的 Git 项目,因此可以单独克隆。
It is conceivable that a Git front-end (e.g. GitHub's web interface, or gitweb) could choose to provide an interface for you to extract a given folder, but to my knowledge none of them do that (though they do let you download individual files, so if the folder does not contain too many files, that is an option)
可以想象,Git 前端(例如 GitHub 的 Web 界面或 gitweb)可以选择为您提供一个界面来提取给定文件夹,但据我所知,它们都没有这样做(尽管它们确实让您下载单个文件,所以如果文件夹不包含太多文件,这是一个选项)
Edit- GitHub actually offers access via SVN, which would allow you to do just this (as per comment). See https://github.com/blog/1438-improved-svn-here-to-stay-old-svn-going-awayfor latest instructions on how to do this
编辑- GitHub 实际上通过 SVN 提供访问权限,这将允许您执行此操作(根据评论)。有关如何执行此操作的最新说明,请参阅https://github.com/blog/1438-improved-svn-here-to-stay-old-svn-going-away
回答by johnny
If you truly just want to just "download"the folder and not "clone"it (for development), the easiest way to simply get a copyof the most recent version of the repository (and therefore a folder/file within it), without needing to clone the whole repo or even install git in the first place, is to download a zip archive (for any repo, fork, branch, commit, etc.) by going to the desired repository/fork/branch/commit on GitHub (e.g. http(s)://github.com/<user>/<repo>/commit/<Sha1>
for a copy of the files as they were after a specific commit) and selecting the Downloads
button near the upper-right.
如果您真的只想“下载”文件夹而不是“克隆”它(用于开发),那么最简单的方法就是获取最新版本的存储库副本(以及其中的文件夹/文件),无需克隆整个 repo 或什至首先安装 git,是通过转到 GitHub 上的所需存储库/fork/branch/commit 下载 zip 存档(对于任何 repo、fork、分支、提交等) (例如,http(s)://github.com/<user>/<repo>/commit/<Sha1>
对于特定提交后的文件副本)并选择Downloads
右上角附近的按钮。
This archive format contains none of the git-repo magic, just the tracked files themselves (and perhaps a few .gitignore files if they were tracked, but you can ignore those :p) - that means that if the code changes and you want to stay on top, you'll have to manually re-download it, and it also means you won't be able to use it as a git repository...
这种存档格式不包含任何 git-repo 魔法,只包含被跟踪的文件本身(如果被跟踪,可能还有一些 .gitignore 文件,但您可以忽略那些 :p)——这意味着如果代码更改并且您想要保持领先,您必须手动重新下载它,这也意味着您将无法将其用作 git 存储库...
Not sure if that's what you're looking for in this case (again, "download"/view vs "clone"/develop), but it can be useful nonetheless...
不确定这是否是您在这种情况下要寻找的内容(再次,“下载”/查看与“克隆”/开发),但它仍然很有用......