Git Tip: Auto update working tree via post-receive hook
Posted on 26/12/08 by Felix Geisendörfer
Oh, this one took me a while to figure out. Lets say you want to setup a git repository on your server that whenever you push into it updates its working tree. Why? Well, because this can be a very convenient workflow for a small project where you want to be able to quickly push changes out without any fancy deploy mechanism.
My first attempt was to do this on the server:
chmod +x .git/hooks/post-receive
And then put this into the post-receive hook file itself:
#!/bin/sh cd .. git reset --hard
However, this would always give me an error like this when trying to push:
$ git push Counting objects: 5, done. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 372 bytes, done. Total 3 (delta 0), reused 0 (delta 0) To machine:/repo/path 122a0cc..747f3d8 master -> master fatal: Not a git repository: '.' error: hooks/post-receive exited with error code 128
The solution? It turns out the post-receive hook starts out with the GIT_DIR environment variable set to the repo/.git folder, so no matter what path you 'cd' into it will always try to run any following git commands there. Fixing this is simply a matter of unsetting the GIT_DIR (thanks to Ulrich Petri for the elegant env -i solution):
#!/bin/sh cd .. env -i git reset --hard
Voila, a quick and dirty-git repository that updates its own working tree upon push!
-- Felix Geisendörfer aka the_undefined
Another issue you might want to watch out for: Depending on the user doing the push and the permission / ownership settings on the working tree you may have to become a little more tricky using a sudo -u command and an entry in your sudoers file.
You can skip to the end and add a comment.
Dardo: Thanks for pointing to the script, looks nice! Definitely the way to go if you need to worry about the repo you push in being dirty.
Just wanted to chime in, I'm loving GIT, just had to throw that in there.
It was super easy to set up on even a shared host. So now I do have that particular set up, where "production" is _one of_ my remote repos. I check in and out to the main branch and when I'm ready to "deploy" just push to repo "live". Bada-bing bada-boom...no more FTP :)
Here's a very complete blog post on setting up a web-focused Git workflow:
http://joemaller.com/2008/11/25/a-web-focused-git-workflow/
Benefits (FTA):
1. Pushing remote changes automatically updates the live site
2. Server-based site edits won’t break history
3. Simple, no special commit rules or requirements
4. Works with existing sites, no need to redeploy or move files
Been a big fan of yours + cakePHP framework for a while. Thanks and. Mucho props
Thanks for the hint, most handy!
The script I'm using as a post-receive hook also gets invoked manually. I used the following idiom to handle this:
if [ -n $GIT_DIR ]; then
unset GIT_DIR
cd ..
fi
Wow, I had exactly this problem. Thanks, this post saved me a lot of digging!
Even simpler:
cd ..
/usr/bin/env -i /usr/bin/git .....
Felix, thank you so very much for the mention of the GIT_DIR var with regards to hooks. I've been building and tweaking hooks for a couple of weeks now and just ran into the problem with the GIT_DIR var today. I should have realized the problem right away, but it took hours of aggravation and finding your post to finally solve the problem.
Thanks again.
Chris Jean: Glad I was able to help : ). You may want to try Ulrich Petri's method as well. I have not yet tested it, but it is probably "cleaner"
Hi, cool post. I have been wondering about this topic,so thanks for writing.
I am find your source via http://google.com
I found this site using http://google.com>google.com And i want to thank you for your work. You have done really very good site. Great work, great site! Thank you!
Sorry for offtopic
This post is too old. We do not allow comments here anymore in order to fight spam. If you have real feedback or questions for the post, please contact us.
Hi Felix, here is a link to another git hook that takes care of some situations that may occur: http://git.or.cz/gitwiki/GitFaq#head-b96f48bc9c925074be9f95c0fce69bcece5f6e73
Keep posting git tricks from time to time, I like them ;)
Regards,
- Dardo Sordi.