GIT to deploy website (in 5 steps)

I believe I just found the easiest and cleanest way to publish a website using GIT. This is based on 2 articles; well, actually one, and a translation. But the translation is a bit different, so…

The article is from Abhijit Menon-Sen, and its translation is from Thiago Belem. Both differs from the approach I used couple years ago, which involved having a full repo at server (let’s call it repo1), outside the public docs, and another bare repo at /www (repo2) pulling from repo1 in the post-commit hook.

The advantage of this method is that you could do little changes on the /www using vi and push them to repo1, which will then execute the hook that would republish the file to /www.

But I admit, it’s a bit confusing. So both articles above have a much cleaner solution that I just tested and it’s working just fine. I won’t be able to vi the server, but that’s a compromise I’m comfortable with.

Step 1: Create the local GIT repository

 Step 2: Prepare the server

ssh into the server, and make sure you have your RSA key on ~/.ssh/authorizedkeys . If you don’t… well, add it! :)

 Step 3: Create a post-receive hook on server

 Step 4: Configure local machine

Step 5: Test the thing!

After the first commit, the normal flow would be:

  • [change local file]
  • git commit -am "Always comment your commits"
  • git push www

Bonus Track: Different Environments
If you have a real website, with billions of billions of users, then you’ll need to test your changes before you push them live (unless you’re like that XX guy).

In that case, just add remotes pointing to different repositories, and repeat the same steps above to configure it. Then you’ll just push to different repos by doing:

Also read...