Git Hooks Tutorial
My public talk about this project at Sberloga:
Git Hooks Is All You Need
1. Git Hooks 101
- Init git repo:
mkdir git_repo
cd git_repo
git init
- Git hooks are located in
.git/hooks
folder:
ls -lah .git/hooks
- Create executable (
chmod +x
) pre-commit hooks (path:.git/hooks/pre-commit
):
Make commit after each step. You'll see different results.
2. Python pre-commit library
- Create and activate virtual environment:
python3 -m venv venv
source venv/bin/activate
- Install and activate pre-commit library:
pip install pre-commit
pre-commit install
-
Create pre-commit configuration file .pre-commit-config.yaml.
Alternatively, you can use local configuration. -
Create main.py file.
Make commit. Pre-commit library will reformat file according to PEP8.
Remove unusedimport sys
string to get ready-to-commit main.py file.
3. Remote repo
- Init empty "remote" repo:
git init --bare ../remote_repo.git
- Link local and "remote" repo:
git remote add origin ../remote_repo.git
- Create executable (
chmod +x
) pre-receive hook on "remote" (path:../remote_repo.git/hooks/pre-receive
).
Make push and see the result.
Reference
Useful links: