There is a way to do this right, which will solve your problem too:
Separate the working site to a bare git repo and to a working dir, let's call them
/repos/demo.git
and/sites/demo
, respectivelyPush to your demo repo as usual, that part is fine
In the post-receive hook of the demo repo, update the working dir with:
git --work-tree /sites/demo reset --hard branchname
where
branchname
is the name of the branch that was received (that you pushed)
This way you will not violate good practices: your demo repo is bare, so it's perfectly normal to push to it. There is no git fetch
step, it's unnecessary, since the demo repo has just received the commits, there's nothing to fetch.
The --work-tree
is of course the main trick here. It makes git commands manipulate a directory at a different place, and you can use this with bare repositories too. Inside the hook, I think the directory of the bare git repo is correctly set. If not, you can force it with the --git-dir
option:
git --git-dir /repos/demo.git --work-tree /sites/demo reset --hard branchname