3

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.

Ben31
  • 31
  • 3
  • possible duplicate of [Why am I getting the message, "fatal: This operation must be run in a work tree?"](http://stackoverflow.com/questions/1456923/why-am-i-getting-the-message-fatal-this-operation-must-be-run-in-a-work-tree) – AD7six May 30 '14 at 13:41
  • The 'possible duplicate' you are pointing to is from someone attempting to run `git add` with a bare repo. Answers explain why this can't work. I understand this, but I do not think it answers my question. – Ben31 May 30 '14 at 14:46
  • You're right - I picked the wrong duplicate, though actually the duplicate is spot on: `I have created a bare git repo` - you are issuing `git checkout` on a _bare_ repository. With what you're attempting to do (update a working copy on push), public_html would ordinarily be a checkout of its own (_in addition_ to the bare repo) and you'd update public_html, if you wanted to, when you push to the bare repo [like so](http://serverfault.com/a/107703/108287). – AD7six May 30 '14 at 19:22

3 Answers3

1

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
Felix Frank
  • 8,125
  • 1
  • 23
  • 30
  • Thanks for taking the time to respond, but paths do not seem to be the issue. I am still getting the same error... – Ben31 May 30 '14 at 14:38
0

I had a similar problem. I had attempted to change the directory in the hooks/post-receive and it was uploading but then I would get the error 'remote: fatal: This operation must be run in a work tree'. I reviewed the instances of git which were working and noticed the only difference was a line break after the GIT_WORK_TREE before the checkout command. When I removed that break so that git checkout -f was on the same line, it worked!

Changed from this:

#!/bin/sh
GIT_WORK_TREE=/var/www/vhosts/dylanglockler.com/dev.brightsideresumes.com 
git checkout -f

To this:

#!/bin/sh
GIT_WORK_TREE=/var/www/vhosts/dylanglockler.com/dev.brightsideresumes.com git checkout -f
Dylan Glockler
  • 1,115
  • 1
  • 20
  • 40
0

My solution:

git clone --mirror git@github.com:iamshaunjp/tailwind-tutorial.git tailwind-tutorial/.git/;
cd tailwind-tutorial;
git init;
git branch -a;
git checkout lesson-19;
ignacio
  • 1,181
  • 2
  • 15
  • 28
ipopop
  • 1
  • 1