Using GIT to manage a website
In regard to this topic : Git website howto here follows a generic git hook :
Copy/paste the code below to your .git/hooks/post-receive file of your remote repository :
#!/bin/bash
GIT_WORK_TREE=`dirname $(dirname $(dirname $(readlink -f $0)))` git checkout -f
Be aware not to forget to set this file executable
and run git config receive.denycurrentbranch ignore
in your remote repository to allow updates to be sent from your local one.
Written by eviweb
Related protips
9 Responses
this works well but I like working with a bare repo outside my public directory and having the working tree inside my public directory (no .git folder in my website) and then checking the contents of the push to the working tree with a similar hook.
Pro-git advises against pushing directly to a non-bare repo.
I will make a protip with this method in a minute, check my profile if your interested to see how to use a bare repo instead.
@jwebcat
thanks for your comment.
For sure, I'm really interested to see how you can manage a working tree with no .git inside.
Regarding the method described by Abhijit (follow the link above), I use the remote repository in a kind of passive mode. Nothing is made directly in this working tree.
All integrations are made on my local desktop and are only pushed to the remote server.
Then I can't see what kind of problem could happened.
Regards
Eric
@eviweb it's not really a big deal until you have many people working on a repo and pushing to the same working tree.
Here is a good article explaining why you shouldn't push to a non bare repo
http://gitready.com/advanced/2009/02/01/push-to-only-bare-repositories.html
I will have some time this afternoon I will make that tip.
@jwebcat thanks for the link.
I've just found it this afternoon ;) and I agree with this article.
But my case is a little bit different, as I don't want to keep changes in the remote repo of my web site.
In fact this repo is not a collaborative working tree.
As I said before, all integrations are made in a local working dir.
These are pushed to a bare remote repository for collaborative work.
Changes are kept here.
But it is very usefull to update your website directly from your computer or directly from the bare repo, once all tests succeed.
You don't need another tool or using a different protocol.
Only git and ssh.
Please, could you keep me informed of your tip ?
I'm very interested.
Eric
-Somehow I missed the article you linked to.
-The article the exact method I use besides taking advantage of ssh aliases.
I'm confused why you are setting the
$ git config receive.denycurrentbranch ignore
-this command tells git to ignore if it is currently in a non-bare repo to over-ride its default setting to disallow this.
-what command are you using to initialize your remote repo?
$ git init --bare
or just
$ git init
if you initialize with --bare
flags then YOU DON'T want to use the
$ git config receive.denycurrentbranch ignore
all you have to at this point if your post-recieve hook is as above, then the very first time you push you must use
$ git push yourremote +master:refs/heads/master
from your local machine.
-the +master:refs/heads/master
tells git about the master branch to track. After that, you can push other branches, tags, etc as normal.
then you can
$ git push yourremote
I hope I didn't get confused somewhere along the way. If I understand correctly, there is no .git folder in your website after you push correct?
here is a newer article explaining the same process as your article.
http://mattbanks.me/wordpress-deployments-with-git/
After seeing the date of the article from Abhijit, I think that setting $ git config receive.denycurrentbranch ignore
is no longer necessary if you are pushing to a checked out branch of a bare
repo for git 1.7+.
It is still required if you are pushing to a non-bare
repo as far as I know.
@jwebcat thanks for all your tips.
The remote repo is non bare so has been initialized using git init
.
The reason is simple : this folder contains the web site tree and yes a .git directory. This is the root folder of the web site in a production context.
Not a working directory at all.
It has only one branch.
Git is only used here to update the web site tree. That's all.
It is important not to do any modifications in that directory, otherwise they would be overwritten on the next update.
And as I explained before you can update this repo from your local working dir or from a shared bare repo after a build succeed for example.
Regarding git config receive.denycurrentbranch ignore
, this config setting is mandatory in this case to prevent reject on pushing.
I didn't find another manner to get around this issue, but it is not really a problem.
The main reason I shared the code of the post-receive hook, is that it is generic enough to allow a simple duplication in another web sites without any other kind of customization.
On your side, how is organized your working architecture ?
Eric
My working architecture is identical to yours without the .git directory in working tree. The process of pushing and updating is exactly identical to your method.
my local working directory is identical to yours.
The ONLY difference is I am pushing to a bare
repo, and you are pushing to a non-bare
repo. For this reason it isn't necessary to set receive.denycurrentbranch ignore
So the ONLY difference is in on my server I have my .git directory in a directory outside of my working tree.
You can change a bare
repo to a non-bare
and vice versa anytime without any adverse affects by changing the config file in your .git directory.
It is possible to have your .git folder outside your working directory with a non-bare
repo like you have, see my newest protip.
@jwebcat
Gosh, I've completly missed the meaning of GIT_WORK_TREE
!
Thanks to have opened my mind ;)
Thanks to you as well bro :D
And thank you for the upvote.
I love that more people are using git