is there a way I can dynamically save the hash number of the commit as I am commiting
No. The hash identifying the commit is generated when the commit object is completely created. So, only after completing the commit, writing the commit message etc. you can access the commit hash.
However, as you write it in your example, you seem to first commit and then save the commit hash into a file. If you don’t want to include that file in the commit itself (which wouldn’t work because it would change the tree within the commit), then that’s perfectly possible.
The simple command to get the hash of the current HEAD, i.e. the pointer that points to your just created commit is this:
git rev-parse HEAD
Ideally what I want to have is a bulletproof way of knowing which commit version is the file I am looking at.
What you probably want is something like Subversion’s $Revision: X$
. But for above reasons, this is not possible. What you could do is provide clean/smudge filters that perform such an operation on checkout, but I wouldn’t recommend that.
Another way would be to use the ident
attribute on files which actually does expand $Id$
to the blob id. Not the commit id though. See also this question.