Skip to content

Instantly share code, notes, and snippets.

@ErickPetru
Created September 13, 2019 13:26
Show Gist options
  • Save ErickPetru/b1b3138ab0fc6c82cd19ea3a1a944ba6 to your computer and use it in GitHub Desktop.
Save ErickPetru/b1b3138ab0fc6c82cd19ea3a1a944ba6 to your computer and use it in GitHub Desktop.
Publishing a `dist` folder from `master` branch using **worktree** feature to `gh-pages` branch.

Setup

First of all, you need to have a gh-pages. If you don't have, create:

git branch gh-pages

This 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 master

Then, mount the branch as a subdirectory using git worktree:

git worktree add dist gh-pages

If 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/" >> .gitignore

Deploy

Every 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-pages

This way nothing was added to the master branch history, keeping it clean.

@HeavyMetalGeek
Copy link

I can't seem to get this to work. Everything in dist is ignored and no changes are ever made to the gh-pages branch.

@Ifaen
Copy link

Ifaen commented May 19, 2025

I can't seem to get this to work. Everything in dist is ignored and no changes are ever made to the gh-pages branch.

A few years late, but in case anyone stumbles upon this; is better to use cd dist and then use git add -f ., when you already have your .gitignore ignoring the dist/ folder

Everything else is the same.

Also the git branch gh-pages seems to not be needed, you can just do git checkout --orphan gh-pages and 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.

@ErickPetru
Copy link
Author

Since @Ifaen comment made me remember about this gist...
Nowadays I usually just use the gh-pages package:

gh-pages -d dist

Should I retry my own procedure and update it here? Probably, yes.
But saving some typing and relying on a third-party to cover eventual edge cases just felt easier.

@Ifaen
Copy link

Ifaen commented May 19, 2025

I agree, is better to use the package, specially for those who use github pages

But I thank you btw, because I am using your method with cloudflare pages (I was thinking of just using gh-pages package even for cloudflare, but Im not sure if using the package also automatically enables Github Pages in my repo settings, which for this use case I don't need it).

Maybe there is a package or a third-party tool to achieve the same, but this worked for me and just made a script in package.json to easily deploy it

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