Last Updated: September 09, 2019
·
3.437K
· clarkeash

Git push to update your website.

This tip will explain how to setup git & push to git to update your website.

Note:

You should have your ssh keys already set up on your server.

Installing git on your server

run sudo apt-get update
then sudo apt-get install git-core
Thats all thats needed to install git.

Add a user

To add a user run sudo adduser git(git is the username, this can be anything)
You now need to add your public ssh key to that users authorised keys:
sudo mkdir /home/git/.ssh

sudo cp ~/.ssh/authorized_keys /home/git/.ssh/

sudo chown -R git:git /home/git/.ssh

sudo chmod 700 !$

sudo chmod 600 /home/git/.ssh/*

Add a repository

Firstly login as the git user with login git

You should then create your repository and initialise it:

This assumes you are in the home directory.

mkdir myrepo.git

cd myrepo.git

git --bare init

Make git update your website

Assuming your website is stored in /var/www/example.com do the following:

You should still be in the myrepo.git directory, edit you need to edit you post hooks file like so:

vim hooks/post-receive
and type the following:

#!/bin/sh
GIT_WORK_TREE=/var/www/example.com git checkout -f

Now set the permissions:
chmod +x hooks/post-receive

Setting up the local repository

Assuming you have git installed on your machine, create a new directory and initialise it with git init

Add a file with some content like so : echo 'hello world' > index.html

add and commit the file:

git add index.html

git commit -q -m "first commit."

You then need to setup the remote like so:

For the following substitue your server.com for ip or domain.

git remote add web ssh://git@server.com/home/git/myrepo.git

You now need to push to the server with git push web +master:refs/heads/master and this should be it, your website will now show your index.html file.

On future updates just run git push web

Thanks