Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the nth commit since the first commit?

Tags:

git

I can find out how many commits there are with this:

git rev-list HEAD --count

Let's say that returns 123 commits.

How can I find the nth commit out of 123? Note that I am not asking for the nth commit before HEAD. I would like to know the nth commit after the very first commit.

like image 996
kraftydevil Avatar asked Jun 16 '14 08:06

kraftydevil


People also ask

How can I see the code of a previous commit?

To view the previous commits, use the git log –-oneline command. This provides the commit details.

How do I get the last commit hash?

# open the git config editor $ git config --global --edit # in the alias section, add ... [alias] lastcommit = rev-parse HEAD ... From here on, use git lastcommit to show the last commit's hash.

How do I get the latest commit?

Viewing a list of the latest commits. If you want to see what's happened recently in your project, you can use git log . This command will output a list of the latest commits in chronological order, with the latest commit first.

How do you see the details of a commit?

`git log` command is used to view the commit history and display the necessary information of the git repository. This command displays the latest git commits information in chronological order, and the last commit will be displayed first.


1 Answers

This could be considered ugly but I could not think of a better way

$git log --skip=N --max-count=1

This will show exactly 1 commit, counting back from HEAD by N. To use this you need to provide a number for N though. N is calculated with

N = total-commits - desired-commit-nr

Say git rev-list HEAD --count returns 10 and you want to view the 3rd commit

$git log --skip=7 --max-count=1

We use 7 because

 7 = 10 - 3
total ^   ^ the commit we want
like image 174
Tim Avatar answered Nov 15 '22 22:11

Tim