Reputation: 763
I have created a Git repo for a JavaScript library. The repo also contains a demo website that uses the bundled library, which is generated in a dist/
folder. I would now like to deploy that website to GitHub Pages.
However, I have the dist/
folder in my .gitignore
and would prefer it to remain ignored.
Is there a way to automatically generate the gh-pages
branch, which should include dist/
, but keep it off the master
branch?
Upvotes: 11
Views: 4387
Reputation: 166
Thanks to this blog post, I finally found a convenient way to forge a commit from a gitignored subfolder on top of another branch. I personally use it to release a built npm package in a release dedicated branch.
Using such a solution is very flexible, keeps history (no need to force push), but involves more git internals:
#!/usr/bin/env bash
# Environment
BUILD_DIR="dist"
RELEASE_BRANCH="gh-pages"
VERSION="$(jq -r ".version" package.json)"
# Script
git fetch origin $RELEASE_BRANCH
git add -f $BUILD_DIR
tree=$(git write-tree --prefix=$BUILD_DIR)
git reset -- $BUILD_DIR
identifier=$(git describe --dirty --always)
commit=$(git commit-tree -p refs/remotes/origin/$RELEASE_BRANCH -m "Deploy $identifier as v$VERSION" $tree)
git update-ref refs/heads/$RELEASE_BRANCH $commit
git tag -a "v$VERSION" -m "Release tag for $VERSION" $commit
git push --follow-tags origin refs/heads/$RELEASE_BRANCH
Upvotes: 1
Reputation: 763
Alternatively, Travis CI or another continuous integration tool could be used to automatically create the build and deploy the generated files on the gh-pages
branch: GitHub Pages Deployment
Upvotes: 1
Reputation: 31177
Commit this script and call it after having built your dist
:
#!/bin/sh
git commit -am "Save uncommited changes (WIP)"
git branch --delete --force gh-pages
git checkout --orphan gh-pages
git add -f dist
git commit -m "Rebuild GitHub pages"
git filter-branch -f --prune-empty --subdirectory-filter dist && git push -f origin gh-pages && git checkout -
I use it for that ;-)
Upvotes: 14
Reputation: 14753
To summarize your context:
master
dist/
folder that is ignored and untrackeddist/
folder on GitHub Pages using a gh-pages
branch.The simplest way to do this seems to follow the workflow suggested in this gist as well as in the documentation of Yeoman.
But note that this relies on the advanced command git subtree and requires that the dist/
folder is not ignored and tracked on master
as well.
You could then follow the doc regarding the configuration of GH Pages' publishing source.
Does this address your question or do you really want to have the two branches separated and no dist/*
file in the master
branch?
In the latter case, the problem is more complicated (because of the untracked or ignored files in the work tree that can hinder the process...) but you may want to have a look at this project, which is an alternative to the git subtree
-based solution, and is based on a Bash script.
(Note: I have not tested this "git-directory-deploy" solution; it was suggested in the gist I mentioned above.)
Upvotes: 1