如何从 git 存储库中稀疏地检出一个文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2466735/
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
How to sparsely checkout only one single file from a git repository?
提问by Arthur Ulfeldt
How do I checkout just one file from a git repo?
如何从 git repo 中检出一个文件?
采纳答案by VonC
Originally, I mentioned in 2012 git archive
(see Jared Forsyth's answerand Robert Knight's answer), since git1.7.9.5 (March 2012), Paul Brannan's answer:
最初,我在 2012 年提到git archive
(参见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 -
But: in 2013, that was no longer possible for remote https://github.com URLs.
See the old page "Can I archive a repository?"
但是:在 2013 年,远程 https://github.com URL不再可能。
请参阅旧页面“我可以存档存储库吗?”
The current (2018) page "About archiving content and data on GitHub" recommends using third-party services like GHTorrentor GH Archive.
当前(2018)页面“关于在 GitHub 上归档内容和数据”建议使用第三方服务,如GHTorrent或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
Or you must clone first the repo, meaning you get the full history: - in the .git repo - in the working tree.
或者您必须首先克隆 repo,这意味着您可以获得完整的历史记录: - 在 .git repo 中 - 在工作树中。
- But thenyou 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
- enable the sparse checkout option (
- 但是你可以做一个稀疏的结帐(如果你使用的是 Git1.7+),:
- 启用稀疏结帐选项 (
git config core.sparsecheckout true
) - 添加您想在
.git/info/sparse-checkout
文件中看到的内容 - 重新阅读工作树以仅显示您需要的内容
- 启用稀疏结帐选项 (
To re-read the working tree:
重新阅读工作树:
$ 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 Gomespoints (in the comments) to "How do I clone, fetch or sparse checkout a single directory or a list of directories from git repository?"
Richard Gomes指出(在评论中)“如何从 git 存储库克隆、获取或稀疏检出单个目录或目录列表?”
A bash function which avoids downloading the history, which retrieves a single branch and which retrieves a list of files or directories you need.
避免下载历史记录的 bash 函数,它检索单个分支并检索您需要的文件或目录列表。
回答by Nick Moore
First clone the repo with the -n option, which suppresses the default checkout of all files, and the --depth 1 option, which means it only gets the most recent revision of each file
首先使用 -n 选项克隆存储库,该选项禁止所有文件的默认签出,以及 --depth 1 选项,这意味着它只获取每个文件的最新版本
git clone -n git://path/to/the_repo.git --depth 1
Then check out just the file you want like so:
然后像这样检出你想要的文件:
cd the_repo
git checkout HEAD name_of_file
回答by techexpert
If you already have a copy of the git repo, you can always checkout a version of a file using a git log
to find out the hash-id (for example 3cdc61015724f9965575ba954c8cd4232c8b42e4) and then you simply type:
如果您已经拥有 git 存储库的副本,您始终可以使用 a 检出文件的版本git log
以找出哈希 ID(例如 3cdc61015724f9965575ba954c8cd4232c8b42e4),然后您只需键入:
git checkout hash-id path-to-file
Here is an actual example:
下面是一个实际的例子:
git checkout 3cdc61015724f9965575ba954c8cd4232c8b42e4 /var/www/css/page.css
回答by kenorb
Normally it's not possible to download just one file from git
without downloading the whole repository as suggested in the first answer.
It's because Git doesn't store files as you think (as CVS/SVN do), but it generates them based on the entire history of the project.
通常,如果不git
按照第一个答案中的建议下载整个存储库,就不可能只下载一个文件。这是因为 Git 不会像您想象的那样存储文件(就像 CVS/SVN 那样),而是根据项目的整个历史生成它们。
But there are some workarounds for specific cases. Examples below with placeholders for user
, project
, branch
, filename
.
但是对于特定情况有一些解决方法。下面的示例包含user
, project
, branch
, 的占位符filename
。
GitHub
GitHub
wget https://raw.githubusercontent.com/user/project/branch/filename
GitLab
GitLab
wget https://gitlab.com/user/project/raw/branch/filename
GitWeb
GitWeb
If you're using Git on the Server - GitWeb, then you may try in example (change it into the right path):
如果您在服务器上使用Git - GitWeb,那么您可以尝试示例(将其更改为正确的路径):
wget "http://example.com/gitweb/?p=example;a=blob_plain;f=README.txt;hb=HEAD"
GitWeb at drupalcode.org
在 drupalcode.org 上的 GitWeb
Example:
例子:
wget "http://drupalcode.org/project/ads.git/blob_plain/refs/heads/master:/README.md"
googlesource.com
googlesource.com
There is an undocumented feature that allows you to download base64-encoded versions of raw files:
有一个未公开的功能允许您下载原始文件的 base64 编码版本:
curl "https://chromium.googlesource.com/chromium/src/net/+/master/http/transport_security_state_static.json?format=TEXT" | base64 --decode
In other cases check if your Git repository is using any web interfaces.
在其他情况下,请检查您的 Git 存储库是否正在使用任何 Web 界面。
If it's not using any web interface, you may consider to push your code to external services such as GitHub, Bitbucket, etc. and use it as a mirror.
如果它不使用任何网络界面,你可以考虑在你的代码推到外部服务,例如GitHub的,到位桶,等等。并将其用作镜子。
If you don't have wget
installed, try curl -O (url)
alternatively.
如果您还没有wget
安装,请尝试curl -O (url)
替代。
回答by Thomas Brushington
git checkout branch_or_version -- path/file
git checkout branch_or_version -- 路径/文件
example: git checkout HEAD -- main.c
例子: git checkout HEAD -- main.c
回答by Geoffrey Hale
Minimal Guide
最小指南
git checkout -- <filename>
git checkout -- <filename>
Ref: https://git-scm.com/docs/git-checkout
参考:https: //git-scm.com/docs/git-checkout
回答by Jared Forsyth
Now we can!As this is the first result on google, I thought I'd update this to the latest standing. With the advent of git 1.7.9.5, we have the git archive
command which will allow you to retrieve a single file from a remote host.
现在我们可以了!由于这是谷歌上的第一个结果,我想我会将其更新为最新状态。随着 git 1.7.9.5 的出现,我们有了git archive
允许您从远程主机检索单个文件的命令。
git archive --remote=git://git.foo.com/project.git HEAD:path/in/repo filename | tar -x
See answer in full here https://stackoverflow.com/a/5324532/290784
回答by anvk
Working in GIT 1.7.2.2
使用 GIT 1.7.2.2
For example you have a remote some_remotewith branches branch1, branch32
例如,你有一个远程some_remote与分支branch1,branch32
so to checkout a specific file you call this commands:
所以要签出一个特定的文件,你调用这个命令:
git checkout remote/branch path/to/file
as an example it will be something like this
例如,它将是这样的
git checkout some_remote/branch32 conf/en/myscript.conf
git checkout some_remote/branch1 conf/fr/load.wav
This checkout command will copy the whole file structure conf/en and conf/fr into the current directory where you call these commands (of course I assume you ran git init at some point before)
这个 checkout 命令会将整个文件结构 conf/en 和 conf/fr 复制到你调用这些命令的当前目录中(当然我假设你之前在某个时候运行过 git init )
回答by broc.seib
Very simple:
很简单:
git checkout from-branch-name -- path/to/the/file/you/want
This will not checkout the from-branch-name
branch. You will stay on whatever branch you are on, and only that single file will be checked out from the specified branch.
这不会结帐from-branch-name
分支。您将停留在您所在的任何分支上,并且只会从指定的分支中检出该单个文件。
Here's the relevant part of the manpage for git-checkout
这是联机帮助页的相关部分 git-checkout
git checkout [-p|--patch] [<tree-ish>] [--] <pathspec>...
When <paths> or --patch are given, git checkout does not switch
branches. It updates the named paths in the working tree from the
index file or from a named <tree-ish> (most often a commit). In
this case, the -b and --track options are meaningless and giving
either of them results in an error. The <tree-ish> argument can be
used to specify a specific tree-ish (i.e. commit, tag or tree) to
update the index for the given paths before updating the working
tree.
Hat tip to Ariejan de Vroom who taught me this from this blog post.
向 Ariejan de Vroom 致敬,他从这篇博文中教会了我这一点。
回答by Fizer Khan
You can do it by
你可以通过
git archive --format=tar --remote=origin HEAD | tar xf -
git archive --format=tar --remote=origin HEAD <file> | tar xf -