在没有版本控制的情况下提供 RMarkdown 输出

Serve RMarkdown outputs without version controlling them

我们经常使用基于 RMarkdown 的包来创建带有 R 的网站(bookdownblogdowndistill...)并使用 github-页面来为html 个文件通过 url username.github.io/repo.

在这种方法中,输出(即 html / css)文件也是版本控制的,并且经常被错误地包含在提交中(git commit -a)。这很烦人,因为这些文件使提交混乱并且经常导致虚构的文件冲突。

理想情况下,输出文件根本不受版本控制,因为二进制文件(图像)还会使存储库膨胀。所以我正在寻找一个解决方案,其中:

1: 该方法应该是基于命令行的,并提供一个很好的URL访问网站

您可以在 main 和除分支之外的所有其他分支中忽略 .html.css 等,例如 gh-page 分支,您的github-页面构建自。

Git 不支持不同分支中的不同 .ignore 文件,因此您必须设置一个 bash 脚本来在每次签出新分支时替换忽略文件。请参阅此处了解如何执行此操作:https://gist.github.com/wizioo/c89847c7894ede628071

也许不是您希望的优雅解决方案,但它应该有效。

You need to tell Git to ignore the folder the book gets built into.

So, for example, by default bookdown puts all the built files in a folder called "_book"

Just add the following line to the .gitignore file in your project.

_book

Then you can work on your book and build it and push changes without worrying about the site being updated.

To update the site, you want to create a gh-pages branch that is only used for the hosted content. Do that with these commands from in your book folder:

git checkout --orphan gh-pages
git rm -rf .

# create a hidden file .nojekyll
touch .nojekyll
git add .nojekyll

git commit -m"Initial commit"
git push origin gh-pages

Make sure (once you put your book content in that branch) that GitHub is set to use that branch for hosting, rather than the folder you were using before.

Then, you can switch back to your main branch with the following command:

git checkout master

Next, you will clone your gh-pages branch into your main project:

git clone -b gh-pages https://github.com/yourprojecturl.git book-output

Then, when you have a version of the built book (in the _book folder) ready to use as your live site, use the following commands to copy the content into the book-output folder and push that to the gh-pages branch where the live site is:

cd book-output
git rm -rf *
cp -r ../_book/* ./
git add --all *
git commit -m"Update the book"
git push -q origin gh-pages

You can continue to use this last set of commands whenever you have a version in _book that you're ready to push live.

如果您的计算机上安装了python,您可以使用GitHub Pages Import,一个专门为此目的设计的工具。

您需要 python 安装,因为它必须使用 pip 安装,但是一旦安装,它就可以完美地集成到 R / RMarkdown 工作流程中。

安装后 (pip install ghp-import),您可以 运行 ghp-import docs(假设 docs 是存储 RMarkdown 输出的位置)。

您可以使用许多实用选项,包括 -p 在提交后将更改额外推送到远程。