How to log first 10 commits in Git
Asked Answered
O

10

172

Two questions:

  1. How to show the first 10 commit in git from beginning to end. (no branch)
  2. How the specify the commit index and log it. (show the second or third)

I know that git use parent to link the commit, it's easy to log the commit from end to start. like: git log HEAD~10

But i need to query from the start to end, is it possible?

Oust answered 27/4, 2012 at 5:25 Comment(2)
Related: How to show first commit by 'git log'?Salinasalinas
scroll to @kostix answerImpedimenta
S
74

Simply log everything with one line format and tail the output:

git log  --pretty=oneline | tail -n 10 
Salinasalinas answered 27/4, 2012 at 7:4 Comment(7)
Great! That's what am looking for. Tks @CharlesB.Oust
Or, use git rev-list HEAD | tail -n 10 to get the 10 IDs to list, which you can then show in whatever format you like (not limited to the one-line method).Bolt
tail seems to lose the nice coloring that I get in my git console. Passing a parameter like git log --oneline -10 as in the other answer works as I expected.Invalidism
This is clearly the hard-core Unix way, lol. Please see @Hamrick answer.Nikolas
The downside of this method is that i can only see my pushed commits.Benyamin
git log --pretty=oneline -10 will show with colorsHexone
git log --oneline -1Tonina
H
354
git log -10

Would show 10 latest commits matching the revision spec (a missing spec means "all commits").

See manpage:

git help log

section Commit Limiting

-<number>, -n <number>, --max-count=<number>
    Limit the number of commits to output.
Hamrick answered 27/4, 2012 at 8:27 Comment(7)
simple, and doesn't depend on tail being available on your platform.Overshoot
& doesn't mess with git formatting/coloring. This should be the accepted answer x100Adcock
Not what the op wanted: he wanted the first 10, not the latest.Dosimeter
@Timo, quite possibly yes, but the question as stated is open for interpretation, and a lot of peple seem to end up upvoting my answer just because of these open differences. So while I technically agree with you, let's keep it as is, I suppose ;-) Among the technically correct suggestions, I like the most the torek's comment to the accepted answer.Hamrick
@Hamrick "How to show the first 10 commit in git from beginning to end" isn't "open to interpretation" as far as I'm concerned. It's obviously asking for the first 10 commits, not the latest.Tetradymite
@BobbyJack, the number of upvotes disproves your point. I mean, I tried to explain that this is a social issue. You did not get it. Fine, let's get over it.Hamrick
@Hamrick I accept that many people have interpreted the question incorrectly — only the OP can explain what they actually meant, of course! However, I think it's very clear when they say " it's easy to log the commit from end to start — git log HEAD~10 — but i need to query from the start to end" that they want the oldest 10 commits, not the newest.Tetradymite
S
74

Simply log everything with one line format and tail the output:

git log  --pretty=oneline | tail -n 10 
Salinasalinas answered 27/4, 2012 at 7:4 Comment(7)
Great! That's what am looking for. Tks @CharlesB.Oust
Or, use git rev-list HEAD | tail -n 10 to get the 10 IDs to list, which you can then show in whatever format you like (not limited to the one-line method).Bolt
tail seems to lose the nice coloring that I get in my git console. Passing a parameter like git log --oneline -10 as in the other answer works as I expected.Invalidism
This is clearly the hard-core Unix way, lol. Please see @Hamrick answer.Nikolas
The downside of this method is that i can only see my pushed commits.Benyamin
git log --pretty=oneline -10 will show with colorsHexone
git log --oneline -1Tonina
M
43

Here my approach,

To get first 10 commits:

git log -n 10

-n is number

Additional To get next 10 commit skip first 10 :

git log --skip=10 -n 10
Milden answered 18/12, 2017 at 9:15 Comment(3)
I think this should be the acceppted answer. Using tail will cause --graph to stop working in git logMcclean
This must be the accepted answer. works like a charm for meVacillating
you lose the font color on the console using tail as well. this is the right answerQuerulous
S
20

i would use below simple syntax command;

git log -10 --oneline
Subaxillary answered 11/5, 2020 at 5:7 Comment(2)
Think this is the most efficient way!Drud
Hm, but this prints 10 last not first commits.Drucill
B
18

To get the last 10 commits:

git log HEAD~10..HEAD

To get them in oldest-to-newest order:

git log --reverse HEAD~10..HEAD

Note that if there are merges, this may show more than 10 commits; add --first-parent if you only want to traverse through the first parent of each branch.

For far more detail, see the documentation for git rev-list.


Edit: You've already gotten a useful answer above to "show commits near the start of history" (again, see the caveats about multiple non-connected commit DAGs in a repo). But you can also do, e.g.:
git log --no-walk `git rev-list HEAD | tail -n 10`

and:

git log --no-walk `git rev-list --reverse HEAD | head -n 10`

depending on which order you want the results.

Bolt answered 27/4, 2012 at 6:14 Comment(4)
Tks for your reply. But i don't need the last 10 commits but the first. To be specific, if the commit is A-B-C-D-E-F-G(HEAD). How to log the A-B-C and how to log C by the index 2?Oust
Oh! There's no guarantee that there is a single "first commit", but if there is, see the link @Salinasalinas added.Bolt
The way can be work. any natural way? git log --pretty=oneline | wc -l git log HEAD~<The number minus one or index>Oust
Thanks for the answer. This is what I needed to avoid '-10' style options in my git aliases. that accept number arguments.Censure
O
18

the best result comes with combination of both best answers:

git log --pretty=oneline -10
Osteen answered 19/3, 2019 at 2:0 Comment(1)
Thanks I use this as a git alias to quickly display the recent commits without filling my terminal. I called the alias lkj = log --pretty=oneline --abbrev-commit -10. Now I realize it doesn't answer the OP's question but I came here looking for the simple number switch.Clipfed
F
5

Simply log everything reverse -1 means list one log

git log  --reverse -1
Ferrocyanide answered 22/11, 2012 at 9:18 Comment(0)
A
2

Because... more detail :p

  1. How to show the first 10 commit in git from beginning to end. (no branch)
  2. How the specify the commit index and log it. (show the second or third)

By (no branch), you might be asking about the reflog rather than any given ancestry chain. The following has nothing to do with the branch you are on.

git log -g --pretty=oneline | tail -10

<sha> HEAD@{###}: action: summary (old)
<sha> HEAD@{###}: action: summary (older)
...
<sha> HEAD@{###}: action: summary (oldest)
  • -g is --walk-reflogs Instead of walking the commit ancestry chain, walk reflog entries.q
  • add |cut -d ' ' -f 2|tr -d ':' > log to log only the reflog commit index.

The following will show the earliest ancestors of the currently checked out branch.

git log --reverse --pretty=oneline | head -10 | cat -n

1 <sha> summary (oldest)
2 <sha> summary (second)
  • --reverse Output the commits in reverse order.
  • Can't use simply -n 10 or -10 since it breaks --reverse
  • cat -n adds line numbers (commit index?)

Antietam answered 17/4, 2014 at 19:57 Comment(0)
A
2

Try this neat output format:

git log --date=short -10 --pretty="%C(Yellow)%h %x09 %C(reset)%ad %x09 %C(Cyan)%an: %C(reset)%s"

It will print short lines with colors, like: enter image description here

Agrapha answered 4/2, 2021 at 16:55 Comment(0)
S
0

In case someone wants more than just git one-line log:

git log --reverse | awk 'NR>1 {print last} {last=$0}; /^commit/ && ++c==11{exit}'

where the 11 at the end should be set to 1 more than the number of commits you want.

As here points out git log --reverse -n 10 doesn't work as you need it to. (I suppose it would need to be non-commutative to give you the ability to chose the first 10 commits in reverse order or the last 10 commits 🤪)

Spelldown answered 3/7, 2019 at 5:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.