Amir Karimi
Amir Karimi

Reputation: 5521

Update the server files using non-bare Git repository

I have a Linux server with Git installed. I'm developing a web application and I want to create a git repository on server at the website path (/var/www/apps/myapp) and then push my local repository changes to it.

When I created a non-bare repository at server and pushed the changes, I got this error:

Refusing to update checked out branch: refs/heads/master. By default, updating the current branch in a non-bare repository is denied, because it will make the index and work tree inconsistent with what you pushed, and will require 'git reset --hard' to match the work tree to HEAD. etc.

It's recommended to use bare repositories. I could successfully do it but my problem is that I want my server to automatically update the web application folder whenever a change is pushed. As I understand, when I use bare repositories, I have to clone the bare repository in the server again to get the real files. This way I have 3 repositories. Two at the server side (one of them is bare) and one at local.

I just want a read-only repository to which I can push my changes and it updates the web application folder contents at server side.

What is the solution?

Upvotes: 0

Views: 285

Answers (1)

SzG
SzG

Reputation: 12619

In the server repo you must allow pushing to the current branch. Excerpt from git help config:

receive.denyCurrentBranch

If set to true or "refuse", git-receive-pack will deny a ref update to the currently >checked out branch of a non-bare repository. Such a push is potentially dangerous because >it brings the HEAD out of sync with the index and working tree. If set to "warn", print a >warning of such a push to stderr, but allow the push to proceed. If set to false or >"ignore", allow such pushes with no message. Defaults to "refuse".

So in the server git repo:

git config receive.denyCurrentBranch ignore

On the server, in .git/hooks/ rename post-receive.sample to post-receive and add the following lines to it:

export GIT_DIR=`pwd`
cd ..
export GIT_WORK_TREE=`pwd`
git reset --hard HEAD

This will update the website whenever you push.

Upvotes: 1

Related Questions