Use Jekyll 3 with GitHub Pages - lmmx/devnotes GitHub Wiki

GitHub Pages does not currently support Jekyll version 3 - 2.4 at time of writing, though v3 was recently released and a pull request to the github/pages-gem repo seems ready to roll any day now.

To use Jekyll's collections (with the syntax currently documented on site), I need Jekyll 3, so will build the static site locally rather than let let GitHub do it for me, instead providing just the static site generated under the _site directory.

To switch from Jekyll 2.4 to Jekyll 3 pre-built site, version the _site output instead of the Jekyll source

Overall idea: fork from an organisational repo (in my case plotspot/plotspot.github.io) to a personal repo (I renamed mine plotspot-jekyll3 as it no longer matters what you name it now it's no longer being used with GitHub Pages). Your personal fork has the site generator, the original (*.github.io) has the generated site, within Jekyll's _site (output destination) folder.

  • Push the current non-working state as of the Jekyll 3 version of your site (this will not build, and the prior 2.4 version will still be served) so that there's a backup before modifying the git tree

    • At this point, I had removed the _site from my .gitignore, which simplifies a git filter-branch step at the end - I recommend doing this!
  • Fork on GitHub to your personal account, and rename to {site}-jekyll3 rather than {site}.github.io

  • Make this fork the new origin for the Jekyll project:

git remote set-url origin git remote set-url origin [email protected]:{user}/{site}-jekyll3.git

  or for HTTPS:

git remote set-url origin git remote set-url origin https://github.com/{user}/{site}-jekyll3


(NB ensure you have `.git` in your `.gitignore`, I don't know how recursive `.git` directories would be handled)

* Move down into the `_site` (generated static site) subdirectory and initialise a git repo for it (really only *re*initialising the existing repo on the organisational remote repo)

cd _site git init


  You'll see `Initialised empty Git repository in path/to/{*}.github.io/_site/.git/`

* Set this fresh `.git` up with the remote URL of the original repo (if HTTPS, change the URL as above)

git remote add origin [email protected]:{organisation}/{*.github.io}.git

* Step back up to the Jekyll site: `cd ..`

If at any point `_site` was tracked (i.e. if at some point in time the `_site` directory has not been declared in `.gitignore`) then you may have to run `git rm -rf _site`. You will receive a `'_site' already exists in the index` error when trying the `git submodule add` command if so ([via StackOverflow](http://stackoverflow.com/q/20929336/2668831)).

* Add the `{*.github.io}` site as a submodule of this parent local git repo in the `_site` folder, using the `-f` flag because you're forcibly adding a path in the `.gitignore`.

git submodule add -f git://github.com/{organisation}/{*.github.io}.git _site

  * you should see `Adding existing repo at '_site' to the index`

* Git has now added a `.gitmodules` file in the parent repo (or "[_superproject_](http://schacon.github.io/gitbook/5_submodules.html)") - containing the references for the `_site` submodule (pointing to the `*.github.io` repo).

[submodule "_site"] path = _site url = git://github.com/{organisation}/{*.github.io}.git


* The submodule needs filling with the remote now (i.e. the original superproject) (see [e.g. SO](http://stackoverflow.com/questions/5828324/update-git-submodule-to-latest-commit-on-origin)). First run:

git submodule init

   to add the submodule repository URLs to `.git/config` ([via Scott Chacon](http://schacon.github.io/gitbook/5_submodules.html)). The head is now detached so you're not working on a branch - specify it and grab the code from remote (_assuming you're using master branch, NB [you won't be](https://help.github.com/articles/creating-project-pages-manually/#create-a-gh-pages-branch) if it's a project GitHub Page_):

git pull origin master


   The submodule is now up to date with the `origin master` branch, and you want to discard everything above it.

* I used `git filter-branch --subdirectory-filter _site -- --all` to move all the files in `_site` up to the parent within the submodule (until now you've just put a copy of the whole Jekyll project inside itself under `_site`), and clean out the rest of the (submodule) repo. It did that, but then I was prompted to `git pull origin master`, which resulted in an apparent reversion to the original organisation. If you have all the Jekyll files then there's no need to pull here.
* At this point there's an odd-looking setup with branches, but it's nothing to worry about. I attempted to commit and push the changed refs, then `git pull origin master` as prompted, but there is no need to.
* Do this however you want to, but just retain the `.git` and `_site` folders from what was in the original repo, deleting everything else, then shift the contents of `_site/_site` out into just `_site` (i.e. make the submodule just contain the static site files). I did so by putting them in the parent and removing everything in the submodule, then bringing them back down <sup>([note RE: file deletion](http://unix.stackexchange.com/q/77127/89254))</sup>:

mkdir movingstorage mv .git movingstorage mv _site/* movingstorage mv _site/.* movingstorage mv movingstorage .. rm -r ./* rm -r ./.* mv ../movingstorage/* . mv ../movingstorage/.* . rm -r ../movingstorage/


* Everything's looking good finally, so you can now build your site, files will be generated in `_site`, and versioned separately from the submodule there.
⚠️ **GitHub.com Fallback** ⚠️