有时候我们需要下载别人 GitHub 仓库的某一个文件,但是不得不 clone 整个项目,此时需要一种能直接下载某一个文件的方式。
只需要更改一下 GitHub 文件 URL,就能实现单个下载功能。
假设 GitHub 文件的原 URL 是:
https://github.com/DeppWang/Java-Books/blob/master/docs/Algorithms, 4th Edition.pdf
将其更改为:
https://raw.githubusercontent.com/DeppWang/Java-Books/master/docs/Algorithms, 4th Edition.pdf
即,将 github.com 替换为 raw.githubusercontent.com,并去除 /blob。
这样就能直接下载这个 Algorithms, 4th Edition.pdf 文件了
原理
raw.githubusercontent.com 返回存储在 GitHub 中的文件的 raw content(原始内容),因此可以将它们简单地下载到计算机上。可以在网页上右键查看源文件的方式验证文件 URL 是否包含 raw.githubusercontent.com。
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
参考
如何让别人直接在线阅读自己 Github 中的 PDF 文件
当 PDF 文件较大时,在 GitHub 中不能直接预览显示。可借助 GitHub 的 GitHub Pages 实现这个功能。
GitHub Pages 可以将 repo 转