19

In Git, there are numerous ways to refer to a commit, including the full SHA hash or a shortened form of the hash (say, the first 6 characters or so). You can also name commits using a "friendly" syntax, like HEAD, HEAD^, HEAD^^, HEAD~3, and so on.

Given an arbitrary commit in SHA hash form, is there a tool in Git to find a "friendly" name for said commit?

If I use git-show-branch, I get a list of revisions in "friendly" form, so I feel like there must be a way...I just can't find a tool to do it.

mipadi
  • 398,885
  • 90
  • 523
  • 479
  • Just for completeness' sake, here is the opposite question: http://stackoverflow.com/questions/3003554/is-there-any-way-to-get-the-sha-of-a-commit-from-its-message – Benjol Sep 09 '10 at 05:14

2 Answers2

19

You can use "git name-rev" to get the form you are asking about. One problem with that form is that, being relative to a branch, it isn't a permanent name. So an alternative is "git describe" which produces an alternative friendly name based on how far ahead of a tag a given commit is.

For example:

srh@devo16:~/src/git <master>$ git name-rev 3cd7388
3cd7388 master~2

But then after I do a "git pull", master~2 could mean something else. By contrast:

srh@devo16:~/src/git <master>$ git describe 3cd7388
v1.6.3.1-153-g3cd7388

Now "v1.6.3.1-153-g3cd7388" is a permanent name. Of course, it's still a bit long (although you can shorten the hash bit on the end by specifying "--abbrev=4" for example) but it communicates that 3cd7388 is 153 changes after version 1.6.3.1.

araqnid
  • 127,052
  • 24
  • 157
  • 134
  • 1
    Thanks, both techniques are useful. I was more interested in the behavior of git-name-rev, because my specific use-case was that I was using git-log --grep to find a commit with a specific message, and then I wanted to be able to refer to it as a parameter to git-revert without having to remember a hash. – mipadi May 27 '09 at 18:12
  • 2
    In that case, note the "--stdin" option to git name-rev : you should be able to pipe git log output straight into it. – araqnid May 27 '09 at 18:14
  • 1
    Instead of having to remember git-name-rev, you can use "git describe --contains", and/or use --decorate option to git-log – Jakub Narębski May 27 '09 at 19:16
4

Try git describe:

$ git describe --all --contains 90de2680dc54c0d600b0694bd175bd09357a8dba
master~2
Joe Beda
  • 2,743
  • 1
  • 19
  • 16