我如何从一个git回购签出一个文件?
当前回答
已有内容的两种变体:
git archive --format=tar --remote=git://git.foo.com/project.git HEAD:path/to/directory filename | tar -O -xf -
and:
git archive --format=zip --remote=git://git.foo.com/project.git HEAD:path/to/directory filename | funzip
它们将文件写入标准输出。
其他回答
最初,我在2012年git档案中提到(见Jared Forsyth的答案和Robert Knight的答案),自git1.7.9.5(2012年3月)以来,Paul Brannan的答案是:
git archive --format=tar --remote=origin HEAD:path/to/directory -- filename | tar -O -xf -
但是:在2013年,这对于远程https://github.com url来说已经不可能了。 请参阅旧页面“我可以存档存储库吗?”
当前(2018)页面“关于GitHub上的内容和数据存档”建议使用第三方服务,如GHTorrent或GH Archive。
所以你也可以处理本地拷贝/克隆:
如果您有本回答中提到的裸存储库的本地副本,您也可以执行以下操作,
git --no-pager --git-dir /path/to/bar/repo.git show branch:path/to/file >file
或者你必须首先克隆回购,这意味着你得到完整的历史:
在.git回购中 在工作树中。 但是你可以做一个稀疏签出(如果你使用Git1.7+),: 启用稀疏签出选项(git config core.)sparsecheckout真的) 在.git/info/sparse-checkout文件中添加您想要看到的内容 重新读取工作树以只显示您需要的内容
要重新阅读工作树:
$ git read-tree -m -u HEAD
这样,您就得到了一个工作树,其中精确地包含了您想要的内容(即使它只有一个文件)。
Richard Gomes指出(在评论中)“如何从git存储库中克隆、获取或稀疏签出单个目录或目录列表?”
避免下载历史记录的bash函数,它检索单个分支并检索所需的文件或目录列表。
在Git 2.40 (Q1 2023)中,通过检查稀疏模式来判断我们是否使用了“锥”模式的逻辑已经收紧,以避免将单个文件命名为指定锥的模式。
参见William Sprent (williams-unity)提交的5842710(2023年1月03日)。 (由Junio C Hamano—gitster—在commit ab85a7d中合并,2023年1月16日)
目录:检查单文件圆锥模式 署名:威廉·斯普伦特 致谢:维多利亚·戴伊
The sparse checkout documentation states that the cone mode pattern set is limited to patterns that either recursively include directories or patterns that match all files in a directory. In the sparse checkout file, the former manifest in the form: /A/B/C/ while the latter become a pair of patterns either in the form: /A/B/ !/A/B/*/ or in the special case of matching the toplevel files: /* !/*/ The 'add_pattern_to_hashsets()' function contains checks which serve to disable cone-mode when non-cone patterns are encountered. However, these do not catch when the pattern list attempts to match a single file or directory, e.g. a pattern in the form: /A/B/C This causes sparse-checkout to exhibit unexpected behaviour when such a pattern is in the sparse-checkout file and cone mode is enabled. Concretely, with the pattern like the above, sparse-checkout, in non-cone mode, will only include the directory or file located at '/A/B/C'. However, with cone mode enabled, sparse-checkout will instead just manifest the toplevel files but not any file located at '/A/B/C'. Relatedly, issues occur when supplying the same kind of filter when partial cloning with '--filter=sparse:oid=<oid>'. 'upload-pack' will correctly just include the objects that match the non-cone pattern matching. Which means that checking out the newly cloned repo with the same filter, but with cone mode enabled, fails due to missing objects. To fix these issues, add a cone mode pattern check that asserts that every pattern is either a directory match or the pattern '/*'. Add a test to verify the new pattern check and modify another to reflect that non-directory patterns are caught earlier.
我添加这个答案作为做正式签出或一些类似的本地操作的替代方案。假设您可以访问Git提供程序的web界面,您可能能够在给定的提交时直接查看任何文件。例如,在GitHub上,你可以使用这样的东西:
https://github.com/hubotio/hubot/blob/ed25584f/src/adapter.coffee
这里ed25584f是感兴趣的提交的SHA-1散列的前8个字符,后面是源文件的路径。
类似的,在Bitbucket上我们可以尝试:
https://bitbucket.org/cofarrell/stash-browse-code-plugin/src/06befe08
在本例中,我们将提交散列放在源URL的末尾。
我没有看到在这里列出的对我有用的东西,所以我将包括它,如果有人在我的情况下。
My situation, I have a remote repository of maybe 10,000 files and I need to build an RPM file for my Linux system. The build of the RPM includes a git clone of everything. All I need is one file to start the RPM build. I can clone the entire source tree which does what I need but it takes an extra two minutes to download all those files when all I need is one. I tried to use the git archive option discussed and I got “fatal: Operation not supported by protocol.” It seems I have to get some sort of archive option enabled on the server and my server is maintained by bureaucratic thugs that seem to enjoy making it difficult to get things done.
最后我进入了bitbucket的网页界面,看到了我需要的一个文件。我右键点击链接下载文件的原始副本,并从弹出的结果中选择“复制快捷方式”。我不能只是下载原始文件,因为我需要自动化操作,而且我的Linux服务器上没有浏览器界面。
为了便于讨论,结果是URL:
https://ourArchive.ourCompany.com/projects/ThisProject/repos/data/raw/foo/bar.spec?at=refs%2Fheads%2FTheBranchOfInterest
我不能直接从bitbucket存储库下载这个文件,因为我需要先登录。经过一番挖掘,我发现这个方法有效: 在Linux上:
echo "myUser:myPass123"| base64
bXlVc2VyOm15UGFzczEyMwo=
curl -H 'Authorization: Basic bXlVc2VyOm15UGFzczEyMwo=' 'https://ourArchive.ourCompany.com/projects/ThisProject/repos/data/raw/foo/bar.spec?at=refs%2Fheads%2FTheBranchOfInterest' > bar.spec
这种组合允许我下载构建其他所有内容所需的一个文件。
已有内容的两种变体:
git archive --format=tar --remote=git://git.foo.com/project.git HEAD:path/to/directory filename | tar -O -xf -
and:
git archive --format=zip --remote=git://git.foo.com/project.git HEAD:path/to/directory filename | funzip
它们将文件写入标准输出。
是的,你可以通过这个命令下载一个特定的文件
wget -o <DesiredFileName> <Git FilePath>\?token\=<personalGitToken>
例子
wget -o javascript-test-automation.md https://github.com/akashgupta03/awesome-test-automation/blob/master/javascript-test-automation.md\?token\=<githubPersonalTone>
推荐文章
- RPC失败;卷度传输已关闭,剩余未完成的读取数据
- 我应该在.gitignore文件中添加Django迁移文件吗?
- 错误:您对以下文件的本地更改将被签出覆盖
- Git rebase—即使所有合并冲突都已解决,仍然会继续报错
- 在Git中,我如何知道我的当前版本是什么?
- 跟踪所有远程git分支作为本地分支
- 自定义SSH端口上的Git
- 如何删除文件中的特定行?
- git如何显示不存在于.gitignore中的未跟踪文件
- Git错误:遇到7个文件应该是指针,但不是
- GitHub克隆与OAuth访问令牌
- 移动(或“撤销”)最后一个git提交到非暂存区域
- 我可以在GitHub上对要点进行拉请求吗?
- Hg:如何做一个像git的rebase
- 如何丢弃远程更改并将文件标记为“已解决”?