4

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.

2

3 Answers 3

4

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.)

3
  • i need a time to understand this script, some of git commands i see in first
    – Ifozest
    Sep 30, 2014 at 12:08
  • @Viachaslau Nor did I know them before I started writing this :) These commands are part of the "plumbing", git-scm.com/book/en/Git-Internals-Plumbing-and-Porcelain
    – Fred Foo
    Sep 30, 2014 at 12:11
  • This is fascinating! Not so much for human use, but probably ideal in a scripted environment.
    – krlmlr
    Apr 13, 2015 at 22:50
3

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.

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.