You can use the post-receive Git hook for this
http://git-scm.com/book/en/Customizing-Git-Git-Hooks
The hook will look like this
cd [LIVE SITE DIR] && git pull origin master
I will describe how I have my deployment set up. I use the same server as live server and as a place where I have my repositories stored.
- I store my Git repositories in
/var/git directory
. Applications are stored in /var/www
Create Git repository using git init --bare
cd /var/git
mkdir project.git
cd project.git
git init --bare
Clone repository into the /var/www/project
folder
- Clone repository to my local computer
Create post-receive hook
cd /var/git/project.git/hooks
touch post-receive
chmod +x post-receive
The post-receive hook looks like this:
#!/bin/sh
unset GIT_DIR
cd /var/www/project && git pull origin master
If you do not unset GIT_DIR, you will get the following error when doing git push:
remote: fatal: Not a git repository: '.'
It's because Git uses the variable GIT_DIR instead of PWD. See more information about this here:
getting "fatal: not a git repository: '.'" when using post-update hook to execute 'git pull' on another repo
When you do changes on your local computer, commit them and push them, these changes will be automatically pulled into the /var/www/project directory.
If you have one server as live server and your repositories are stored on another server. You will have to change your post-receive hook so you would ssh to the live server and execute the git pull origin master there.