如何从 git 存储库中稀疏地检出一个文件? [英] How to sparsely checkout only one single file from a git repository?

查看:29
本文介绍了如何从 git 存储库中稀疏地检出一个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何只检出 git 存储库中的一个文件?

How do I checkout just one file from a git repo?

推荐答案

原来我在 2012 年提到过 git archive(参见 Jared Forsyth答案Robert Knightanswer),因为 git1.7.9.5(2012 年 3 月)Paul Brannan答案:

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 上的内容和数据"建议使用第三方服务,例如 GHTorrentGH 档案.

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:

如果您有 这个答案 中提到的裸存储库的本地副本,您也可以执行以下操作,

You could alternatively do the following if you have a local copy of the bare repository as mentioned in this answer,

git --no-pager --git-dir /path/to/bar/repo.git show branch:path/to/file >file

<小时>

或者您必须先克隆 repo,这意味着您将获得完整的历史记录:- 在 .git 仓库中- 在工作树中.


Or you must clone first the repo, meaning you get the full history: - in the .git repo - in the working tree.

  • 然后你可以做一个sparse checkout(如果您使用的是 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 点数 (在评论中)到如何从 git 存储库克隆、获取或稀疏检出单个目录或目录列表?"

      避免下载历史记录的 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆