Recently I started to use Black code formatter for my projects and sometimes it can be hard to track did I formatted all my changed files or not. Let's assume that my project contains 20 different scripts (.py files) and I made changes on last 5 of them what I do normally before commit.
First option
black /project_directory
Second option
black script1.py
black script2.py
and etc.
Is there a way to automate this process before git commit or not ?
Thanks a lot in advance
Yes, you can use a pre-commit hook. If you're using Linux or macOS, or Windows with Git Bash, put the following in a file named
.git/hooks/pre-commit:Use
chmod +x .git/hooks/pre-committo make this runnable. Then, every time you do acommit, the hook will run first and format all your code. You can read more about hooks on the Git Hooks documentation.