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-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
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 mymain
branch, 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-worktree
mounts your sub-directory,dist
, in this example, to a separate branch,gh-pages
.Here's how:
dist
is annpm
build script which looks like:All it does is re-creates the
git-worktree
reference, because the.git
file indist
was removed byng build
by default.This reference is needed by
git
to linkdist
to the index.And the workflow goes something like this:
If you run
git status
it will replyOn branch gh-pages
.And
git log
will show one commit"Init"
.But when you
cd ..
and rungit status
again, the response will beOn branch main
.And
git log
will show all of your original commits tomain
.So what's happened here is quite interesting. The folder
dist
now has a separate branch, with it's own, unrelated history tomain
,and all you have to do to switch is
cd dist
to access that branch (gh-pages
).This is unlike
git checkout dist
, which would append thedist
directory, with the auto generated build files to your working tree, intermingling yourmain
and deployment histories, which is inconvenient.Here your
src
files 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 thesrc
history 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 dist
do 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
echo
command in thenpm build dist
script.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.