matanox
matanox

Reputation: 13686

get hash without committing

I need to track data being created by an application, such that the data has an identifier written alongside it, that tells the code version of the software that wrote it.

For this to work also in a development environment, it is clear that a git commit would not be made on every experimental change to the software, but rather after data has already been created by the changed software.

So is there a way to get a hash of the code, without a commit?

Squashing and rebasing as mentioned here seem like a lot of potential for manual error, and thus seem inadequate.

I can take my own hash as a task during compilation, but the benefit of using git is that .gitignore already describes all that needs to be excluded from factoring into a hash of any kind; it describes the boundaries of the project quite safely. Further to that, if the project is committed after data had been created, the git hash from before the commit will still work in those cases where no additional code change will have been made.

Is there an orderly way to get a git hash of a project, or something similarly useful, without a commit?

Upvotes: 2

Views: 1559

Answers (4)

jthill
jthill

Reputation: 60235

Sure. For what you've got staged it's

git write-tree

which spits the hash of the currently-indexed content and structure, no timestamps or message dependencies at all.

For an already-committed tree it's

git rev-parse $commit^{tree}

edit: To directly retrieve the state as of a tree id, do

git read-tree -um HEAD $treeid

and you can hunt up commits of that content using

git log --all --pretty=format:"%T Commit %H %d %S" \
| grep "^$treeid"

Upvotes: 1

gran_profaci
gran_profaci

Reputation: 8463

As Jonathan pointed, you need info like the time of get the commit hash.

However, you could always do this to get a sample commit hash:

Add all your files : git add .

Make a commit : git commit

Do whatever book-keeping stuff you want to do with the commit hash :

git rev-parse HEAD

Get rid of the commit as if it never existed :

git reset --soft HEAD^

So you're back in the original place before the git add. This would probably be the cleanest and best way to do it. And you also now have an approximate commit hash.

Upvotes: 1

rfliam
rfliam

Reputation: 198

We use git describe --always --dirty for this purpose. If the working tree contains uncommitted changes it will append "-dirty". If the current commit has an annotated tag that will be used instead of the hash.

In general our build scripts (Makefiles) generate a version.h that has this information in it. The version.h is in the .gitignore and never committed.

Upvotes: 2

Jonathan Wakely
Jonathan Wakely

Reputation: 171253

So is there a way to get a hash of the code, without a commit?

No, because the time of the commit is part of the hash.

Upvotes: 3

Related Questions