Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git checkout in post-receive hook: "operation must be run in a work tree"

Tags:

git

In order to update my website remotely with git, I have created a bare git repo at the root of my shared hosting account together with the following post-receive hook:

#!/bin/sh
export GIT_WORK_TREE=/public_html
git checkout -f

This initially worked as intended: when pushing my changes to the remote repo, the post-receive hook fired and files were checked out to the /public_html folder.

However, I am now getting a "fatal: This operation must be run in a work tree" error on checkout. I have no clue why this error is appearing now, since I haven't made any change to my setup.

I am stuck. To me this should just work (and it actually did), unless I have completely misunderstood how git works. I have been looking for questions similar to mine but haven't found anything useful so far.

I would be grateful for any idea where I should look.

like image 660
Ben31 Avatar asked Feb 03 '26 04:02

Ben31


1 Answers

Hooks run in the .git dir.

I have used cd .. before git checkout in the hook as a simple workaround.

If the .git repo is fully disjoint from the checkout location, you could also conceivably

cd /path/to/checkout
GIT_DIR=/path/to/repo/.git git checkout -f
like image 79
Felix Frank Avatar answered Feb 04 '26 20:02

Felix Frank