Code Smoothie

Deploying a compiled directory with Git

Github supports most Jekyll features, but I wanted more, which meant that I had to compile it myself and upload it. I went through a lot of trouble before just sticking to a good ol’ bash script.

I initially thought of using Git hooks to automatically deploy, but that meant loss of control and more complexity.

Then I thought of using subtrees:

git subtree push --prefix _site origin live

I was really excited to see that this is exactly what I wanted—pushing a compiled directory to Github Pages.

But, it turns out, since I didn’t want _site to be version controlled and added it to .gitignore, Git will also refuse to recognize this (as far as I know with git subtree) in the same way.

What actually worked:

I really shouldn’t have thought fancy….

The concept is simple: turn the _site/ folder into its own repo. The “parent” repository will completely ignore this nested repository.

Initial Setup:

Git allows you to have a repository with its .git/ folder somewhere else. The _site/ directory would get overwritten by Jekyll, so I used another arbitrary directory, dist/, and linked it with core.worktree. Then, I linked my Github repo.

mkdir dist
git init
git config core.worktree ../../_site/
git remote add live <my_repo_url>

And then setup the script

file: publish

jekyll build       # compile the site
cd _site           # enter into build dir
git add .
git commit -m "auto-generated"
git push live master
chmod +x publish   # give it executable permissions

Now, every time I want to upload, I just change directory to my project root and run

./publish


My folder structure now looks like this:

├── _site
│   ├── about-me
|       ...
├── dist
|   ├── .git
├── publish
...

Using Github without a history

If you don’t want Github to keep your history, you can choose amend (overwrite) the latest commit, rather than adding a new commit. You must already have an initial commit before you do this.

In the script, add an --amend tag right after commit and add a --force tag right after push.


If this helped you or you have a better way, please let me know in the comments!