Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Know git hash before committing?

Tags:

Is there any way to know the hash of the commit before committing?

like image 751
Marcelo Liberato Avatar asked Jan 08 '13 04:01

Marcelo Liberato


People also ask

How do I see commit hash?

Looking up changes for a specific commit If you have the hash for a commit, you can use the git show command to display the changes for that single commit. The output is identical to each individual commit when using git log -p .

How do I get previous commit hash?

To pull up a list of your commits and their associated hashes, you can run the git log command. To checkout a previous commit, you will use the Git checkout command followed by the commit hash you retrieved from your Git log.

How does git determine commit hash?

The commit hash by hashing the data you see with cat-file . This includes the tree object hash and commit information like author, time, commit message, and the parent commit hash if it's not the first commit.

Does git use SHA-1 or sha256?

At its core, the Git version control system is a content addressable filesystem. It uses the SHA-1 hash function to name content.


1 Answers

What possible reason do you have for needing this? If you were thinking of putting the hash of the commit into its own commit message, I'm sorry to tell you but that's impossible (or at least, impossible without breaking SHA1). The commit message is one of the pieces that are used when generating the hash, so any attempt to modify the message would change the hash.

In any case, finding out the hash of the commit before committing is nearly indistinguishable from actually committing, writing down the hash, and then throwing away the commit (as Carl Norum suggested in his comment). The reason is that the hash is generated by making the commit object and passing it through SHA1. So in order to find the hash without committing, you'd have to basically walk through the commit process manually and SHA1 the results, without actually writing the object to disk. And not only is that rather impractical, but it's also completely pointless.

like image 78
Lily Ballard Avatar answered Sep 18 '22 01:09

Lily Ballard