如何稀疏地从git存储库中仅签出一个文件? [英] How to sparsely checkout only one single file from a git repository?
问题描述
如何从git存储库中仅检出一个文件?
How do I checkout just one file from a git repo?
推荐答案
最初,我在2012年提到过 git archive
(请参见 Jared Forsyth 的answer 和罗伯特·奈特的答案),因为 Paul Brannan 的 answer :
Originally, I mentioned in 2012 git archive
(see Jared Forsyth's answer and Robert Knight's answer), since git1.7.9.5 (March 2012), Paul Brannan's answer:
git archive --format=tar --remote=origin HEAD:path/to/directory -- filename | tar -O -xf -
但是:在2013年,对于远程https://github.com URL ,不再可能.
>
参见旧页面"
But: in 2013, that was no longer possible for remote https://github.com URLs.
See the old page "Can I archive a repository?"
当前(2018年)页面"关于存档GitHub上的内容和数据"建议使用第三方服务,例如 GHTorrent 或
The current (2018) page "About archiving content and data on GitHub" recommends using third-party services like GHTorrent or GH Archive.
因此,您还可以处理本地副本/克隆:
So you can also deal with local copies/clone:
如果您具有此答案,
git --no-pager --git-dir /path/to/bar/repo.git show branch:path/to/file >file
或者您必须先克隆该存储库,这意味着您可以获得完整的历史记录: -在.git仓库中 -在工作树中.
Or you must clone first the repo, meaning you get the full history: - in the .git repo - in the working tree.
- 但是然后您可以执行 稀疏签出 (如果您使用的是Git1.7 +),则:
- 启用稀疏签出选项(
git config core.sparsecheckout true
) - 添加要在
.git/info/sparse-checkout
文件中看到的内容 - 重新读取工作树以仅显示您需要的内容
- But then you can do a sparse checkout (if you are using Git1.7+),:
- enable the sparse checkout option (
git config core.sparsecheckout true
) - adding what you want to see in the
.git/info/sparse-checkout
file - re-reading the working tree to only display what you need
要重新阅读工作树:
$ git read-tree -m -u HEAD
这样,您最终会得到一棵工作树,其中包括您想要的树(即使它只是一个文件)
That way, you end up with a working tree including precisely what you want (even if it is only one file)
Richard Gomes 点(
一个bash函数,避免下载历史记录,它检索单个分支,并检索所需文件或目录的列表.
A bash function which avoids downloading the history, which retrieves a single branch and which retrieves a list of files or directories you need.
这篇关于如何稀疏地从git存储库中仅签出一个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- enable the sparse checkout option (
- 启用稀疏签出选项(