It might be better to do this as part of your packaging, rather than after every commit.
There are two primary options:
-
Use
git-archiveto package, and use theexport-substattribute. Unfortunately, the things you can substitute in are limited to the placeholders fromgit log --format=.... For example, you could write__version__ = $Format:%H$in your file, put<filename> export-substin your .gitattributes, and when you rungit archive, that’d be changed to the full hash of the commit you’re archiving with. This is just about what you’re asking for, but I do prefer the next option. -
Do it yourself as part of a packaging process (often a build process for compiled packages), and use
git describe. That will get you a nice pretty string likev1.7.4.1-59-ge3d3f7d, meaning “59 commits past the tagv1.7.4.1, at commitge3d3f7d” which you can then insert somehow into the right place in your code as you package/build. This is what Git itself does; the result is dumped to a file, whose contents are read into the makefile and then passed into the build via a-Dpreprocessor option, and placed into various filenames (e.g. the release tarball) directly.
If you really, really want to do this after every commit, you could, with a post-commit hook, but then only you (and those you give the hook to) will have it, and it’s very very possible to get out of sync – you’ll also have to have a post-checkout hook, and so on and so on. It’s really better for whatever processes that create something needing this version number to get it themselves.
You could also use a smudge/clean filter, which would be more like what you actually want (rather than simply after every commit).