How to Create a Git Hook to Push to Your Server and Github Repo | by Shinichi Okada | Towards Data Science

Git hooks are scripts that Git executes before or after commit, push, and receive. Setting up a Git hook makes your development and deployment easy.
In this article, you will learn how to create a post-receive Git hook that executes when you use the git push command.
I use $ for a local terminal prompt and # for a remote server prompt. Also for simplicity, I use john for username and Github username, and yourdomain.com for our domain name.
You already have a working Git repository on your local machine. We call it newsletter. You have a Linux server for your website (I use Ubuntu 20.04) and you can use ssh to connect from your local computer to your remote server.
I assume you secured your server and set firewalls on your server. You can connect to your server using SSH.
Let’s connect to your server:
$ ssh john@yourdomain.com// or use your IP address$ ssh john@167.99.123.45
Once you are in the remote server, create a new directory, newsletter:
# pwd/home/john# mkdir newsletter# cd newsletter
Initialize an empty Git repository in a new directory:
# git init --bare
This will omit the working directory but create the directories and files we need.
Create a new directory under /var/www. We are going to redirect all files to this directory.
# sudo mkdir /var/www/newsletter/public_html
Using git remote set-url allows you to set multiple Git repo URLs.
Find out your current remote config using the git remote -v command:
$ git remote -vorigin git@github.com:john/newsletter.git (fetch)origin git@github.com:john/newsletter.git (push)
Let’s set remote URLs. One for our remote server and one for our Github repo.
$ git remote set-url --add --push origin john@yourdomain.com:/home/john/newsletter$ git remote set-url --add --push origin git@github.com:john/newsletter.git
You need to run git remote set-url twice as above since the first one will overwrite the current one.
Now you should have one fetch and two pushes:
$ git remote -vorigin git@github.com:john/newsletter.git (fetch)origin john@yourdomain.com:/home/john/newslette (push)origin git@github.com:john/newsletter.git (push)
On your remote server, create a file ~/newsletter/hooks/post-receive.
# cd ~/newsletter/hooks# touch post-receive
Add the following:
We need to make the file executable:
# chmod +x post-receive# $ ls -Al ~/newsletter/hookstotal 56-rwxrwxr-x 1 shin shin  478 Apr 24 03:07 applypatch-msg.sample-rwxrwxr-x 1 shin shin  896 Apr 24 03:07 commit-msg.sample-rwxrwxr-x 1 shin shin 3079 Apr 24 03:07 fsmonitor-watchman.sample-rwxrwxr-x 1 shin shin  732 May  3 00:58 post-receive-rwxrwxr-x 1 shin shin  189 Apr 24 03:07 post-update.sample-rwxrwxr-x 1 shin shin  424 Apr 24 03:07 pre-applypatch.sample...
The post-receive file mode should have -rwxrwxr-x.
On your local machine, run git push origin main:
$ git push origin mainEnumerating objects: 5, done.Counting objects: 100% (5/5), done.Delta compression using up to 4 threadsCompressing objects: 100% (3/3), done.Writing objects: 100% (3/3), 303 bytes | 303.00 KiB/s, done.Total 3 (delta 2), reused 0 (delta 0), pack-reused 0remote: Push received! Deploying branch: main...remote: Already on 'main'To okadia.net:/home/john/newsletter   2b35421..aa80729  main -> mainEnumerating objects: 5, done.Counting objects: 100% (5/5), done.Delta compression using up to 4 threadsCompressing objects: 100% (3/3), done.Writing objects: 100% (3/3), 303 bytes | 303.00 KiB/s, done.Total 3 (delta 2), reused 0 (delta 0), pack-reused 0remote: Resolving deltas: 100% (2/2), completed with 2 local objects.To github.com:john/newsletter.git   2b35421..aa80729  main -> main
The Git hook, post-receive, is an excellent tool for developers who frequently work with a server. Git has more tools for client and server-side hooks at your disposal. How about start using it for your project?
If you like my article and would like to receive newsletters, please sign up.
Get full access to every story on Medium by becoming a member.
Githooks
Deploying Code with a Git Hook on a DigitalOcean Droplet