Skip to content

Instantly share code, notes, and snippets.

@pierrejoubert73
Last active August 21, 2024 17:43
Show Gist options
  • Save pierrejoubert73/7c7cc85a2c8fe278bfa8641654fd3089 to your computer and use it in GitHub Desktop.
Save pierrejoubert73/7c7cc85a2c8fe278bfa8641654fd3089 to your computer and use it in GitHub Desktop.
How to add a git commit hook

Git Hooks - A very basic guide

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...

What is a Git Hook?

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!

Give me an example!

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:

  1. Extract some info from the name of my branch
  2. Add it at the beginning of my commit message

Step 1 - Enable .git/ directory visibility in your IDE

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.

Visual Studio Code

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.

Step 2 - Create your hook file

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

Husky

If you use Husky then your hook files should go in the .husky/ directory.

Step 3 - Make sure your hook file is executable

Depending on your user rights, you may have to run this command:

chmod +x .git/hooks/prepare-commit-msg

Step 4 - Edit your hook file

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

Step 5 - Test your hook

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.

@RegR2
Copy link

RegR2 commented Jan 23, 2024

hi, is it possible to include examples using intellij ?

@pierrejoubert73
Copy link
Author

Hey. Sorry for not seeing this. The only difference would be where you enable the hidden files. The rest should be the same.

@Seif-Mamdouh
Copy link

Hello! How can I add the ./git/hooks folder my github repo?

@4DRIAN0RTIZ
Copy link

Hello! How can I add the ./git/hooks folder my github repo?

Just must run git init.

That command creates a .git folder and their subfolders including hooks folder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment