我如何从一个git回购签出一个文件?
当前回答
这对我很有用。使用git和一些shell命令
git clone --no-checkout --depth 1 git.example.com/project.git && cd project && git show HEAD:path/to/file_you_need > ../file_you_need && cd .. && rm -rf project
其他回答
首先使用-n选项和——depth 1选项克隆repo,前者抑制所有文件的默认签出,后者意味着它只获取每个文件的最新修订
git clone -n git://path/to/the_repo.git --depth 1
然后签出你想要的文件,就像这样:
cd the_repo
git checkout HEAD name_of_file
通常不可能像第一个回答中建议的那样,只从git下载一个文件,而不下载整个存储库。 这是因为Git不像您想象的那样存储文件(像CVS/SVN那样),而是根据项目的整个历史生成文件。
但在特定情况下有一些变通办法。下面的例子为用户、项目、分支和文件名设置了占位符。
GitHub
wget https://raw.githubusercontent.com/user/project/branch/filename
GitLab
wget https://gitlab.com/user/project/raw/branch/filename
GitWeb
如果你在服务器上使用Git - GitWeb,那么你可以在示例中尝试(将其更改为正确的路径):
wget "http://example.com/gitweb/?p=example;a=blob_plain;f=README.txt;hb=HEAD"
GitWeb 和 drupalcode.org
例子:
wget "http://drupalcode.org/project/ads.git/blob_plain/refs/heads/master:/README.md"
googlesource.com
有一个未记录的特性允许你下载base64编码的原始文件版本:
curl "https://chromium.googlesource.com/chromium/src/net/+/master/http/transport_security_state_static.json?format=TEXT" | base64 --decode
在其他情况下,检查Git存储库是否使用任何web界面。
如果它不使用任何web界面,你可以考虑将你的代码推送到外部服务,如GitHub, Bitbucket等,并将其用作镜像。
如果你没有安装wget,试试curl -O (url)。
Git checkout branch_or_version——path/file
示例:git checkout HEAD——main.c
这对我很有用。使用git和一些shell命令
git clone --no-checkout --depth 1 git.example.com/project.git && cd project && git show HEAD:path/to/file_you_need > ../file_you_need && cd .. && rm -rf project
非常简单:
git checkout from-branch-name -- path/to/the/file/you/want
这将不会签出from-branch-name分支。您将停留在您所在的分支上,并且只有该文件将从指定的分支签出。
下面是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.
向Ariejan de Vroom致敬,他在这篇博客文章中教会了我这些。
推荐文章
- 为什么我需要显式地推一个新分支?
- 如何撤消最后的git添加?
- Rubymine:如何让Git忽略Rubymine创建的.idea文件
- Gitignore二进制文件,没有扩展名
- Git隐藏错误:Git隐藏弹出并最终与合并冲突
- 如何在Python中获得所有直接子目录
- 即使模板文件存在,Flask也会引发TemplateNotFound错误
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- 如何在Ruby中创建文件
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支