Skip to content

Instantly share code, notes, and snippets.

@raftaar1191
Last active February 8, 2018 05:54
Show Gist options
  • Save raftaar1191/c0c9d4981327b20f8ab02f1d08bef168 to your computer and use it in GitHub Desktop.
Save raftaar1191/c0c9d4981327b20f8ab02f1d08bef168 to your computer and use it in GitHub Desktop.
Create a global git commit hook
1> Create a directory to hold the global hooks:
mkdir -p ~/git-templates/hooks
2> Enable git templates:
git config --global init.templatedir '~/git-templates'
3> Write your hooks in ~/.git-templates/hooks.
cd ~/git-templates/hooks && touch commit-msg
4> Copy Code to commit-msg file
```
#!/usr/bin/env bash
# regex to validate in commit msg
commit_regex='(build|doc|feat|fix|font|image|perf|refactor|style|test)\([a-z\-]+\)\:\s'
error_msg="Aborting commit. The commit message should be of the format '{type}({scope}): Your commit message'\n"
visit_link="Learn more @ https://github.com/WordImpress/Give/wiki/Prefixes\n"
if ! grep -iqE "$commit_regex" "$1"; then
printf "\033[0;31m$error_msg $visit_link\033[0m" >&2
exit 1
fi
```
5> Make sure the hook is executable.
chmod a+x ~/git-templates/hooks/commit-msg
6> 5. Re-initialize git in each existing repo you'd like to use this in:
git init
@raftaar1191
Copy link
Author

Reset Git templates global

git config --global --unset-all init.templatedir

@raftaar1191
Copy link
Author

@raftaar1191
Copy link
Author

Its' not working in Sourcetree Direclty

we have to do this setting to work in Sourcetree
https://medium.com/@onmyway133/sourcetree-and-pre-commit-hook-52545f22fe10

@raftaar1191
Copy link
Author

Sourcetree

This can be implemented in one of the two ways. Git 2.9+ has a new setting called core.hooksPath which can be set to a custom folder that has the hooks. Another is to create symlinks. I opted for the 2nd approach assuming developers might not be on Git version 2.9+

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