You're getting this because /master
and /develop
are non-existent directories:
$ git --work-tree=/nonexistent checkout master
fatal: This operation must be run in a work tree
You may also want to see my answer to a question about another problem that crops up with this approach, which also addresses a small bug you've copied from a popular-but-wrong post-receive technique (the use of cut
to parse the updated ref).
[Your phrasing also makes me wonder if you are thinking of deploying those two branches into a sub-directory within the (presumably --bare
) repository that is receiving the pushes. This is probably not a great idea.]
Another (different) method for deploying is to have a "real" git tree in the deployment location. Then, instead of git --work-tree=... checkout
you do something like this instead:
deploy()
{
local path=$1 branch=$2
(cd $path && unset GIT_DIR && git fetch && git checkout -f origin/$branch)
}
(untested, feel free to experiment and/or modify). This has other, slightly different tradeoffs with respect to disk space and update windows (which I mention in the other answer).