How to manage a website with GIT

It is not known by many people, but we can use GIT to manage our website or web project in seconds, without the need to upload one by one all the files through a FTP clients.

Maybe, your are asking: Why I must start using it?

  • You can update your website to the last version in seconds.
  • You can go back to the lastest stable version in seconds in case you have any problem on production environment.
  • Prevent human errors when you are uploading many files with a FTP client.
  • Your website update is faster.
  • Prevent reupload unnecesary files.

If you want to try it, continue reading:

First, you need a Linux server with shell access and root permissions.

Obviously, you must have GIT installed in your server.

sudo apt-get install git

Once you have installed GIT, create a folder where you are going to save our GIT repositories.

mkdir /var/www/gitrep/project.git

Access to the just created folder

cd /var/www/gitrep/project.git/

Init a GIT bare repository

git init --bare

A bare repository, is a central repository. This means that is a repository where we only are going to do push from our project.

Now, we create a HOOK which is used for the auto-updating the folder where is located our web project in our web server when we do a push.

sudo vi /var/www/gitrep/project.git/hooks/post-update

Enter the next content:

GIT_WORK_TREE=/var/www/project/ git checkout -f

Replace ‘/var/www/project/’ for the path where is located your project folder in your web server. After this, save the file changes.

Then, you must set necessary permissions to the HOOK you created before.

chmod +x /var/www/gitrep/project.git/hooks/post-update

With the GIT repository configured in your web server, it is time to configure a few things in our local repository (working copy).

First, add remote repository to your local repository:

git remote add remote_repository_name ssh://user@host/var/www/git_rep/project.git

Set write permissions to your GIT repository

sudo chmod -R 0777 /var/www/git_rep/project.git

Push local repository content to remote repository

git push remote_repository_name +master:refs/heads/master

In case that your web server hasn’t updated your project with your local repository content, you must to do a local change in some of your project files, then create a new commit, and push to the remote repository again.

git push remote_repository_name master

And this is all you must to know. Now, when you do a push into your remote (production) server your project will be updated automatically.

If you have any question, tell us as a post comment.