tags:

views:

69

answers:

3

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?

A: 

Try

git rev-list --grep='message excerpt' HEAD

This will only list commits that are ancestors of HEAD. If you wanted to search all commits in the repository (including those that are not ancestors of HEAD), I'm not sure how to do that other than by manually going through all the branch heads manually and running this command. (There's probably a way, I just don't know what it might be)

David Zaslavsky
+4  A: 
git log --grep=word

should help find the relevant commit.
(as opposed to searching for commit contents, where git log -S (pickaxe option) or git grep are more appropriate)

Add various format options like

git log --grep=word --pretty=online
git log --grep=word --pretty=format:"%h"

(the last one displaying only the SHA1), and you are good to go.


See also "Fun with git log --grep"

git log --grep=frotz --grep=nitfol --since=1.month

This finds the commits that happened within the last month and mentioned either frotz or nitfol in their commit log messages.
As with the usual "git grep", giving more than one patterns means "this or that".

The article points out that git grep is line oriented, and use the --all-match option in this git log --grep command.

The way "git log" family uses the grep mechanism is exactly for this "does the whole thing has a match?" aspect , not "show me the lines that match these criteria" aspect. It allows you to use this mechanism like so:

git log --all-match --grep=frotz --author=Linus

This will show commits that mention frotz and written by Linus.

VonC
I was looking for something that I could pipe/use directly in another command, so if you add `--pretty=format:"%h"` to that, I'll mark you as the answer ;)
Benjol
Adding --pretty=online to the command should improve readability even more.
Unknown
@Benjol: format options added.
VonC
Oops, sorry, I lied, @Charles Bailey got the tick :(
Benjol
+8  A: 

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.

Charles Bailey
What happens if the text is non-unique?
Benjol
@Benjol: IIRC, it finds the youngest reachable commit; you'll have to check the documentation.
Charles Bailey
Thanks, any idea of a keyword for looking that up `:/` doesn't work too great...
Benjol
git help rev-parse
Charles Bailey
http://www.kernel.org/pub/software/scm/git/docs/git-rev-parse.html
Charles Bailey
Excellent point on a revision specification I rarely use. +1
VonC
Nicely done. This was definitely new to me.
MikeSep