What’s that, you say? You’ve become tired of regular old boring paper checklists? Well, my friend, today is your lucky day! You, yes, you, can become the proud owner of a brand-spanking-new automatic interactive pre-commit hook checklist! You’re gonna love this! Your life will be so much easier! Just wait until your friends see you.
What’s a pre-commit hook
Did you know that nearly 1 out of 5 coders are too embarrassed to ask this question? Don’t worry, it’s perfectly normal. In the next 60 seconds we’ll tell you all you need to know to pre-commit with confidence.
A Git hook is a feature of Git that triggers custom scripts at useful moments. They can be used for all kinds of reasons to help you automate your work, and best of all, you already have them! In every repository that you initialize with git init
, you’ll have a set of example scripts living in .git/hooks
. They all end with .sample
and activating them is as easy as renaming the file to remove the .sample
part.
Git hooks are not copied when a repository is cloned, so you can make them as personal as you like.
The useful moment in particular that we’ll talk about today is the pre-commit. This hook is run after you do git commit
, and before you write a commit message. Exiting this hook with a non-zero status will abort the commit, which makes it extremely useful for last-minute quality checks. Or, a bit of fun. Why not both!
How do I get a pre-commit checklist
I only want the best for my family and my commits, and that’s why I choose an interactive pre-commit checklist. Not only is it fun to use, it helps to keep my projects safe from unexpected off-spec mistakes!
It’s so easy! I just write a bash script that can read user input, and plop it into .git/hooks
as a file named pre-commit
. Then I do chmod +x .git/hooks/pre-commit
to make it executable, and I’m done!
Oh look, here comes an example bash script now!
#!/bin/sh
echo "Would you like to play a game?"
# Read user input, assign stdin to keyboard
exec < /dev/tty
while read -p "Have you double checked that only relevant files were added? (Y/n) " yn; do
case $yn in
[Yy] ) break;;
[Nn] ) echo "Please ensure the right files were added!"; exit 1;;
* ) echo "Please answer y (yes) or n (no):" && continue;
esac
done
while read -p "Has the documentation been updated? (Y/n) " yn; do
case $yn in
[Yy] ) break;;
[Nn] ) echo "Please add or update the docs!"; exit 1;;
* ) echo "Please answer y (yes) or n (no):" && continue;
esac
done
while read -p "Do you know which issue or PR numbers to reference? (Y/n) " yn; do
case $yn in
[Yy] ) break;;
[Nn] ) echo "Better go check those tracking numbers!"; exit 1;;
* ) echo "Please answer y (yes) or n (no):" && continue;
esac
done
exec <&-
Take my money
Don’t delay! Take advantage right now of this generous one-time offer! An interactive pre-commit hook checklist can be yours, today, for the low, low price of… free? Wait, who wrote this script?
git coding terminal