Sometimes you want to have a subdirectory on the master branch be the root directory of a repository’s gh-pages branch. This is useful for things like sites developed with Yeoman, or if you have a Jekyll site contained in the master branch alongside the rest of your code.
For the sake of this example, let’s pretend the subfolder containing your site is named dist.
Remove the dist directory from the project’s .gitignore file (it’s ignored by default by Yeoman).
Make sure git knows about your subtree (the subfolder with your site).
git add dist && git commit -m "Initial dist subtree commit"Use subtree push to send it to the gh-pages branch on GitHub.
git subtree push --prefix dist origin gh-pagesBoom. 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-pagesWhich lets you type commands like:
git gh-deploy path/to/your/site
I tried this approach, but I think
git-worktree, and deployment from a seperate branch, is a cleaner alternative, since I won't have commits in mymainbranch, intermingled with commits from re-deployment, which I find more succinct, and I won't have to delete the remote branch every time, which is unnecessary.git-worktreemounts your sub-directory,dist, in this example, to a separate branch,gh-pages.Here's how:
distis annpmbuild script which looks like:All it does is re-creates the
git-worktreereference, because the.gitfile indistwas removed byng buildby default.This reference is needed by
gitto linkdistto the index.And the workflow goes something like this:
If you run
git statusit will replyOn branch gh-pages.And
git logwill show one commit"Init".But when you
cd ..and rungit statusagain, the response will beOn branch main.And
git logwill show all of your original commits tomain.So what's happened here is quite interesting. The folder
distnow has a separate branch, with it's own, unrelated history tomain,and all you have to do to switch is
cd distto access that branch (gh-pages).This is unlike
git checkout dist, which would append thedistdirectory, with the auto generated build files to your working tree, intermingling yourmainand deployment histories, which is inconvenient.Here your
srcfiles will be untouched, along with their own history inmain, orcd .., and only the files needed for deployment, will be on this branch, which is really convenient, because it keeps thesrchistory seperate from the deployment history.Now you'd deploy not from a folder, but from a branch, which holds the latest compiled version of your site in GitHub pages.
Of course there's probably an improvement that could be done here as well.
For example make
npm run distdo all of this, but my personal preference is to do these steps manually.Read more about this method here.
This is the how, for the suggestion by @kutsan.
After posting I realized @ChrisBAshton had already documented this approach. The only difference being the
echocommand in thenpm build distscript.But I'd agree, that if you're working on a team, it's probably better to use a tool like gh-pages, to enforce standards in your project.
I hope my explanation is somewhat of a contribution as well, and not just a re-statement of the mentioned methods above.