Prepare the working directory and directory for the git repo. Example is using root login with ssh keybased auth, configured in ssh server.
# mkdir -p /root/repo/projectname.git
# mkdir -p /var/www/projectname/public_html
Initialize bare git repo in the git repo dir and setup post-receive hook
# cd /root/repo/projectname.git
# git init --bare
# vim hooks/post-receive
# chmod +x hooks/post-receive
post-receive is an executable file containing bash script as follows;
Content of hooks/post-receive
#!/bin/sh
git --work-tree=/var/www/projectname/public_html --git-dir=/root/repo/projectname.git/ checkout -f
chown -R www-data.www-data /var/www/projectname/public_html
Notice the additional chown command so the ownership of the pushed data changed to www-data user, not recommended for a large project, need to find another method.
In the local machine ssh is configured to login to remote server via ~/.ssh/config file as follows:
Content of ~/.ssh/congfig file
Host project
Hostname server.ip.address
Port 22
User root
IdentityFile ~/.ssh/keyfilepath
So we can login to remote server using ssh only with an alias such as ssh project
;
Create the directory for the local project development, init local git repo and add remote git repo
$ mkdir ~/Project/www-deploy/ && ~/Project/www-deploy
$ git init .
$ git remote add remote ssh://project/root/repo/project.git
$ touch .gitignore
$ git add .
$ git commit -m "Initial commit"
$ git push remote master