Improving development workflow using Git Hooks
Git hooks are scripts that are triggered at certain points in git’s execution. Think of git hooks as a program which runs before/after a predefined action.There can be client or server side hooks.Client-side hooks are triggered by operations such as committing and merging, while server-side hooks run on network operations such as receiving pushed commits.
Here are some of the git hooks
prepare-commit-msg
– Provide a default commit message if one is not given.pre-commit
– Runs beforegit commit
commit-msg
– Commit message validation.post-commit
– Runs after a successful commit.pre-push
– Runs beforegit push
after the remote is verified to be working. It takes 2 arguments: the name of the remote and the URL to it.pre-rebase
– Runs beforegit rebase
.post-checkout
– Runs after a successful checkout.post-merge
– Runs after a successful merge
Why should you use a git hook?
Any custom script can be placed in any of the git hooks.This will ensure the developer follows a set of guidelines during the development process. For example, a company policy states that no function should have more than 50 lines of code.Just place this guideline in form of a script in a pre-commit
hook.
Git hooks are also very helpful in preventing what is called as a Commit Porn.Commit messages like Fix some issues
can easily be prevented by enforcing a proper format to the commit messages. (See more of commit porn — http://whatthecommit.com/ , keep on refreshing to see new messages)
Or Git hooks can be used to enforce that code formatting is according to the company’s style guide principles.
These are only some of the many examples which developers can use to improve their development workflow.
How to set up a git hook?
By default the hooks directory is $Project_Root/.git/hooks
We will update pre-commit
hook to enforce style guides for Javascript using jshint
.
- Install jshint in your system.
Runnpm install --save-dev jshint
- To use any git hook we must remove the
.sample
extension.
Renamepre-commit.sample
topre-commit
.Now pre-commit hook is active. - Copy the following code and paste in the
pre-commit
file.
#!/bin/sh
echo "pre-commit started"
files=$(git diff --cached --name-only --diff-filter=ACM | grep ".js$")
if [ "$files" = "" ]; then
exit 0
fi
pass=true
for file in ${files}; do
result=$(jshint ${file})
if [ $? == 0 ]; then
echo "\033[32mJSHint Passed: ${file}\033[0m"
else
echo "\033[31mJSHint Failed: ${file}\033[0m"
echo "\033[31m$(jshint ${file} | sed '$d' | sed '$d')\033[0m\n"
pass=false
fi
done
if ! $pass; then
echo "\033[41mCOMMIT FAILED:\033[0m Please install JSHINT(if not installed) and then make sure there are no JSHINT errors \n"
exit 1
fi
The above script runs on each of the javascript file modified and then runs jshint to check for style guidelines errors. The style guidelines can be put in .jshintrc
file at the root level of your project.
Refer : http://jshint.com/docs/
As the last step you should set proper permissions for your git hook to run.
Run chmod +x .git/hooks/pre-commit
That’ it . Now if the developer commits and there are some jshint style violations , git will throw an error.
How to share git hooks ?
Git Hooks are not shared.This means that you cannot push them or pull it from/to a remote repository.However, you can create symbolic link — :
- Create a folder named
hooks
in your project at root level - Add a file named
pre-commit
and write your hook script in it. - Run
ln -s -f ../../hooks/pre-commit ./git/hooks/pre-commit
By this way, the hooks can also be under version control.Anyone who clones your repo should run the above command for the pre-commit
hook to be activated.
Git hooks are extremely powerful and can help you automate your workflow to a large extend.
Are you using git hooks in a cool way? Let me know in the comments.