GIT Commit Message Hook on Windows (without Admin Rights)

Standard

 

To get GIT to prevent a commit unless your commit message contains a certain format you need to use a GIT commit-msg hook.

To create the hook you need to create a hooks folder under your .git folder within each GIT repository. Within the .git/hooks folder place your commit-msg file.

The following commit-msg script makes sure that your commit message must start with the text “[Mobile]” – maybe Mobile┬áis your team name.

#!/bin/sh
commit_regex='^\[Mobile\]'

error_msg="Aborting commit. Your commit message is missing '[Mobile]' at beginning of comment."

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

To have one central commit-msg file create a junction (if you don’t have Window’s Administrator Rights) or a symlink if you prefer, but you’ll need admin rights.

For example, if you have a repository called my-app and your central hooks folder is stored in your “OneDrive for Business” folder.

cd C:\Users\philip.murphy\Documentes\my-app\.git\
mklink /J /D hooks "C:\Users\philip.murphy\OneDrive for Business\hooks"

You should do this for each repository. Now when you update the central commit-msg file, all repositories will reflect your new commit message hook rules.

BTW, if you want to initially populate your commit comment with some text:

git config --global commit.template "C:\Users\philip.murphy\OneDrive for Business\committemplate.txt"

It may contain the text that you want to initially populate the comment message with:

[MOBILE]

Note for SourceTree users. The commit-msg hook works well, but the commit template doesn’t.

2016-05-30_231747

Example output of commit being prevented as comment doesn’t begin with [Mobile].

Advertisements