1

I have a machine with a production website, I want to create a git repository in that machine in order to manage the website using git.

So the first thing I did was to create an empty .git repository in the production machine:

mkdir repos
cd repos
mkdir production.git
cd production.git
git init --bare

In this repository under hooks/post-receive I've added the following lines:

#!/bin/sh
GIT_WORK_TREE=/var/www/production_website git checkout -f

Then I've downloaded the folder with my production website into my local machine and started git:

cd production_website
git init

After that I made the first commit:

git add .
git commit -m "first commit"

And finally I've added the remote repository and made my first push:

git remote add origin ssh://myuser@productionmachine.com/repos/production.git
git push origin master

But when I make the push it gives me the following error message:

remote: fatal: This operation must be run in a work tree

Which it's triggered when the post-receive hook it's activated.

Any suggestions of what I might be doing wrong?

UPDATE:

In the first step, when creating an empty .git repository in the production machine, if I use git init instead of git init --bare I get this error message when making a push:

remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: error: is denied, because it will make the index and work tree inconsistent
remote: error: with what you pushed, and will require 'git reset --hard' to match
remote: error: the work tree to HEAD.
remote: error: 
remote: error: You can set 'receive.denyCurrentBranch' configuration variable to
remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into
remote: error: its current branch; however, this is not recommended unless you
remote: error: arranged to update its work tree to match what you pushed in some
remote: error: other way.
remote: error: 
remote: error: To squelch this message and still keep the default behaviour, set
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
rfc1484
  • 9,441
  • 16
  • 72
  • 123

1 Answers1

2

I would rather keep the repo bare, then in the post-receive hook

cd /path/to/non-bare/repo
git --git-dir /path/to/non-bare/repo/.git --work-tree=/path/to/non-bare/repo pull

That is:

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Is there any way to do this using the checkout command? I would rather do it using it because all the other websites we have are mounted like this. Regretfully our system administrator is no longer with us and I have to do this myself. – rfc1484 Jul 30 '12 at 18:31
  • @rfc1484 sure: the question I reference in y answer suggests that you can (as well as http://stackoverflow.com/questions/11592452/why-do-i-need-to-force-git-to-sync-my-remote-repository/11593246#11593246) See http://toroid.org/ams/git-website-howto, which involves a checkout, but still use a bare repo. That is what has been considered as the most practical solution in http://stackoverflow.com/questions/11525004/git-init-bare-not-working-on-working-tree . – VonC Jul 30 '12 at 19:22