Skip to content

Instantly share code, notes, and snippets.

Last active November 29, 2022 12:08
What would you like to do?
Git commit-msg hook to validate for jira issue or the word merge
#!/usr/bin/env bash
# set this to your active development branch
current_branch="$(git rev-parse --abbrev-ref HEAD)"
# only check commit messages on main development branch
[ "$current_branch" != "$develop_branch" ] && exit 0
# regex to validate in commit msg
error_msg="Aborting commit. Your commit message is missing either a JIRA Issue ('WAP-1111') or 'Merge'"
if ! grep -iqE "$commit_regex" "$1"; then
echo "$error_msg" >&2
exit 1


  • copy the file commit-msg to .git/hooks/commit-msg
  • make sure your delete the sample file .git/hooks/commit-msg.sample
  • Make commit msg executable. chmod +x .git/hooks/commit-msg
  • Edit commit-msg to better fit your development branch, commit regex and error message
  • Profit $$

Shell example

curl > .git/hooks/commit-msg

rm .git/hooks/commit-msg.sample

chmod +x .git/hooks/commit-msg

vim .git/hooks/commit-msg

echo "Profit $$"
Copy link

The commit msg hook can be enforced on local repository by adding it manually.
Is there a way we can push this hook to remote repository and everyone who clones it will be enforced to commit messages in certain format ?

Copy link

You'd need to write a server-side commit hook for that

This might help:

Copy link

Alternatively, if your projects use any sort of automation, e.g. using Composer or npm or other to install dependencies, you can use that to install the git hook.

Copy link

sebinsua commented Jun 19, 2017

Copy link

opyate commented Oct 10, 2017

Copy link

bcowgill commented Jan 24, 2018

I believe that last line should be:

echo "Profit \$$$"

Copy link

swmagesh commented Mar 17, 2018

How to validate the code error free before commit? Anyone please help!!!

Copy link

pgilad commented Apr 11, 2018

@swmagesh, what do you mean by error free? Do you mean running lint or tests before the commit?

Copy link

@pgilad inspired by your and @opyate work, I've crafted this:

It auto inserts into the message Jira issues found in branch's name but not in the content otherwise behaves like yours. For example commit in branch "WAP-1234.ThisIsAPullRequestBranch" with message "support for WAP-567" will commit message "WAP-1234, support for WAP-567".

Copy link

configuration free git-commit-msg-linter

Copy link

UncleCJ commented May 6, 2020

Found this old gem - much appreciated! I expanded it and shared with the corporation:

#!/usr/bin/env bash

commit_regex = '^((build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test|Publish)(\([a-zA-Z0-9 /_,.-]+?\))?: .{1,59}\w (\([A-Z]+-[0-9]+\)|\(#[0-9]+\)|\[skip ci\]|\[ci\])|Merge branch .+|(fixup|squash)! .+|WIP: .+)$'

error_msg = '
.git/hooks/commit-msg: Aborting commit, try again with a valid message. The regexp checks for:

* a conventional commit message subject (to be parsed into changelogs, see ):
   - a change type, either of "build", "chore", "ci", "docs", "feat", "fix", "perf", "refactor", "revert", "style", "test" or "Publish"
   - optionally, in parenthesis, subsystem(s) affected by the change (comma delimited list)
   - a colon, space (": ") and a brief subject (60 characters maximum) not ending with a period
   - a space and in parenthesis, an issue id " (FOO-123)" or " (#123)"... or the tags "[skip ci]" or "[ci]" (do we use these?)
* ... or, a Git autosquash line, i.e. "fixup! My previous commit" or "squash! Another previous commit" (see )
* ... or, a merge commit "Merge branch x..."
* ... or, a work-in-progress tag "WIP: "
* optionally (not validated), two line breaks and a commit message body
This script is written by CJ Sveningsson (, inspired by (Gilad Peleg)'

if ! grep -iqE "${commit_regex}" "$1"; then
    echo "${error_msg}" >&2
    exit 1

Copy link

I wrote a very simple syntax checker for N amount of characters and numbers as the Jira key (e.g. ABCDE-12345):

[[ $(cat $1) =~ [A-Z]+-[0-9]+.* ]] || {
    echo >&2 "ERROR: Commit message requires JIRA key. Example: \"ABC-123: Test Commit\""
    exit 1

Hope it helps for anyone looking for a super basic checker.

Copy link

Reimirno commented Jul 3, 2022


I wrote a regex for commit message convention 1.0.0 which is based off the Angular Convention.

commit_regex='^(feat|fix|build|chore|docs|style|refactor|perf|test)(\(.+\))?!?: (.+[^.\r\n])([\r\n]+(.+[\r\n]+)+)?$'

This is a simple, lightweight commit message convention enforcing a format like this:

<type>[optional scope]: <description>

[optional body]

[optional footer(s)]

This should work out of box but you can modify it the way you want. Hope it's helpful!

Copy link

Hi Remirno, you should add the '?' question mark before group 2 because the scope is not a positional argument in the conventional commit,
so it should be like this;
^(feat|fix|build|chore|docs|style|refactor|perf|test)?(\(.+\))?!?: (.+[^.\r\n])([\r\n]+(.+[\r\n]+)+)?$

Copy link

You'd need to write a server-side commit hook for that

This might help:

@thekiwicoder0 I know this is an old issue. can you provide an example reference or more details??

Copy link

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