Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to get the SHA of a commit from its message?

Tags:

git

When doing a git tag, I'm not always great at remembering if HEAD~6 (for example) is inclusive or exclusive.

Given that most of my commits are prefixed with an issue number, I wondered if there is some magic command for searching for the commit SHA from part of its message.

I know it's easy to do a git log and work from there, but I want more easy :)

EDIT : Someone also asked the opposite question: In Git, is there a way to get the “friendly” name for an arbitrary commit?

like image 880
Benjol Avatar asked Jun 09 '10 06:06

Benjol


People also ask

What is the SHA for the commit message?

"SHA" stands for Simple Hashing Algorithm. The checksum is the result of combining all the changes in the commit and feeding them to an algorithm that generates these 40-character strings. A checksum uniquely identifies a commit.

What is a commit hash?

The commit hash is an SHA-1 hash made up of a few properties from the commit itself. As mentioned above, it is a lot more complex than this post can go into, but understanding the fundamentals is a great first step. The git hash is made up of the following: The commit message. The file changes.


1 Answers

You can use :/blah syntax to specify a commit whose commit message starts with the given text.

E.g.

git show ":/Issue #299"

This is valid anywhere a commit can be used. E.g.

git cherry-pick ":/Issue #299"

If you actually need the SHA-1 of the commit you can just use rev-parse.

git rev-parse ":/Issue #299"

See the "SPECIFYING REVISIONS" section of the git rev-parse man page:

A colon, followed by a slash, followed by a text:
This names a commit whose commit message starts with the specified text.
This name returns the youngest matching commit which is reachable from any ref.
If the commit message starts with a !, you have to repeat that; the special sequence :/!, followed by something else than ! is reserved for now.

like image 54
CB Bailey Avatar answered Oct 22 '22 11:10

CB Bailey