In this tutorial, we aim to automate tasks using Git hooks. Git hooks are scripts that Git executes before or after events such as commit, push, and receive. They are a built-in feature of Git, which makes them an ideal tool for automating tasks in your workflow, such as linting code or deploying to a server.
By the end of this tutorial, you will be able to:
Prerequisites: Basic knowledge of Git and Bash scripting.
Git hooks reside in the .git/hooks
directory of every Git repository. By default, Git provides several sample hooks in this directory.
Hooks are either designated as 'pre' or 'post' actions. A 'pre' hook is executed before the event, and if it fails (exits with a non-zero status), the event is aborted. On the other hand, a 'post' hook is executed after the successful completion of the event.
To create a Git hook, you need to create a file in the .git/hooks
directory, give it an appropriate name related to the event it should respond to (like pre-commit
, post-commit
, etc.), and make it executable.
Here's an example of creating a pre-commit
hook:
bash
cd /path/to/your/repo
pre-commit
:bash
touch .git/hooks/pre-commit
bash
chmod +x .git/hooks/pre-commit
```bash
#!/bin/sh
if git grep -q "TODO" -- ':!*.md'; then
echo "Commit contains TODO, aborting commit."
exit 1
fi
```
pre-commit
Hook for Linting#!/bin/sh
# Run linter
npm run lint
# If linting fails, exit with non-zero status code
if [ $? -ne 0 ]; then
echo "Lint errors found. Please fix them before committing."
exit 1
fi
This pre-commit
hook runs your project's linting script before every commit. If there are any linting errors, the commit is aborted.
pre-push
Hook for Running Tests#!/bin/sh
# Run tests
npm test
# If tests fail, exit with non-zero status code
if [ $? -ne 0 ]; then
echo "Tests failed. Please fix them before pushing."
exit 1
fi
This pre-push
hook runs your project's test suite before every push. If any test fails, the push is aborted.
In this tutorial, we learned about Git hooks and how to use them to automate tasks in our development workflow. We learned how to create custom hooks and provided examples of hooks for linting code and running tests.
For further learning, you can explore more complex uses of Git hooks, like automating deployment or integrating with continuous integration services.
pre-commit
hook that checks for console.log statements in your code. If any are found, abort the commit.Solution: Here's a possible solution using a Bash script:
```bash
#!/bin/sh
if git grep -q "console.log" -- ':!*.md'; then
echo "Commit contains console.log, aborting commit."
exit 1
fi
``
This script uses
git grep` to search for "console.log" in the project files. If it's found, the script exits with a non-zero status code, aborting the commit.
post-commit
hook that automatically pushes to the remote repository after every successful commit.Solution: Here's a possible solution using a Bash script:
```bash
#!/bin/sh
git push origin main
```
This script automatically pushes to the 'main' branch of the 'origin' remote after every successful commit.
pre-push
hook that runs your project's build script before every push. If the build fails, abort the push.Solution: Here's a possible solution using a Bash script:
```bash
#!/bin/sh
# Run build script
npm run build
# If build fails, exit with non-zero status code
if [ $? -ne 0 ]; then
echo "Build failed. Please fix the issues before pushing."
exit 1
fi
```
This script runs your project's build script before every push. If the build fails, the script exits with a non-zero status code, aborting the push.
Remember to test your Git hooks thoroughly to ensure they behave as expected. Happy coding!