Automate Blog Deployment with Git Hooks: A Step‑by‑Step Guide
This tutorial explains what Git hooks are, how to install and configure them, and provides a complete example of using a post‑update hook on a bare repository to automatically deploy a blog whenever code is pushed.
Git hooks are scripts that Git runs automatically at specific points such as commits, merges, or pushes. They come in two varieties: client‑side hooks triggered by local actions and server‑side hooks triggered by network operations like receiving a push.
Common uses include adjusting the project environment based on repository state, integrating continuous‑integration pipelines, and automating any part of the development workflow.
Installing Git Hooks
Hook scripts reside in the .git/hooks directory of each repository. When you run git init, Git populates this folder with sample shell (and some Perl) scripts. Any executable file with the correct name will be used, and you can write hooks in Ruby, Python, or any language you prefer.
➜ hooks git:(master) ls
applypatch-msg.sample pre-applypatch.sample pre-rebase.sample
commit-msg.sample pre-commit.sample prepare-commit-msg.sample
post-update.sample pre-push.sample update.sampleRemove the .sample extension to activate a hook.
Scope of Git Hooks
Hooks affect only the local repository; they are not shared via version control because the .git/hooks folder is not part of the project files. Teams often keep hook scripts in the project directory (outside .git) and either symlink them into .git/hooks or copy them after each update.
A simple solution is to store hooks alongside the code, version‑control them, and create a link or copy them into .git/hooks after each pull.
Using a Git Hook for Automatic Blog Deployment
The idea is to listen for a local git push, then on the remote server pull the latest changes and restart the application (e.g., with pm2 reload).
1. Create a bare repository on the server
A bare repository contains only the Git metadata and no working tree. Create it under a directory such as /home/USER/repos/:
git init --bare xxx-bare.git
# or
mkdir xxx-bare.git
cd xxx-bare.git
git init --bare2. Configure the post‑update hook
Navigate to /home/USER/repos/xxx-bare.git/hooks, copy the sample hook, and edit it:
#!/bin/sh
unset GIT_DIR
DIR_ONE=/home/user/www/blog/ # directory served by the web server
cd $DIR_ONE
git init
git remote add origin ~/repos/xxx-bare.git
git clean -df
git pull origin master
pm2 restart xxx # restart the Node.js appImportant: unset GIT_DIR prevents the “remote: fatal: Not a git repository: '.'” error.
Make the hook executable:
chmod +x post-update3. Add the remote to the local repository
After adding the remote, any push from the local repo will trigger the post‑update hook and start the deployment process:
git init
git remote add origin [email protected]:/home/USER/repos/xxx-bare.git # example remote
# or with explicit port
# git remote add origin ssh://[email protected]:26244/home/USER/repos/xxx-bare.git
git push origin masterConclusion
By understanding where hooks live, how they are activated, and how to script a post‑update hook, you can set up a reliable push‑to‑deploy workflow that automatically updates and restarts your blog whenever you push new commits.
Aotu Lab
Aotu Lab, founded in October 2015, is a front-end engineering team serving multi-platform products. The articles in this public account are intended to share and discuss technology, reflecting only the personal views of Aotu Lab members and not the official stance of JD.com Technology.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
