21

I have a git commit hook script, that checks the commit message, and if the message does not contain the word "updated", the script should reject the commit.

#!/bin/bash
read -p "Enter a commit message: " message

if [[ ${message} != *"updated"* ]];then
  echo "Your commit message must contain the word 'updated'"
  else
  git commit -m "$message"
  fi

How to make this hook automatically execute if I try to push some files in my local repo using the command

git commit -m "updated:something"

My idea is to make it not like "run this script to do commit", but rather when you open the console and try to make a commit and entering the commit message, the script will check your commit message automatically and pass it or reject it.

2

2 Answers 2

30

Taking commit-msg for example.

#!/bin/bash

MSG="$1"

if ! grep -qE "updated" "$MSG";then
    cat "$MSG"
    echo "Your commit message must contain the word 'updated'"
    exit 1
fi

chmod 755 commit-msg and copy it as .git/hooks/commit-msg.

6
  • can you please explain last line ? chmod 755 commit-msg and copy it as .git/hooks/commit-msg. you mean chmod +x ./commit-msg ? what 755 is do ? Jul 31, 2017 at 14:19
  • now i copy your code ant add it on file and then i try commit i have an error : error: cannot run .git/hooks/pre-commit: No such file or directory Jul 31, 2017 at 14:32
  • @Andrej 755 is an alternative of a+x here though they are not exactly the same. The last line chmod ... is not part of the hook commit-msg. Copy the code into a file and name the file commit-msg, make it executable and copy it to the current repository's .git/hooks/. When git commit is done, the hook commit-msg is invoked and checks the commit message. If it does not contain "updated", the commit fails.
    – ElpieKay
    Jul 31, 2017 at 14:57
  • I think this should be used chmod +x .git/hooks/commit-msg Sep 23, 2019 at 2:55
  • 3
    Alternatively, you can use [[ $(cat $1) =~ updated ]] || exit 1
    – Northnroro
    Mar 17, 2020 at 9:52
1

You can use commitlint with git pre-commit hook.

The simplest way I found to use it is via pre-commit framework. The nice thing is that you can easily add and customize any other check before commiting, here's a list of supported hooks.

Adding commitlint with pre-commit

pip install pre-commit
or brew install pre-commit

Create your .pre-commit-config.yaml (check and update rev if needed):

repos:
- repo: https://github.com/alessandrojcm/commitlint-pre-commit-hook
  rev: v2.3.0
  hooks:
      - id: commitlint
        stages: [commit-msg]
        additional_dependencies: ['@commitlint/config-conventional']
        args: ["--config",".commitlintrc.yaml"]

You have a lot of options with a commitlint configuration file .commitlintrc.yaml:

rules:
  body-leading-blank: [1, always]
  body-max-line-length: [2, always, 100]
  footer-leading-blank: [1, always]
  footer-max-line-length: [2, always, 100]
  header-max-length: [2, always, 100]
  subject-case:
    - 2
    - never
    - [sentence-case, start-case, pascal-case, upper-case]
  subject-empty: [2, never]
  subject-full-stop: [2, never, "."]
  type-case: [2, always, lower-case]
  type-empty: [2, never]
  type-enum:
    - 2
    - always
    - [build, chore, ci, docs, feat, fix, perf, refactor, revert, style, test]

Install commit message hook pre-commit install --hook-type commit-msg

And then you can try to git commit.

You can also install commitlint directly via npm, here's how.

# Install commitlint cli and conventional config
npm install --save-dev @commitlint/{config-conventional,cli}

# Configure commitlint to use conventional config
echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js

I recommend Conventional Commits format.

The commit message should be structured as follows:

<type>[optional scope]: <description>

[optional body]

[optional footer(s)]

Default types: build, chore, ci, docs, feat, fix, perf, refactor, revert, style, test

Examples:

  • feat(parser): add ability to parse arrays
  • docs: correct spelling of CHANGELOG
  • ci: add docker build

Some benefits of using this structure:

  • Automatically generating CHANGELOGs.
  • Automatically determining a semantic version bump (based on the types of commits landed).
  • Communicating the nature of changes to teammates, the public, and other stakeholders.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.