Blog available for sell
This blog is available for sale. Please 'contact us' if interested.
Advertise with us
git github hooks commit   2   4176
Using git hooks to boost your productivity

Writing a proper git message is a good coding practice. A good commit message should clearly convey what has been changed and why it has been changed. Sometimes developers use an improper message in a hurry to commit and push the changes.

To avoid such situations, we can use git hooks.

Git hooks are shell scripts placed in .git/hooks directory of a repository. These shell scripts are executed in response to specific git events like commit messages, pushing to the repository, etc.


Here is a working hook that enforces the developer to use a commit message of 30 characters.

Please create a new file with the name commit-msg in .git/hooks directory of your repository. Copy and paste the below content into it.

Make this script executable using the command chmod 777 commit-msg.


#!/usr/bin/env bash

# Hook to make sure that no commit message line is less than 30 characters

while read line; do
    # Skip comments
    if [ "${line:0:1}" == "#" ]; then
        continue
    fi
    if [ ${#line} -le 30 ]; then
        echo "Commit messages must be atleast 30 characters."
        echo "The following commit message has ${#line} characters."
        echo "${line}"
        exit 1
    fi
done < "${1}"


You can create and customize hooks. For example, you may start including the branch name in the commit message. This is a good practice in my opinion.


#
# Automatically adds branch name to every commit message.
#
NAME=$(git branch | grep '*' | sed 's/* //')
echo "[$NAME]" $(cat "$1") > "$1" 

Here is the complete example. 

#!/usr/bin/env bash

# Hook to make sure that no commit message line is less than 30 characters

while read line; do
    # Skip comments
    if [ "${line:0:1}" == "#" ]; then
        continue
    fi
    if [ ${#line} -le 30 ]; then
        echo "Commit messages must be atleast 30 characters."
        echo "The following commit message has ${#line} characters."
        echo "${line}"
        exit 1
    fi
done < "${1}"


#
# Automatically adds branch name to every commit message.
#
NAME=$(git branch | grep '*' | sed 's/* //')
echo "[$NAME]" $(cat "$1") > "$1"

exit 0


Download and place this file in your .git/hooks repository with the name commit-msg and make it executable.


Comment if you are using any other git hook to boost your productivity.



git github hooks commit   2   4176
2 comments on 'Using Git Hooks To Boost Your Productivity'
Login to comment

Bernd S. Dec. 2, 2022, 5:26 a.m.
What's the purpose of writing the branch name into the commit message?
Code108 Labs Dec. 2, 2022, 11:10 a.m.
Hi Bernd We follow this approach. We are almost 20 people working on the same project. We name our branches according to the JIRA ID of the story we are working on. By keeping the branch name in the commit message we are linking JIRA ID, branch name, and commit messages. It just makes it easier to figure out what changes were made under what JIRA ID.

Related Articles:
Avoid typing git credentials everytime. Use git credentials manager.
How to store the git credentials in git credentials manager, Using GCM to avoid typing the git credentials every time you run a git command, Different ways to store git username and passwords, increase the productivity by not typing git password everytime...
DigitalOcean Referral Badge

© 2024-2025 Python Circle   Contact   Sponsor   Archive   Sitemap