Git hooks are scripts that Git executes before or after events such as commit, push, and receive. They allow you to automate tasks and enforce policies in your Git workflow. This section will cover the basics of Git hooks, how to set them up, and provide practical examples.
What are Git Hooks?
Git hooks are custom scripts that run automatically when certain events occur in a Git repository. They can be used to:
- Enforce coding standards
- Run tests before committing code
- Automatically format code
- Notify team members of changes
Types of Git Hooks
Git hooks are divided into two main categories:
- Client-Side Hooks: These hooks are triggered by operations such as committing and merging. They are stored in the
.git/hooks
directory of your local repository. - Server-Side Hooks: These hooks are triggered by network operations such as receiving pushed commits. They are stored in the
hooks
directory of the Git repository on the server.
Common Client-Side Hooks
Hook Name | Trigger Event | Description |
---|---|---|
pre-commit |
Before a commit is made | Used to inspect the snapshot before committing. |
prepare-commit-msg |
Before the commit message editor is fired up | Used to edit the default commit message. |
commit-msg |
After the commit message is created | Used to validate or modify the commit message. |
post-commit |
After a commit is made | Used to perform actions after a commit, such as notifications. |
pre-push |
Before a push is made | Used to run tests or checks before pushing changes to a remote repository. |
Common Server-Side Hooks
Hook Name | Trigger Event | Description |
---|---|---|
pre-receive |
Before changes are accepted | Used to enforce policies before changes are accepted. |
update |
Before a branch is updated | Used to enforce policies on a per-branch basis. |
post-receive |
After changes are accepted | Used to perform actions after changes are accepted, such as deployment. |
Setting Up Git Hooks
To set up a Git hook, follow these steps:
- Navigate to the
.git/hooks
directory in your repository. - Create a new file with the name of the hook you want to set up (e.g.,
pre-commit
). - Make the file executable by running
chmod +x <hook-name>
. - Write your script in the file.
Example: Pre-Commit Hook
Let's create a simple pre-commit
hook that checks for TODO comments in the code before allowing a commit.
-
Navigate to the
.git/hooks
directory:cd .git/hooks
-
Create the
pre-commit
file:touch pre-commit
-
Make the file executable:
chmod +x pre-commit
-
Add the following script to the
pre-commit
file:#!/bin/sh # Check for TODO comments if grep -r "TODO" .; then echo "Commit rejected: TODO comments found." exit 1 fi
This script will search for "TODO" comments in the code and reject the commit if any are found.
Practical Example: Running Tests Before Commit
Another common use case for Git hooks is to run tests before allowing a commit. This ensures that only code that passes tests is committed to the repository.
-
Navigate to the
.git/hooks
directory:cd .git/hooks
-
Create the
pre-commit
file:touch pre-commit
-
Make the file executable:
chmod +x pre-commit
-
Add the following script to the
pre-commit
file:#!/bin/sh # Run tests npm test if [ $? -ne 0 ]; then echo "Commit rejected: Tests failed." exit 1 fi
This script will run the npm test
command before allowing a commit. If the tests fail, the commit will be rejected.
Exercises
Exercise 1: Create a Pre-Commit Hook
Task: Create a pre-commit
hook that checks for trailing whitespace in the code and rejects the commit if any is found.
Solution:
-
Navigate to the
.git/hooks
directory:cd .git/hooks
-
Create the
pre-commit
file:touch pre-commit
-
Make the file executable:
chmod +x pre-commit
-
Add the following script to the
pre-commit
file:#!/bin/sh # Check for trailing whitespace if grep -r "[[:blank:]]$" .; then echo "Commit rejected: Trailing whitespace found." exit 1 fi
Exercise 2: Create a Post-Commit Hook
Task: Create a post-commit
hook that prints a message to the console after a commit is made.
Solution:
-
Navigate to the
.git/hooks
directory:cd .git/hooks
-
Create the
post-commit
file:touch post-commit
-
Make the file executable:
chmod +x post-commit
-
Add the following script to the
post-commit
file:#!/bin/sh # Print a message after commit echo "Commit successful!"
Summary
In this section, we covered the basics of Git hooks, including what they are, the different types, and how to set them up. We also provided practical examples and exercises to help you get started with using Git hooks in your workflow. By leveraging Git hooks, you can automate tasks, enforce policies, and improve the overall quality of your codebase.
Mastering Git: From Beginner to Advanced
Module 1: Introduction to Git
Module 2: Basic Git Operations
- Creating a Repository
- Cloning a Repository
- Basic Git Workflow
- Staging and Committing Changes
- Viewing Commit History
Module 3: Branching and Merging
- Understanding Branches
- Creating and Switching Branches
- Merging Branches
- Resolving Merge Conflicts
- Branch Management
Module 4: Working with Remote Repositories
- Understanding Remote Repositories
- Adding a Remote Repository
- Fetching and Pulling Changes
- Pushing Changes
- Tracking Branches
Module 5: Advanced Git Operations
Module 6: Git Tools and Techniques
Module 7: Collaboration and Workflow Strategies
- Forking and Pull Requests
- Code Reviews with Git
- Git Flow Workflow
- GitHub Flow
- Continuous Integration with Git
Module 8: Git Best Practices and Tips
- Writing Good Commit Messages
- Keeping a Clean History
- Ignoring Files with .gitignore
- Security Best Practices
- Performance Tips
Module 9: Troubleshooting and Debugging
- Common Git Problems
- Undoing Changes
- Recovering Lost Commits
- Dealing with Corrupted Repositories
- Advanced Debugging Techniques