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.
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”.
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.
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 .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With