git hooks will fire at various times, depending on the name. You can find example hooks inside .git/hooks/ in any git project.
You might want a distinct number to represent this “build”, or commit (even though we don't really have binary builds in languages like php). We can use a post-commit hook to pull in a subset of the SHA-1 hash. In this example we use the first 6 characters of the SHA-1, writing out to a file named “BUILD”.
#!/bin/sh git log|head -n 1|cut -c 8-13 - > BUILD
Don't forget this gotcha…when you change branches, the build file would keep the commit hash from the last commit. It is likely that your other branch wouldn't be on the same commit, so we need another hook to update this when we do any checkout. Copy the same contents of post-commit to a new file called post-checkout.
You should also see global hooks.
You probably wouldn't want to do this all the time. An example where it is very handy is if you might be working on a project that is a walkthrough from a book or web site. Although you can commit your code, it may depend on the state of the database. A prime example would be a drupal project.
#!/bin/sh # adds a compressed database dump to every commit echo "creating compressed database dump..." mysqldump -umysqluser -pmysqlpass databasename | gzip > filename.gz git add filename.gz exit $?
There are several ways to set up hooks so that all projects use the same set of files. I prefer to symlink a hooks directory. See git on windows for examples of using mklink in windows. Basically we allow git to do its think with git init or git clone, then we delete the .git/hooks directory and replace it with a symlink called hooks which points to our global location.