我如何从一个git回购签出一个文件?
当前回答
现在我们可以了!因为这是谷歌上的第一个结果,我想我应该把它更新到最新的位置。随着git 1.7.9.5的出现,我们有了git归档命令,它将允许您从远程主机检索单个文件。
git archive --remote=git://git.foo.com/project.git HEAD:path/in/repo filename | tar -x
在这里可以看到完整的答案https://stackoverflow.com/a/5324532/290784
其他回答
Git checkout branch_or_version——path/file
示例:git checkout HEAD——main.c
我添加这个答案作为做正式签出或一些类似的本地操作的替代方案。假设您可以访问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 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回购的副本,你可以使用git日志签出一个文件的版本来找出哈希id(例如3cdc61015724f9965575ba954c8cd4232c8b42e4),然后你只需输入:
git checkout hash-id path-to-file
下面是一个实际的例子:
git checkout 3cdc61015724f9965575ba954c8cd4232c8b42e4 /var/www/css/page.css
推荐文章
- 即使模板文件存在,Flask也会引发TemplateNotFound错误
- 了解Git和GitHub的基础知识
- 没有。Git目录的Git克隆
- 如何在Ruby中创建文件
- Git与Mercurial仓库的互操作性
- 忽略git中修改(但未提交)的文件?
- “git restore”命令是什么?“git restore”和“git reset”之间有什么区别?
- Git合并与强制覆盖
- Git拉另一个分支
- 在Bash命令提示符上添加git分支
- __FILE__宏显示完整路径
- 如何更改Git日志日期格式
- git pull -rebase和git pull -ff-only之间的区别
- GitHub -致命:无法读取用户名https://github.com':没有这样的文件或目录
- 撤消git平分错误