Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Git, is there a way to get the "friendly" name for an arbitrary commit?

Tags:

git

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.

like image 553
mipadi Avatar asked May 27 '09 17:05

mipadi


People also ask

What is the Sha of a commit?

The long string following the word commit is called the commit hash. It's unique identifier generated by Git. Every commit has one, and I'll show you what they're used for shortly. Note: The “commit hash” is sometimes called a Git commit “reference” or “SHA”.

What are the different ways you can refer to a commit?

1) commits are referred as relative to one another. For instance HEAD~1 would refer to the commit parent of HEAD. 2) refspecs can be used to refer commits that reside on remote branches using local Git environment. 3) Every tags created in repository would become a ref, which Git would maintain certain commits alias's.

What is ref name in git?

A ref is an indirect way of referring to a commit. You can think of it as a user-friendly alias for a commit hash. This is Git's internal mechanism of representing branches and tags. Refs are stored as normal text files in the .git/refs directory, where .git is usually called .git .


1 Answers

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.

like image 60
araqnid Avatar answered Sep 21 '22 02:09

araqnid