Deploy A Subdirectory To A Git Branch

Jan 23, 2015Ryan Burnette

I love using Jekyll and Github Pages together. In my case I use some custom plugins so I compile the site myself and push the changes over to a gh-pages branch.

The other day I was looking at the jekyll-proteus Gem the Thoughtbot folks put together. They had a little deploy script in there that would push changes into the gh-pages branch. I thought that it was shiny and went looking for some background info.

I started looking for a bit of documentation to start with and I found this gist. The thing I don't like about it is that you have to commit you build directory into the repository. Here's my way around that.

Let's break it down.

First, we add the build directory to a commit. Don't worry, we'll remove it later. Note that we're adding the -f switch assuming that the build directory is ignored.

git add build -f

Next we commit the build directory. Don't worry, we won't push it to the upstream.

git commit -m "deploy"

Next we need to get the SHA-1 checksum for that last commit. In my case, I'm doing this in a Rake task so I get the output of the following command, split it on new lines then use the last method to get the SHA-1 sum. You could just as easily do this in a Bash script or even all on one line.

git subtree split --prefix build master

Now that we have the SHA-1 checksum we can push our build directory into the gh-pages branch. In my case I'm doing this in a Rake task so I drop the checksum into the command using string interpolation.

git push origin #{commit}:gh-pages --force

Remember how I promised that we won't check the build directory into the repository? Reset back 1 commit.

git reset HEAD~1 --hard

Fork and star it on Github Gist.

Blog Index