GIT how to inject commit infomation inside a file so that during/after push to remote?

745 Views Asked by At

We were given a task to inject commit information like committer, commit date and author name inside a file committed.

After some search and this thread How can I populate the Git commit ID into a file when I commit? we achieved injection into a file using keyword expansion and format placeholders. However, information was injected either after checkout or archive.

Is it possible to inject commit info into a file during or after push so that file in remote contains that commit information? We realize this may sound like non-necessary job but we would like to know about possibility of performing this requirement.

1

There are 1 best solutions below

1
On BEST ANSWER

You cannot do that because commit id is in fact SHA-1 hash calculated from the whole repository snapshot (all files) including also the file into which you want to write that commit id (which would change file and thus also commit id).

Also storing any information about commit into a file is redundant with what git already does and doesn't make much sense. It would help if you explain a bit more in your question why you want to do this.

If you just want this information visible somewhere (to e.g. pin certain deployable artifact to a commit in source repository) you don't really need to commit it but just call out to git and ask it for current HEAD commit properties and build these in to the binary artifact.