Catmal Catmal - 15 days ago 5
Git Question

Digital Ocean git bare repo

I am trying to setup my own production environment on digital ocean with ubuntu, rails and git.

I have followed this and this tutorial among others.

They all instruct to create the repository as bare, i.e.:

mkdir site.git && cd site.git
git init --bare


Problem is that when I push from my local computer, I got the error:


This operation must be run in a work tree


I looked into it and it seems that the cause is that the repo is bare.

In my post receive file I have:

#!/bin/sh
git --work-tree=/home/rails/myapp --git-dir=/var/repo/site.git checkout -f


So I am confused. I suppose that I could initialize the repo as non bare instead. But why every tutorial suggests to initialize it as bare if then it causes that error?

Answer

If you are making a git push, you must do so from a normal git repo, meaning a local repo on your workstation (pushing indeed to a bare repo)

cd /path/to/my/local/repo

git init .
git add -A
git commit -m "my first commit"
git remote add origin /usr/bare/repo
git push -u origin master

In your case, you cannot push directly from where the post-receive hook does a checkout, unless you mention in your git push command where the git repo is (bare or not)

cd /home/rails/myapp
git --work-tree=/home/rails/myapp --git-dir=/var/repo/site.git push -u origin master

(and this assume /var/repo/site.git does include a remote named origin)