tags:

views:

172

answers:

3

I'm using Git wrong. I want to use it right.

Here's what I've got:

Over here, on my development machine is a Git repository, that I commit to and test on.

Over there, is my web-server - the one place this code will be deployed. The web-server has another bare git repository that I can push to, over SSH, when I am ready to deploy.

What I want to happen is to have a view into the Git repository which always has the latest versions of the source files (on some branch or with some tag).

What I COULD do is to create another (non-bare) git repository on the web-server, and do a manual pull after each push, but I was hoping to avoid having to log in to the web-server each time I do a git push.

Is there a way to do a remotely "push to the web-server and refresh its checked-out files, if I promise I didn't edit any files on the web-server"?

Or am I just doing it so wrong you want to slap me? :-)

+6  A: 

The hooks might be what you're looking for.

You create a post-receive hook. And it'll be executed on the server after every push you're doing. Then you do a pull on the second bare repository and you'll always be up to date :)

Damien MATHIEU
+5  A: 

I usually create a non-bare repository with a file .git/hooks/post-receive containing

#!/bin/sh
cd ..
env -i git reset --hard

This file must be executable and a branch must be checked out.

Then every time you push to that repository, the work tree will be reset to the latest version of the current branch. Any local change will be overwritten (but untracked files won't be removed).

Git now displays a warning when you push to a checked out branch (and will reject it in future releases). To avoid this you can set this config on the remote repository :

git config receive.denyCurrentBranch ignore

You can work on this repository, but you must commit your changes before any push is done to it.

I found the tip there : http://debuggable.com/posts/git-tip-auto-update-working-tree-via-post-receive-hook:49551efe-6414-4e86-aec6-544f4834cda3

Michaël Witrant
+1  A: 

I use for such things a bare repository (inside a user account) and a shared repository which is the root of the web.
The bare repository has a post-update hook to pull the web root, so after pushing to the repository the web root will be pulled.

To have read/write access from the webserver, my git-user is also in the group www-data and the web root directory was chmod ug+rwx,g+s so new created files will get the ownership of the group. The umask makes sure that the group has read/write access because Git uses the default umask which is normally 0022 (the documentation says something about of using a own umask in the config but this has never worked for me)

#!/bin/bash
# post-update hook

WEB_DIR="/var/www/myweb"
pushd $WEB_DIR > /dev/null
export GIT_DIR="$WEB_DIR/.git"
umask 0007
git pull
popd > /dev/null
devarni