git log show one commit id only

GitGit CommitGit Log

Git Problem Overview


I need some help. It is possible to only show one commit id? Since git log -3 show the log from 1 - 3, I just want to show only 3. What possible command will match for it?

I use the command

       git log -3 --pretty=format:"%h"

the result is

       ffbef87
       cf0e073
       1c76c5d

I only want to display the 1c76c5d only.

Git Solutions


Solution 1 - Git

You can use git show referencing the third parent from your current commit (i.e. the second ancestor from HEAD). Also, git show accepts the same format string as git log:

git show HEAD~2 --pretty=format:"%h" --no-patch
Update (2016-12-01)

An even better way would be to use the rev-parse plumbing command with the --short option to output the abbreviated (7 characters) commit SHA-1:

git rev-parse --short HEAD~2

Or you could also specify the exact length of the commit SHA-1:

git rev-parse --short=4 HEAD~2

Solution 2 - Git

There is a tool for that:

git log -3 --pretty=format:"%h" | tail -n 1

You can include n characters of the hash (instead of the default) with the following flag:

--abbrev=n 
Relevant pieces of the Unix Philosophy

> 1) Make each program do one thing well. To do a new job, build afresh rather than complicate old programs by adding new "features".

> 2) Expect the output of every program to become the input to another, as yet unknown, program. Don't clutter output with extraneous information. Avoid stringently columnar or binary input formats. Don't insist on interactive input.

> ... > [i.e.] >

  • Write programs that do one thing and do it well.
  • Write programs to work together.

https://en.wikipedia.org/wiki/Unix_philosophy

Solution 3 - Git

Since at least git version 2.3.8, you can use the --skip option:

   git log -1 --skip 2 --pretty=format:"%h"

Not sure which earlier versions of git support --skip.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionrkevx21View Question on Stackoverflow
Solution 1 - GitEnrico CampidoglioView Answer on Stackoverflow
Solution 2 - GitDylanYoungView Answer on Stackoverflow
Solution 3 - GitAndyView Answer on Stackoverflow