Open In App

Customizing Git Hooks for Workflow Automation

Git is a distributed version control system that monitors changes to a project. Git hooks are scripts that run in response to Git events such as commit or push. They automate tasks, enforce rules, and can be tailored to your project’s requirements. A pre-commit hook, for example, can detect errors in the code, whereas a post-receive hook can send email notifications. Customizing these hooks can help streamline your workflow and make Git a more versatile tool.

There are two types of Git Hooks: server-side and client-side. Client-side hooks function locally, while server-side hooks run on the Git server. Git hooks are commonly used to run tests before committing changes, enforce code-style guidelines, and notify team members after successful pushes.



Use Cases of Git Hooks

Types of Git Hooks

Steps to create a hook from scratch

cd /path/to/your/repo/.git/hooks
touch pre-commit
chmod +x pre-commit

Example of a simple pre-commit hook script that prevents commits with a certain word in the commit message:



#!/bin/bash
if git rev-parse --verify HEAD >/dev/null 2>&1
then
    against=HEAD
else
    # Initial commit: diff against an empty tree object
    against=$(git hash-object -t tree /dev/null)
fi
# Check for forbidden words in the commit message
if test $(git diff --cached --name-only | xargs grep -l 'FORBIDDEN_WORD' | wc -l) != 0
then
    echo "Your commit contains a forbidden word. Please remove it."
    exit 1
fi
# If there were no issues, exit with success
exit 0

Remember that Git hooks are unique to your Git repository and are not versioned. If you want to share your hooks with others, add them to your project in a separate directory and create a script to copy them to the .git/hooks folder.

Customizing Git Hooks

Workflow automation with customized Git hooks can improve your team’s output and adherence to best practices significantly.

How to customize Git hooks for various automation tasks:

Example: Pre-commit Hook for Code Linting’:

#!/bin/bash
# Run the linter
linter_output=$(npm run lint)
# If the linter found issues, print them and exit with a non-zero status
if [[ $linter_output == *"ERROR"* ]]; then
  echo "Linting errors found, aborting commit:"
  echo $linter_output
  exit 1
fi
# If there were no issues, exit with success
exit 0

Example of “Post-commit Hook for Automated Push:

#!/bin/bash
# Push the commit to the remote repository
git push origin master
# Exit with success
exit 0

A pre-commit hook that runs tests before allowing a commit:

#!/bin/bash
run_test(){
    npm test
}
run_lint(){
    eslint .
}
run_formatting(){
    prettier --check .
}
run_test
run_lint
run_formatting
if [$? -ne 0 ]; then
    echo "Checks Failed. Aborting commit."
    exit 1
fi
exit 0

A post-merge hook that notifies team members after a successful merge.

#!/bin/bash
notify_team(){
    slack-cli -c react-assets-gh-bot "New changes into main branch"
}
if[ $? -eq 0]; then
    notify_team
fi

Best Practices

Custom Git Hooks – FAQs

What is a pre-commit hook?

It’s a script that runs before each commit and is used to inspect the snapshot about to be committed.

Can Git hooks be shared across team members?

Yes, though not directly through Git, they can be distributed via a separate shared repository or a file-sharing system.

How do I disable a Git hook?

You can disable a Git hook by removing the executable permission from the hook script file.

Can Git hooks be used to enforce coding standards?

Absolutely, pre-commit hooks are often used to run linters or other code quality checks.


Article Tags :
Git