Skip to content

Instantly share code, notes, and snippets.

@belohlavek
Forked from cobyism/gh-pages-deploy.md
Last active August 21, 2022 19:33
Show Gist options
  • Save belohlavek/61dd16c08cd9c57a168408b9ac4121c2 to your computer and use it in GitHub Desktop.
Save belohlavek/61dd16c08cd9c57a168408b9ac4121c2 to your computer and use it in GitHub Desktop.
Deploy to `gh-pages` from a `dist` folder on the master branch. Useful for use with [yeoman](http://yeoman.io).

Deploying a subfolder to GitHub Pages

Sometimes you want to have a subdirectory on the master branch be the root directory of a repository’s gh-pages branch. Here's how to do it:

For the sake of this example, let’s pretend the subfolder containing your site is named dist.

Step 1

Remove the dist directory from the project’s .gitignore (or skip and force-add afterwards).

Step 2

Make sure git knows about your subtree (the subfolder with your site).

git add dist

or force-add it if you don't want to change your .gitignore

git add dist -f

Remember to commit!

git commit -m "gh-pages first commit!"

Step 3

Use subtree push to send it to the gh-pages branch on GitHub.

git subtree push --prefix dist origin gh-pages

Boom. If your folder isn’t called dist, then you’ll need to change that in each of the commands above.


If you do this on a regular basis, you could also create a script containing the following somewhere in your path:

#!/bin/sh
if [ -z "$1" ]
then
  echo "Which folder do you want to deploy to GitHub Pages?"
  exit 1
fi
git subtree push --prefix $1 origin gh-pages

Which lets you type commands like:

git gh-deploy path/to/your/site
@tyleryoungblood
Copy link

After running git subtree push --prefix build origin gh-pages (my directory was called build, not dist) I was never able to update my Github Pages code. I could merge my changes from master into my gh-pages branch, and push the changes to my gh-pages branch up. But the content of the Github Pages page never changed. Ultimately I had to delete the gh-pages branch and re-push using the command you mentioned. Can you explain why updating the gh-pages branch doesn't result in changes to the visible (frontend) Github Page?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment