I am currently getting my commit message for a certain commit hash by using this below:
hash='b55da97'
git log --pretty=oneline ${hash} | grep "${hash}" | awk '{ print $2 }'
These seems extremely inefficient though. Is there a smarter or cheaper way to do this, or am I stuck with grepping and awking?
The description is better to be up to 72 characters. Preslav Rachev in his article explains the reason for the 50/72 rule. The ideal size of a git commit summary is around 50 characters in length. Analyzing the average length of commit messages in the Linux kernel suggests this number.
A commit in git always has a hash that contains 40 characters. But to make the id:s easier to handle it also supports using a short version of the id. The short commit id can actually be any number of characters as long as it's unique for a commit within the same repo.
git log
takes (among other things):
-n num
to limit the number of commits shown: choose 1 (and if num
is 9 or less you can just write -num
, hence, -1
, for short)--pretty=format:string with directives
to change the log output format. The %s
directive gets the commit "subject", which is what you also get with oneline
.Hence: git log -n 1 --pretty=format:%s $hash
(or git log -1 --pretty=format:%s
) will do the trick here.
For a complete list of format directives, see the git log documentation, under "PRETTY FORMATS" (about halfway down).
Depending on how much of the commit message you actually want, there are several pretty-format specifiers that you can use:
· %s: subject
· %f: sanitized subject line, suitable for a filename
· %b: body
· %B: raw body (unwrapped subject and body)
So something like git log -1 --pretty=format:%b <hash>
, or use one of the other specifiers (I think %s
is probably closer to what you are doing now). The -1
limits git log
to just the one commit, rather than walking the history tree.
I like having the important stuff dumped into a single line... Here's what I use, built off other answers on this page:
IT=$(git log -1 --pretty=format:"%an, %s, %b, %ai" $*)
echo "$IT"
jdoe, WORK1766032 - Added templating engine, WIP, 2013-08-15 14:25:59 +0000
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