Back to all articles
DevOpsGitDevOpsAutomationCI/CD
Development with Git Hooks
Automating linters, branch protection, and commit message compliance locally using client-side Git hooks.
YS
Yavuz Selim Yesilyurt
Senior Software Engineer
4 min read
Read on MediumDevelopment with Git Hooks
Waiting for CI pipelines to fail on simple formatting errors or lint violations slows down developer feedback loops. Client-side Git hooks allow teams to run fast checks locally before code is committed or pushed.
Client-Side Hooks
Git hooks are executable scripts located in .git/hooks/. Key lifecycle hooks include:
pre-commit: Runs before a commit message is entered. Ideal for running linters, code formatters, and static analysis.commit-msg: Inspects the commit message format (e.g. enforcing Conventional Commits likefeat:,fix:).pre-push: Runs before remote refs are updated. Suitable for quick unit tests.
Setting Up a Pre-Commit Hook
Here is an example pre-commit hook that ensures Go code is formatted and passes tests:
#!/bin/sh
set -e
echo "๐ Running gofmt check..."
DIFF=$(gofmt -l .)
if [ -n "$DIFF" ]; then
echo "โ Unformatted Go files detected:"
echo "$DIFF"
echo "Run 'gofmt -w .' before committing."
exit 1
fi
echo "๐งช Running unit tests..."
go test ./... -short -race
echo "โ
All pre-commit checks passed!"
Sharing Hooks with Teams
Because .git/hooks is not tracked by version control, teams can configure Git to use a versioned directory:
git config core.hooksPath .githooks
Placing your team's scripts in .githooks allows everyone to share the same pre-commit and commit-msg checks seamlessly.
