How to properly commit in repository with Github Pages branch

i've got a question

Situation based on gh-pages

My project uses preprocessors and another difficult staff, so, my project stucture looks as follows:

master branch

./-|
   /src
   /node_modules
   /public-|
       /js
       /css
       /etc
       index.html           
   /etc
   package.json 
   etc

all code sources in src folder, compiled - into public folder.
public folder also in .gitignore
I would like to commit all files in my public folder into gh-pages branch. Is it possible? If yes, how to do this? This is a structure of my gh-pages branch

gh-pages branch

 ./-|
    /js
    /css
    /assets
    index.html    

I think it will be great to run grunt/gulp tasks and commit result into another branch.


I have a pair of scripts that do exactly this. In my situation, doc sources are in . (relative to where the scripts are run, of course), generated documentation is in _build/html, and I issue the commands

touch _build/html/.nojekyll    # disable GitHub's Jekyll page generator
./commit-to-gh-pages.sh _build/html

where commit-to-gh-pages.sh is

#! /bin/sh

# Commit the generated HTML pages to the branch gh-pages.
# Will not push them to GitHub.

set -e -v

treehash=$(./hash-tree.py "${1:-_build/html}")
parent=$(git rev-parse gh-pages)

msg="Regenerated docs for $(git rev-parse HEAD)"
commithash=$(echo "$msg" | git commit-tree $treehash -p $parent)
echo "Updating gh-pages to $commithash"
git update-ref refs/heads/gh-pages "$commithash"

This writes the directory _build/html to the Git index, makes a commit object that has exactly this directory tree as its contents, and writes the commit object to the gh-pages branch with the previous state of that branch as the parent.

The tricky part is the hash-tree.py script, which extends the git hash-object command to directory trees. I won't copy-paste it, but you can get it here. (If anyone knows a more elegant way to do this, please tell me.)


I have had excellent experience with cloning the repo into a subdirectory of itself and checking out a different branch: For your case something like

# add public to .gitignore
git clone . public
cd public
git checkout --orphan gh-pages
git reset --hard
git commit --allow-empty "initial"

gets you started. See http://krlmlr.github.io/git-subbranch for a writeup.


You could try using git subtree if you have git version > 1.7.11. I can barely manage to understand the details myself yet, so I won't try to explain it, but there is a tutorial at Hugo website builder. The relevant part starts at the heading configure git workflow.