First of all, you need to have a gh-pages. If you don't have, create:
git branch gh-pagesThis makes a branch based on the master HEAD.
It would be okay but the files and the git history of master branch are not meaningful on gh-pages branch.
Using an --orphan branch, you can initialize gh-pages in a clean way.
git checkout --orphan gh-pages
git reset --hard
git commit --allow-empty -m "Init gh-pages branch"
git checkout masterThen, mount the branch as a subdirectory using git worktree:
git worktree add dist gh-pagesIf you didn't ignore the dist folder, ignore it so that you don't add generated files accidentally in your master branch commits.
echo "dist/" >> .gitignoreEvery time you build the static bundle, generated files are in dist directory.
Since dist folder is now gh-pages branch, you can deploy it directly by just creating a commit and pushing it.
cd dist
git add --all
git commit -m "Deploy on gh-pages updated"
git push origin gh-pagesThis way nothing was added to the master branch history, keeping it clean.
A few years late, but in case anyone stumbles upon this; is better to use
cd distand then usegit add -f ., when you already have your .gitignore ignoring the dist/ folderEverything else is the same.
Also the
git branch gh-pagesseems to not be needed, you can just dogit checkout --orphan gh-pagesand the branch will be created, at least I got an error because the branch was already created when using git branch gh-pages and then checkout --orphan.And when doing
git worktree add dist gh-pages, the dist folder must not exist before executing the command, is okay to delete it, because that command will just create another one empty, and then delete the .gitkeep file that was created after the command.