GitHub deployment using worktrees failing

I’ve tried following the instructions for GitHub deployment using the gh-pages branch version as documented at Up to the git worktree line, everything appears to work fine and make sense. Here’s what I’m getting after this:

$ git worktree add -B gh-pages public origin/gh-pages
Branch gh-pages set up to track remote branch gh-pages from origin.
Preparing public (identifier public)
fatal: Refusing to point HEAD outside of refs/


$ git --version
git version 2.7.4

While I realize this is more of a git usage issue than a hugo one, I’m hoping someone watching who uses the worktree feature for their deployment can help with this. Fwiw, the clone method mentioned for older git versions works fine for me.


/cc @gunnarmorling

Did you ever find the solution for this particular problem?

I ended up not using worktrees. I started out by following the Git 2.4 and earlier instructions but have since modified them. In case it’s helpful, here’s the script that I’m using:


set -e

DIR=$(dirname "$0")

cd $DIR/..

if [ "$(git status -s)" ]; then
    echo "The working directory is dirty. Please commit any pending changes."
    exit 1;

SHA=$(git rev-parse HEAD)
if [ -n "$GITHUB_API_TOKEN" ]; then
    GIT_USER_ARGS="-c'travis' -c'travis'"

echo "Deleting old publication"
rm -rf public
mkdir public

echo "Creating gh-pages branch in ./public"
git -C public init
git -C public checkout -b gh-pages

echo "Generating site"

echo "Updating gh-pages branch"
git -C public add --all
git -C public $GIT_USER_ARGS commit -m "Publishing to gh-pages ($SHA)"

echo "Pushing gh-pages branch"
if [ -n "$GITHUB_API_TOKEN" ]; then
    # CI deployment
    git -C public push -f https://fhunleth:$ gh-pages:gh-pages 2>&1 | \
        sed s/$GITHUB_API_TOKEN/HIDDEN/g
    # Manual deployment
    git -C public push -f gh-pages

This is fixed in git now, I’m running 2.13.1 and works as expected.


I also met this problem with git version 2.7.4. Upgrade to 2.14.2 solved the problem, besides you can read this post for simpler solution

1 Like