It is usually not a good idea to push code directly to a live website. For this you can create a bare repository on your server, outside the public directory. This repository will push changes to your Live website repository and will be like the gate keeper for your Website. To set things up, create a repository around your live website directory:
$ cd ~/www
$ git init
$ git add .
$ git commit -m"initial import of pre-existing web files"
With this created, move to a directory not accessible via HTTP. To keep things simple,lets call this bare directory HUB and the website directory LIVE. Initialize a bare repository here:
$ cd; mkdir site_hub.git; cd site_hub.git
$ git --bare init
Then, from inside your live website working directory, add this bare repository as a remote and push live website’s master branch.
$ cd ~/www
$ git remote add hub ~/site_hub.git
$ git remote show hub
* remote hub
URL: /home/rizwan/site_hub.git
$ git push hub master
You need hooks to commit changes to the live repository. Create a post-update
hook inside the HUB repository :
#!/bin/sh
echo
echo "**** Pulling changes into Live [Hub's post-update hook]"
echo
cd $HOME/www || exit
unset GIT_DIR
git pull hub master
exec git-update-server-info
Inside this hook, you can have the code to restart the server after the pull is completed.
Also, create a post-commit
hook on the LIVE repo to send changes done to the live website back to HUB.
#!/bin/sh
echo
echo "**** pushing changes to Hub [Live's post-commit hook]"
echo
git push hub
On your local machine, add the HUB repository as a remote and push changes to it:
git remote add hub <hub-repository-url>
How this works is, you write some code and push it to the bare repository, which using its post-update hook to push changes to the live respository and restart the server.