Skip to content

How to set up Git Hooks

Daisho Komiyama edited this page Jul 28, 2020 · 15 revisions

This is a note about what I've done for setting up Git Hooks. The website is daishodesign.com. domain is already taken and setup. How to create a server block

Create a directory

Create a folder site-daishodesign.git under /var/repo/

Initiate Git

Initiate Git with bare flag git init --bare The commande above should have created the directory hooks/. Navigate into it.

Create post-receive file

Create it under hooks/ sudo vim post-receive

Edit post-receive file

Open it with vim then add this block of code.

#!/bin/sh
git --work-tree=/var/www/daishodesign.com/html --git-dir=/var/repo/site-daishodesign.git checkout -f

Create symbolic link

Create symlink from server block file to a directory sites-enabled sudo ln -s /etc/nginx/sites-available/daishodesign.com /etc/nginx/sites-enabled/

Give permissions

Give execution permissions to post-receive file sudo chmod +x post-receive

Add production remote to local git

Go back to local and navigate to the root directory of your application. Then add a git remote for production git remote add production ssh://root@<YOUR_SERVER_IP_ADDRESS>/var/repo/site-daishodesign.git

That's it.

Now you can push a commit with git push origin master to your Github repo and you can also push all your changes to production with git push production master It's super fast compared to sending all files using a FTP software. Not only it's faster but also safer because you can't miss files that you changed.

2020-0727

If you add a new user, you have to change ownership of your directories and files from root to new user. (Leave $USER as it is. It echoes out current user name) sudo chown -R $USER:$USER /var/www and suod chown -R $USER:$USER /var/repo

Clone this wiki locally