4

I have a website that I'd like to update whenever I push to a remote repository. I am coming from a svn background and still trying to figure out git.

Right now, I have done the following:

  • Setup a Git repository on my local machine containing the website.
  • Cloned the (bare) repository to my web server.

Now I'm a bit stuck. I can push the changes to my bare repository on the server but I have no idea of how to checkout a working copy of the repository in my www directory and automatically update it whenever I push my local repository to the server. I'll probably need a hook script right?

Related question, Deploy PHP using Git, partially answers my question, but I'd like to know what the script is actually doing.

Community
  • 1
  • 1
Olivier Lalonde
  • 19,423
  • 28
  • 76
  • 91
  • Why don't you keep a normal repository instead of a bare one on your server? And push/serve from that directly? – Dogbert Jan 06 '11 at 11:41
  • @Adam: I tried that initially but then when I tried to push (git push web-server), I got this error: remote: error: By default, updating the current branch in a non-bare repository is denied, because it will make the index and work tree inconsistent – Olivier Lalonde Jan 06 '11 at 11:44

2 Answers2

6

This worked for me, it might work for you: A web-focused Git workflow

leonbloy
  • 73,180
  • 20
  • 142
  • 190
  • +1 It's a neat solution but I just have to mention that it requires you to store the information twice on your host, since it involves two repositories. I know this is the trade off, just saying :) – rtn May 28 '11 at 10:40
5

I found a very good (and elegant) solution for me on the Caius Theory Website

It basically starts with a bare repository and changes the worktree to the web-server folder. After that it uses a post-receive hook to update the work-tree after every push it receives. An elegant and easy to follow procedure!

Additional to the setup in that article I added a soft-linked ".git" directory from the website location back to the git repository location:

ln -s /home/caius/git/somesite.git/ /home/caius/vhosts/somesite.com/htdocs/.git

This way I can checkout another branch on the web-server by logging into it and using "git checkout " in the website folder!

I also used a slightly modification on the Python-Script "ygit-push-all.py" from here to update my multiple machines which all run the same framework code with using different config files. You could even setup a branch per server (like Demo/Development).

In addition I added the following aliases to my global git config file:

[alias]
    push-all = !ygit-push-all.py
    check-all = !sh -c 'git branch -r -v | grep master | awk \"{ print \\$1, \\$2 }\"'

git push-all will update all my remote locations

git check-all will show me the rev on which every remote master branch is

OderWat
  • 5,379
  • 3
  • 29
  • 31
  • The beauty of this solution is that you can have an existing public directory (say with wordpress core files) and the bare repo can only have a subset (say /wp-content/themes/mytheme) `git checkout -f` will manage/merge the subset of tracked files (very similar a solution used by wpengine). – farinspace May 17 '19 at 09:28