Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get second last commit hash in git

Tags:

git

I am using below git command to get last 2 commit hash

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

But I needs only second last commit hash.

Any help would be great

Thanks

like image 718
Android Team Avatar asked Sep 01 '15 05:09

Android Team


People also ask

How do I get previous commit hash?

To pull up a list of your commits and their associated hashes, you can run the git log command. To checkout a previous commit, you will use the Git checkout command followed by the commit hash you retrieved from your Git log.

How do I view commit hash?

If you have the hash for a commit, you can use the git show command to display the changes for that single commit. The output is identical to each individual commit when using git log -p .

Can two commits have the same hash?

Hashes are what enable Git to share data efficiently between repositories. If two files are the same, their hashes are guaranteed to be the same. Similarly, if two commits contain the same files and have the same ancestors, their hashes will be the same as well.


2 Answers

git rev-parse @~

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

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

like image 198
amalloy Avatar answered Oct 03 '22 20:10

amalloy


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

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

Follow this link for more info on git log

like image 40
Ashwani Avatar answered Oct 03 '22 20:10

Ashwani