using git hooks to ensure stability
intro⌗
git, to me, is the gift that keeps on giving. It does revision control, synchronization with remote servers, reverting back changes and rewriting a repository’s history from scratch.
Add to that how portable git is, being available on android, linux, windows, macOs, the BSDs and virtually any other operating system. Git is the best.
There is a neat trick I’ve recently discovered in git that allows for stability before you push your commits. Think about this: Git is meant to be used by a person or more, with so many people committing, it is often hard to track if a commit is harmful or not.
That’s where git hooks come into play. Git hooks are shell scripts that are run before or after certain stages in the git process.
For example, say you have a blog similar to this one that uses git, you could create a hook that gets executed after the blog has been pushed to. That hook could perhaps build your website, minimize the assets, compress the text files and restart the web server.
What makes git even more practical is that these hooks can be run in the client side or in the server side. Yes, you heard it right. You don’t have to wait for your slow server to execute your hooks, you could make your performant PC do it and then sync the results with server.
In this article, I’ll go over different uses of git hooks to hopefully shed a light on how useful they can be.
building websites⌗
Dynamic web pages are slowly losing popularity over the static web pages. This is because static web pages are more performant, more secure and require less running dependecies than dynamic web pages.
Unless you are planning to build HTML pages by hand, you should use a static site generator. A static site generator allows you to write your webpages in a human-readable format such as Markdown, and afterwards transforms that format into a format that web pages could use.
With git hooks, you could make it so that whenever you publish a change your website would be built, minimized, compressed and finally synchronized with your server.
running tests to ensure code stability⌗
A unit test is a test for code that checks that a particular function does what it’s supposed to do. Many times, developers implement a new future by modifying an already existing feature, and at times that messes with the whole system in a way that cannot be foreseen by the developer.
A unit test could then be used to mitigate side effects similar to that. By running tests before publishing a change to the remote server, one can ensure that old features aren’t slaughtered away because a developer wanted to implement a new feature.
Recently, I’ve used unit tests with the server implementation of my project ft
. Previously I have always ran tests manually but now, everything’s automatic, which saves me a lot of time.
automatic changelog generation⌗
A changelog is a file that contains changes that have happened to the software. These could include new added features, fixed bugs or patched security vulneribities. With a git hook, one could make a hook take a tagged commit and add it’s comment to the changelog without any further procedures.
The workflow would look something like this:
git tag v1.4 -a -m "Added search capability
Fixed freezing bug
Patched XXX security-vulnerbility"
git push origin --tags
Simple, right?
conclusion⌗
Git hooks can easily make git workflow a lot better. You could use them to build your website automatically, run tests to ensure code stability and compatibility, or even generate changelog automatically. I highly recommend using git hooks.