Git & GitHub - Git Hooks Advanced
Advanced usage of Git hooks
Git hooks are scripts that run automatically at specific points in the Git workflow. They are useful for automating tasks, enforcing policies, and integrating with other tools. This guide covers advanced usage of Git hooks, including creating custom hooks, chaining hooks, and using hooks for CI/CD.
Key Points:
- Git hooks automate tasks and enforce policies at different stages of the Git workflow.
- Custom hooks can be created to extend Git's functionality.
- Chaining hooks allows multiple hooks to run sequentially.
- Hooks can be integrated with CI/CD pipelines for automated testing and deployment.
Creating Custom Hooks
Step 1: Identify Hook Points
Identify the hook point where you want your custom script to run. Common hook points include pre-commit
, post-commit
, pre-push
, and post-merge
.
Step 2: Create the Hook Script
Create a script in the .git/hooks
directory with the appropriate hook name:
# Example: Creating a pre-commit hook
$ touch .git/hooks/pre-commit
$ chmod +x .git/hooks/pre-commit
# Edit the pre-commit hook script
$ nano .git/hooks/pre-commit
Step 3: Write the Hook Logic
Write the logic for your hook script. For example, a pre-commit hook to check for TODO comments:
#!/bin/bash
# pre-commit hook to check for TODO comments
if grep -r "TODO" .; then
echo "Commit aborted: TODO comments found."
exit 1
fi
Step 4: Test the Hook
Test the hook by performing the action that triggers it (e.g., making a commit for a pre-commit hook):
# Test the pre-commit hook
$ git add .
$ git commit -m "Testing pre-commit hook"
Chaining Hooks
Chaining hooks allows you to run multiple scripts sequentially at the same hook point. Create a main hook script that calls other scripts:
# Example: Chaining pre-commit hooks
# .git/hooks/pre-commit
#!/bin/bash
# Call multiple pre-commit scripts
./.git/hooks/pre-commit-1.sh
./.git/hooks/pre-commit-2.sh
Create the individual hook scripts:
# .git/hooks/pre-commit-1.sh
#!/bin/bash
echo "Running pre-commit script 1"
# Add script logic here
# .git/hooks/pre-commit-2.sh
#!/bin/bash
echo "Running pre-commit script 2"
# Add script logic here
Ensure all scripts are executable:
# Make all scripts executable
$ chmod +x .git/hooks/pre-commit .git/hooks/pre-commit-1.sh .git/hooks/pre-commit-2.sh
Using Hooks for CI/CD
Pre-Push Hook for Running Tests
Use a pre-push hook to run tests before pushing changes to a remote repository:
# .git/hooks/pre-push
#!/bin/bash
# Run tests before pushing
echo "Running tests..."
if ! ./run-tests.sh; then
echo "Push aborted: Tests failed."
exit 1
fi
Post-Receive Hook for Deployment
Use a post-receive hook on the server to automatically deploy code after receiving a push:
# .git/hooks/post-receive
#!/bin/bash
# Deploy code after receiving a push
DEPLOY_DIR="/var/www/myapp"
GIT_DIR="/home/user/myapp.git"
WORK_TREE="/home/user/myapp"
echo "Deploying code..."
git --work-tree=$WORK_TREE --git-dir=$GIT_DIR checkout -f
Best Practices
Follow these best practices when using Git hooks:
- Keep Hooks Simple: Write simple and focused hook scripts to avoid complexity and errors.
- Use Version Control: Store your hook scripts in version control to track changes and collaborate with your team.
- Test Hooks Thoroughly: Test your hooks in different scenarios to ensure they work as expected.
- Document Hooks: Document the purpose and usage of each hook script to help your team understand their functionality.
Summary
This guide covered advanced usage of Git hooks, including creating custom hooks, chaining hooks, and using hooks for CI/CD. Git hooks are a powerful tool for automating tasks, enforcing policies, and integrating with other tools, helping you maintain a smooth and efficient workflow.