从存储库中检索单个文件

206 git git-checkout sparse-checkout git-sparse-checkout

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

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

git clone --no-checkout --depth 1 git@github.com:foo/bar.git && cd bar && git show HEAD:path/to/file.txt
Run Code Online (Sandbox Code Playgroud)

这仍然有点矫枉过正.

如何从回购中获取多个文件?

Yis*_*Dov 136

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

git archive --remote=ssh://host/pathto/repo.git HEAD README.md
Run Code Online (Sandbox Code Playgroud)

这将捕获文件的内容README.md.

  • ...除非它不适用于GitHub.荡.:( https://twitter.com/GitHubHelp/status/322818593748303873 (25认同)
  • @FrerichRaabe只需添加`| tar -x`命令.`git archive --remote = ssh://host/pathto/repo.git HEAD README.md | tar -x``cat README.md` (18认同)
  • 这似乎不会产生原始文件,而是只有一个文件的tar文件. (12认同)
  • 您可以使用`tar -xO`输出到STDOUT进行管道输送,例如`FILE = README.md && git archive --remote = ssh://host/pathto/repo.git HEAD"$ FILE"| tar -xO"$ FILE"` (11认同)
  • 正是我正在寻找的答案,但我得到“致命:协议不支持的操作”。来自 Git 的回应。啊。 (3认同)
  • 当我尝试在 GitHub 上托管的存储库上使用它时,出现错误:__fatal:协议不支持操作.__。如果此方法不适用于 GitHub 使用的协议,那么它可以被安全地视为“不可靠”。 (2认同)
  • 对于那些心烦意乱的人来说,这是字母 O 而不是数字零:“-xO”。它使输出成为标准输出,而不是将文件保存在当前目录中。 (2认同)

Rob*_*ght 59

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

git archive --remote=git://git.foo.com/project.git HEAD:path/to/directory filename | tar -x
Run Code Online (Sandbox Code Playgroud)

将从当前目录中的远程存储库的HEAD保存"filename"的副本.

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

此外,如果要启用git archive --remotegit-daemon托管的Git存储库,则需要启用daemon.uploadarch配置选项.请参阅https://kernel.org/pub/software/scm/git/docs/git-daemon.html

  • 如果它是一个文本文件,我们想将它保存到另一个部分,那么使用它是直接的:| tar -xO>〜/ destfile.ext (2认同)

Jak*_*ski 42

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

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

$ git archive --remote=git@github.com:foo/bar.git --prefix=path/to/ HEAD:path/to/ |  tar xvf -
Run Code Online (Sandbox Code Playgroud)

  • 对于您自己的存储库,如果在远程存储库上使用git-daemon(git:// style urls)和`git config daemon.uploadarch true`,则需要专门启用upload-archive.默认情况下,git守护程序禁用远程存档,其中包含"致命:远程错误:拒绝访问或未导出存储库:..." (7认同)
  • 对于它的价值,它看起来不适用于GitHub托管的存储库.请参阅https://help.github.com/articles/can-i-archive-a-repository和https://groups.google.com/forum/#!topic/github/z8vLHcX0HxY (4认同)

Ank*_*wal 26

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

对于我来说wget,原始网址被证明是下载一个特定文件的最佳和最简单的方法.

在浏览器中打开文件,然后单击"原始"按钮.现在,刷新浏览器,复制该网址并做wgetcurl就可以了.

wget示例:

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

卷曲示例:

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

  • 这是最简单的解决方案,适用于任何可以找到的原始文本.`curl https://example.com/raw.txt> savedFile.txt` (3认同)
  • 解决办法是问Git,答案认可Github是git,与git无关。它基于著名 git 解决方案提供商提供的附加 API! (3认同)

Kou*_*sha 15

要从远程导出单个文件:

git archive --remote=ssh://host/pathto/repo.git HEAD README.md | tar -x
Run Code Online (Sandbox Code Playgroud)

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

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

git archive --remote=ssh://host/pathto/repo.git HEAD README.md | tar -xO
Run Code Online (Sandbox Code Playgroud)

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


ATo*_*ras 10

如果没有其他答案有效(即限制 GitLab 访问),您可以通过以下方式进行“选择性结帐”:

  1. git clone --no-checkout --depth=1 --no-tags URL
  2. git restore --staged DIR-OR-FILE
  3. git checkout DIR-OR-FILE

尽管此解决方案 100% 兼容 git,并且您可以签出目录,但它不像对文件执行 wget/curl 那样在磁盘或网络上都是最佳的。


Mar*_*son 8

它看起来像我的解决方案:http://gitready.com/intermediate/2009/02/27/get-a-file-from-a-specific-revision.html

git show HEAD~4:index.html> local_file

其中4意味着从现在起四次修订,并且~是评论中提到的代字号.

  • 这似乎不会从远程存储库获取文件,就像OP需要的那样. (17认同)
  • @MichalStefanow:Mike Weller拥有它; 具体而言,这不适用于远程存储库.你至少需要一个本地克隆,即使你已经设置了遥控器. (9认同)

mat*_*sca 8

我是这样解决的:

git archive --remote=ssh://git@gitlab.com/user/mi-repo.git BranchName /path-to-file/file_name | tar -xO /path-to-file/file_name > /path-to-save-the-file/file_name
Run Code Online (Sandbox Code Playgroud)

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


小智 6

我用这个

$ 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'
Run Code Online (Sandbox Code Playgroud)

  • 这仅适用于公共存储库.对于私有存储库,您需要身份验证. (2认同)

Wil*_*ich 5

此处一些答案的细微差别,可以回答OP的问题:

git archive --remote=git@archive-accepting-git-server.com:foo/bar.git \
  HEAD path/to/file.txt | tar -xO path/to/file.txt > file.txt
Run Code Online (Sandbox Code Playgroud)

  • Err .. git不仅用于GitHub (2认同)
  • 不过,您在答案中明确将 --remote 设置为 github URL ;-) (2认同)

小智 5

在我看来,使用以下内容是最简单的方法:

wget https://github.com/name/folder/file.zip?raw=true
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,确实简单。要删除保存文件末尾的“?raw=true”,可以在上述命令末尾使用:“``-O your-file-name``”。 (2认同)