How to get second last commit hash code in git

I use the git command below to get the last 2 hash commits

git log -n 2 --pretty=format:"%H" #To get only hash value of commit 

But I only need the second last commit hash code.

Any help would be great

thanks

+7
source share
3 answers
 git rev-parse @~ 

rev-parse turns various notations into hashes, @ is the current chapter, and ~ is the previous commit.

This generalizes commits arbitrarily far back: for example, you can write @~3 (or @~~~ ) to indicate "three commits in front of the current head."

+12
source

Use skip attribute
--skip=<number> skips the number of commits before starting to show commit output.

 git log -n 1 --skip 1 --pretty=format:"%H" 

Follow this link for more info on git log

+7
source

You can simply pass the output of your command via tail :

 git log -n 2 --pretty=format:"%H" | tail -1 
+2
source

All Articles