git 从存储库中检索单个文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1125476/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:43:54  来源:igfitidea点击:

Retrieve a single file from a repository

gitgit-checkoutsparse-checkoutgit-sparse-checkout

提问by

What is the most efficient mechanism (in respect to data transferred and disk space used) to get the contents of a single file from a remote git repository?

从远程 git 存储库获取单个文件内容的最有效机制是什么(就传输的数据和使用的磁盘空间而言)?

So far I've managed to come up with:

到目前为止,我已经设法想出了:

git clone --no-checkout --depth 1 [email protected]:foo/bar.git && cd bar && git show HEAD:path/to/file.txt

This still seems overkill.

这似乎仍然有些矫枉过正。

What about getting multiple files from the repo?

从 repo 中获取多个文件怎么样?

回答by Yisrael Dov

in git version 1.7.9.5 this seems to work to export a single file from a remote

在 git 版本 1.7.9.5 中,这似乎可以从远程导出单个文件

git archive --remote=ssh://host/pathto/repo.git HEAD README.md

This will cat the contents of the file README.md.

这将 cat 文件的内容README.md

回答by Robert Knight

Following on from Jakub's answer. git archiveproduces a tar or zip archive, so you need to pipe the output through tar to get the file content:

Jakub回答之后git archive生成 tar 或 zip 存档,因此您需要通过 tar 管道输出以获取文件内容:

git archive --remote=git://git.foo.com/project.git HEAD:path/to/directory filename | tar -x

Will save a copy of 'filename' from the HEAD of the remote repository in the current directory.

将从远程存储库的 HEAD 中保存当前目录中的“文件名”副本。

The :path/to/directorypart is optional. If excluded, the fetched file will be saved to <current working dir>/path/to/directory/filename

:path/to/directory部分是可选的。如果排除,则提取的文件将保存到<current working dir>/path/to/directory/filename

In addition, if you want to enable use of git archive --remoteon Git repositories hosted by git-daemon, you need to enable the daemon.uploadarch config option. See https://kernel.org/pub/software/scm/git/docs/git-daemon.html

此外,如果您想启用git archive --remote由 git-daemon 托管的 Git 存储库,则需要启用 daemon.uploadarch 配置选项。见https://kernel.org/pub/software/scm/git/docs/git-daemon.html

回答by Jakub Nar?bski

If there is web interfacedeployed (like gitweb, cgit, Gitorious, ginatra), you can use it to download single file ('raw' or 'plain' view).

如果部署了Web 界面(如 gitweb、cgit、Gitorious、ginatra),您可以使用它来下载单个文件('raw' 或 'plain' 视图)。

If other side enabled it, you can use git archive's '--remote=<URL>' option (and possibly limit it to a directory given file resides in), for example:

如果其他方启用它,您可以使用git archive的 ' --remote=<URL>' 选项(并可能将其限制为给定文件所在的目录),例如:

$ git archive [email protected]:foo/bar.git --prefix=path/to/ HEAD:path/to/ |  tar xvf -

回答by Ankur Agarwal

Not in general but if you are using Github:

不是一般的,但如果你使用 Github:

For me wgetto the raw url turned out to be the best and easiest way to download one particular file.

对我来说wget,原始 url 是下载特定文件的最佳和最简单的方法。

Open the file in the browser and click on "Raw" button. Now refresh your browser, copy the url and do a wgetor curlon it.

在浏览器中打开文件,然后单击“原始”按钮。现在刷新您的浏览器,复制 url 并在其上执行 awgetcurl

wget example:

wget 示例:

wget 'https://github.abc.abc.com/raw/abc/folder1/master/folder2/myfile.py?token=DDDDnkl92Kw8829jhXXoxBaVJIYW-h7zks5Vy9I-wA%3D%3D' -O myfile.py

wget 'https://github.abc.abc.com/raw/abc/folder1/master/folder2/myfile.py?token=DDDDnkl92Kw8829jhXXoxBaVJIYW-h7zks5Vy9I-wA%3D%3D' -O myfile.py

Curl example:

卷曲示例:

curl 'https://example.com/raw.txt' > savedFile.txt

curl 'https://example.com/raw.txt' > savedFile.txt

回答by Kousha

To export a single file from a remote:

从远程导出单个文件:

git archive --remote=ssh://host/pathto/repo.git HEAD README.md | tar -x

This will download the file README.mdto your current directory.

这会将文件下载README.md到您的当前目录。

If you want the contents of the file exported to STDOUT:

如果要将文件内容导出到 STDOUT:

git archive --remote=ssh://host/pathto/repo.git HEAD README.md | tar -xO

You can provide multiple paths at the end of the command.

您可以在命令末尾提供多个路径。

回答by Mars Robertson

It looks like a solution to me: http://gitready.com/intermediate/2009/02/27/get-a-file-from-a-specific-revision.html

它对我来说似乎是一个解决方案:http: //gitready.com/intermediate/2009/02/27/get-a-file-from-a-specific-revision.html

git show HEAD~4:index.html > local_file

where 4means four revision from now and ~is a tilde as mentioned in the comment.

where4表示从现在开始的四次修订,并且~是评论中提到的波浪号。

回答by Steven Penny

I use this

我用这个

$ cat ~/.wgetrc
check_certificate = off

$ wget https://raw.github.com/jquery/jquery/master/grunt.js
HTTP request sent, awaiting response... 200 OK
Length: 11339 (11K) [text/plain]
Saving to: `grunt.js'

回答by Willem van Ketwich

A nuanced variant of some of the answers here that answers the OP's question:

此处某些答案的细微变体可回答 OP 的问题:

git archive [email protected]:foo/bar.git \
  HEAD path/to/file.txt | tar -xO path/to/file.txt > file.txt

回答by panticz

If you repository supports tokens (for example GitLab) then generate a token for your user then navigate to the file you will download and click on RAW output to get the URL. To download the file use:

如果您的存储库支持令牌(例如 GitLab),则为您的用户生成一个令牌,然后导航到您将下载的文件并单击 RAW 输出以获取 URL。要下载文件,请使用:

curl --silent --request GET --header 'PRIVATE-TOKEN: replace_with_your_token' \
'http://git.example.com/foo/bar.sql' --output /tmp/bar.sql

回答by matiasmasca

I solved in this way:

我是这样解决的:

git archive --remote=ssh://[email protected]/user/mi-repo.git BranchName /path-to-file/file_name | tar -xO /path-to-file/file_name > /path-to-save-the-file/file_name

If you want, you could replace "BranchName" for "HEAD"

如果需要,您可以将“BranchName”替换为“HEAD”