0

I highly doubt I can do this, given that the commit hash is generated on commit, but is there a way I can dynamically save the hash number of the commit as I am commiting.

Example:
I make a couple changes and commit
The commit hash is saved to file
Then I sync the commit to the Github server

Ideally what I want to have is a bulletproof way of knowing which commit version is the file I am looking at. I could implement versions, but given the extremely rapid pace of development (often times 5-6 commit in a couple minutes time) its very unpractical.

I am open to alternative suggestions.

chriscct7
  • 527
  • 5
  • 11
  • Why do you need to have this information in the file? It's very easy to ask git about that. And if the versions are changing that much, having a hash in the file doesn't provide any value in my opinion – Ikke Nov 26 '12 at 17:46
  • What I want to do is be able to print off the git hash on run. The hash gives me a unique identifier regardless of branch or version or anything else as a reference point. – chriscct7 Nov 26 '12 at 17:50
  • Can't you run git symbolic-ref HEAD on run to get the hash? – Ikke Nov 26 '12 at 17:53
  • No, I'm printing this hash off in a config file. I cannot assume the user has git installed. – chriscct7 Nov 26 '12 at 17:55
  • One thought I had would be to find a way to automate the commit process so after commit, a second commit would automatically happen that saves the prevous commits hash to fine and then commit that. – chriscct7 Nov 26 '12 at 17:57
  • What about exporting the last commit hash generated to a file? – random Jan 18 '13 at 05:58

3 Answers3

0

No, you can't include the commit ID in a file that is in the commit. The sha1 hash that identifies the commit, so inserting one ID into a file would cause the hash that will be used for the commit to change.

qqx
  • 18,947
  • 4
  • 64
  • 68
0

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.

Community
  • 1
  • 1
poke
  • 369,085
  • 72
  • 557
  • 602
0

You might look at using a Git post-commit hook, though the commit hash must be stored in a separate file as others have noted above.

Mark Leighton Fisher
  • 5,609
  • 2
  • 18
  • 29
  • Yeah. What I want to do is store it in a version.txt file and then just read it from there using get_contents. – chriscct7 Nov 27 '12 at 01:59