Blog

Git pre-commit hook & git commit --all

Informatique,
Tags: git, scripts

It's generally good when a task you have to do many times a day is really fast. For example, when I want to make a commit, I just type gc <message>, because I made gc an alias for git commit -am. And when you have to commit, it's hard to go faster.

But sometimes, fast is not enough.

Today, I just tried to add a pre-commit hook that checks pep8 for the python files I’m about to commit. Easy.

The man page says that pre-commit is bypassed when we use the option --no-verify, so everything should be OK.

BUT sadly git commit -a also bypasses the pre-commit hook. And I don't wanna loose my time adding manually all the modified files in the repo. git add * is definitively not a solution.

However, the solution is quite easy: let the pre-commit script do the right git add stuff !

By "the right stuff", I mean git update-index --add, because it seems that on some systems with some versions of git, git add in a pre-commit adds files but for the next commit… 

Here is my implementation of this solution: https://github.com/nim65s/scripts/blob/master/pre-commit

Now, you may want to add this pre-commit to all your git repositories in every computer you use… Well, just look at this one https://github.com/nim65s/scripts/blob/master/pre-commit-deploy.sh (it just takes a ~/.gitrepos which contains all the repositories you use on the current computer… ie the config file for this script https://github.com/nim65s/scripts/blob/master/gitup.sh)