git log show one commit id only
Asked Answered
P

3

111

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.

Patriapatriarch answered 16/7, 2015 at 7:56 Comment(0)
C
118

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
Calton answered 16/7, 2015 at 8:8 Comment(12)
when I use git show head~3 --pretty=format:"%h" --no-patch, it says fatal: ambiguous argument 'HEAD~3': unknown revision or path not in the working tree. Use '--' to separate paths from revisions, like this: 'git <command> [<revision>...] -- [<file>...]'Patriapatriarch
Could it be that head~2 is the first commit in your history?Calton
To reference the commit in your question, you need to use head~2.Calton
@EnricoCampidoglio why --no-patch is usedNino
@KasunSiyambalapitiya Because git show also outputs the diff associated to the commit (i.e. the patch) but we're not interested in that in this case.Calton
Anyone know what the --no-patch part does?Jinks
@GitSyncApp --no-patch suppresses the diff output of git show.Calton
Is it costly to call git show? I need to iterate over a commit log. The other option is to use "--pretty=format" and insert an end char. And then split the log output based on the end char.Jinks
@GitSyncApp What do you want to print out for each commit? The commit SHA-1, metadata, patch or a combination of those?Calton
I want to print aprox 200x of: Sha1, Data, Author, Subject, Body. But i supress the patch.Jinks
@EnricoCampidoglio Iterating over 20x commits took 1.5 sec. Using Log and splitting the output should be much faster. Or maybe I need to do every git show call in one terminal call? Hmm thought git was pretty optimised?!?Jinks
I had to do git rev-parse --short HEAD~1 not 2 to get the latest merged master commit id.Element
M
32

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

Mcnair answered 10/11, 2017 at 18:46 Comment(0)
V
14

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.

Virginiavirginie answered 12/7, 2018 at 18:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.