Git hooks overview Automate your git workflow with hooks - scripts that run before or after git events
Ever forget to run tests before committing? Or wish your code would format itself automatically? Git hooks are here to save the day. They’re like little scripts that run automatically when you do git stuff, making your workflow smoother and more reliable.
Git hooks are scripts that git executes at certain points in your workflow. They can run before or after events like commit, push, merge, and more. Think of them as automated assistants that check your work and can even fix things for you.
Let’s start with the basics. Git hooks are stored in the .git/hooks
directory of your repository. Each hook has a specific name that tells
git when to run it. The most commonly used hooks are pre-commit
,
post-commit
, pre-push
, and post-merge
.
Here’s what happens when you run git commit
:
- Git runs pre-commit hook
- If pre-commit succeeds, git creates the commit
- Git runs post-commit hook
The beauty is that if any pre-hook fails (exits with non-zero status), git stops the operation. This is perfect for running tests, linting, or any other checks you want to enforce.
Let’s create a simple pre-commit hook that runs your tests:
#!/bin/bash
# .git/hooks/pre-commit
echo "Running tests before commit..."
make test
if [ $? -ne 0 ]; then
echo "Tests failed! Commit aborted."
exit 1
fi
echo "Tests passed! Proceeding with commit."
Make it executable:
chmod +x .git/hooks/pre-commit
Now every time you commit, git will run your tests first. If tests fail, the commit is cancelled. Pretty neat, right?
You can also use hooks to automatically format your code. Here’s a pre-commit hook that runs prettier:
#!/bin/bash
# .git/hooks/pre-commit
echo "Formatting code with prettier..."
npx prettier --write .
# Add the formatted files to the commit
git add .
This way, your code is always formatted consistently, and you don’t even have to think about it!
You can also use hooks to update external systems. For example, a post-push hook could trigger a deployment:
#!/bin/bash
# .git/hooks/post-push
if [[ $1 == "origin" && $2 == "main" ]]; then
echo "Pushed to main branch, triggering deployment..."
curl -X POST https://your-deploy-service.com/deploy
fi
There are many other hooks available for different git operations. Pre-commit hooks run tests and checks before creating a commit, while post-commit hooks handle logging, notifications, and cleanup tasks. Pre-push hooks perform final validations before sharing code, such as running full test suites or checking for sensitive data.
Pre-merge-commit hooks can validate changes before merging branches, while post-merge hooks are perfect for updating dependencies or running migrations. Pre-rebase hooks can prevent rebasing on certain branches, and post-checkout hooks can set up environment-specific configurations.
Pre-receive and post-receive hooks run on the server side when someone pushes to a shared repository, making them ideal for enforcing team policies, running CI/CD pipelines, or updating deployment environments.
Happy hacking!