The aim of this guide is to create the basic idea of how to set up a very basic hook. Once you've read through this guide, please read through the Git Hooks Documentation. But first...
In very basic terms, it's a thing that "fires off custom scripts when certain important actions occur". For example, each time we make a commit, a git hook will run some code that does some cool stuff automatically. Because sometimes we want stuff to happen automatically and Git Hooks help us do that!
Oh, sure. Imagine a conversation between you and your supervisor that goes like this:
Supervisor
: So you know how we name our branches [project-name][ticket-number][description]?
You
: Yes?
Supervisor
: I need you to do that, but for each of your commit messages from now on.
You
: Bu.. wha?.. I
Supervisor
: Thanks
Wow, what a pain in the butt. So I need to make sure that each of my commit messages contain specific, hard-to-remember information? Sure, I can get a hook to do this for me!
So for the purpose of this guide, I am going to use a hook to:
- Extract some info from the name of my branch
- Add it at the beginning of my commit message
git hooks
exist in the .git/hooks
directory in your project. By default, this directory is hidden and you may need to tell your IDE to show it.
Open you settings and search for files.exclude
. Remove **/.git
from the list and close settings.
If you use another IDE, please comment and I will happily reference it here.
Now that you can view the .git/hooks
directory you should see the hook sample files.
The sample files are "examples". They don't actually work, but they're a great way to help us get started.
When you create a hook file, remember that the name of a hook file is important and typos in the filename will cause your hooks to not work. For this example, we need a hook file called prepare-commit-msg
.
Let's just duplicate the prepare-commit-msg.sample
file and rename it. Make sure it goes in the .git/hooks/
directory.
You can run this in your terminal: cp .git/hooks/prepare-commit-msg.sample .git/hooks/prepare-commit-msg
If you use Husky then your hook files should go in the .husky/
directory.
Depending on your user rights, you may have to run this command:
chmod +x .git/hooks/prepare-commit-msg
Now that we have our hook file, let's edit it. We can run code .
in the project directory terminal if you use VSC
#!/bin/bash
# Get the current branch name
branch_name=$(git symbolic-ref --short HEAD)
# Extract the project name and ticket number from the branch name
# Update this line to suit the naming convention of your project
# If your branch name is called ProjectName-443-fix-bug then the regex below will extract "ProjectName-443"
ticket_number=$(echo "$branch_name" | grep -o "ProjectName-[0-9]*")
# If [project-name][ticket-number] is found, append it to the beginning of the commit message.
# E.g. "ProjectName-443: Refactored some code"
# Nothing will be appended if your branch name does not follow the specified ticket naming convention
if [ $ticket_number ]; then
sed -i.back "1s/^/$ticket_number: /" .git/COMMIT_EDITMSG
fi
exit 0
Your hook should run whether you use git via terminal or a GUI. Whenever you commit, simply type your regular message. The hook will automatically add the extra information at the beginning of your commit message for you.
Hey. Sorry for not seeing this. The only difference would be where you enable the hidden files. The rest should be the same.