Git pre-commit hook

Dilanka Muthukumarana
3 min readOct 30, 2022

Do you need your own safety for the commit message? here you go. you can validate the commit message with a pre-commit hook.

As a best practice, we wanted to have a Jira ticket id in the commit message to git commit. But as a developer, we are in a little hurry to commit our code as soon as the code is implemented.

So we can save ourselves by having our own validation in place in our own way. The below example shows you how you have a pre-commit hook to validate the git commit.

Validate the git commit to check that the commit message contains the Jira ticket id.

Step 1

  1. Go to your code repository and find the git hooks location.
.git/hooks

2. You can find below the files with other git-provided files. In this case, we need only the below file.

.git/hooks/commit-msg.sample or
.git/hooks/commit-msg

3. if the .git/hooks/commit-msg is not there. Then you can keep the existing example or rename .git/hooks/commit-msg.sample to .git/hooks/commit-msg.

Step 2

Add the below content to the file.

file name now:

.git/hooks/commit-msg

file content:

#!/bin/shcheck=$(head -1 $1 | grep -e '[A-Z]\+-[0-9]\+')
if [ "" = "$check" ]; then
echo "Commit message should have jira ticket id." 1>&2
echo "Required format:" 1>&2
echo " [ticketid] <description>" 1>&2
exit 1
fi

Step 3

Now you need to make sure this file is executable.

you can do it with the below command.

chmod +x .git/hooks/commit-msg

Global config needed for your all local git repositories

Step 1: Enable Git templates globally.

git config --global init.templatedir '~/.git-templates'

Step 2: Create a templates directory

mkdir -p ~/.git-templates/hooks

Step 3. Copy your commit-msg hook to the created directory

Step 5: Make the file executable

chmod +x ~/.git-templates/hooks/commit-msg

Step 4: re-init your git repository

git init

Let’s test it now

Let's assume your Jira Id is ABC-123

Test 1: commit message “ABC123 Test commit”

Test 2: commit message “ABC=123 Test commit”

Test 3: commit message “abc=123 Test commit”

Test 4: commit message “abc-123 Test commit”

But you will get a commit failed message like below.

Test 4: commit message “ABC-123 Test commit”

You will get committed success.

Want to skip your own rules?

Just use the below flag with your git commit command.

--no-verify

It is simple as that...

Pros

  • Since this is a built-in feature in Git, it can be used with every Git vendor (GitHub/GitLab/BitBucket).
  • Can be configured and triggered locally before every commit therefore the impact is local also the results also immediate. (That is the scope of this article)

Cons:

  • The Rules are defined by the developers themselves, they can also be easily ignored by the developers.
  • Configured locally, therefore every developer needs to configure it on their own local environment.

Note: We can add this validation to the git server side as well. Also, most tools such as bitbucket, and GitLab, provide rule base control mechanisms to do it such as defining push rules in GitLab repository settings itself which applies all members to comply with the commit message format.

Happy coding..!

--

--

Dilanka Muthukumarana

TOGAF® Enterprise Architecture Practitioner | Experienced Software Professional | Freelance Architect/Consultant https://buy.stripe.com/8wMbMpdvO31ycsUbII