How to git log in reverse order?
Asked Answered
C

7

258

I recently learned that I can get hg log to print the history in reverse order with:

hg log -r :

So of course I tried:

git log -r :

Well, it didn't work. So what is the command to do the same thing in git?

Clive answered 9/5, 2010 at 19:13 Comment(3)
man git-log grep reverse.Fractocumulus
man git-log /reverse and press enter.Mariquilla
For clarity, according to the docs -r in GIT translates to: "Show recursive diffs."Mantoman
L
476

Use the --reverse option:

git log --reverse
Laith answered 9/5, 2010 at 19:16 Comment(4)
Note that e.g. git log -10 --reverse would get 10 last commits then reverse list.Timepiece
You could create a git alias: #2554286Laith
This also works (currently) for git show, even though the documentation doesn't mention it. E.g. git show origin/master.. --reverseHyla
I use git gol for git log --reverseRaimundo
E
16

You don't need to type --reverse all the time, nor do you need a bash function. You can just create a git alias. Open up your favorite text editor and open up your global .gitconfig file. It's usually found in your home directory.

Navigate to or create a section like this:

[alias]
    lg = log -10 --reverse

That creates a git alias that grabs the ten most recent commits then reverses that list so the most recent of those 10 is at the bottom. Now you can simply run:

git lg

Electrometallurgy answered 12/3, 2014 at 16:9 Comment(0)
A
13

Jakub Narębski's comment ("Note that e.g. git log -10 --reverse would get 10 last commits then reverse list") has been clarified in Git 2.11 (Q4 2016):

See commit 04be694 (27 Sep 2016) by Pranit Bauva (pranitbauva1997).
(Merged by Junio C Hamano -- gitster -- in commit 54a9f14, 11 Oct 2016)

rev-list-options: clarify the usage of --reverse

Users often wonder if the oldest or the newest n commits are shown by log -n --reverse.
Clarify that --reverse kicks in only after deciding which commits are to be shown to unconfuse them.

See Commit Limiting.

Amplexicaul answered 12/10, 2016 at 11:44 Comment(2)
But what if you want the reverse before the -n? I can't seem to figure it out. (aka the first commit)Driskill
It might be a bit inneficient but this worked for me git log --reverse --format=%cd | head -n 1Driskill
S
3

I combined few of suggested one into one and I created an alias.

git log -10 --pretty=oneline --abbrev-commit --reverse
alias gl='git log -10 --pretty=oneline --abbrev-commit --reverse'
Smarm answered 7/11, 2019 at 11:14 Comment(1)
reverse is brokenOpuscule
C
3

If you want a git --graph with reversed order, you can't make use of --reverse unfortunately, but you can make use of tac:

git log --graph --color | tac

Note that --color is important here.

As a git alias:

git config --global alias.logr '!git log --graph --color | tac'

(Then of course add your favorite flags to git log --graph ;)

Chicane answered 31/5, 2022 at 10:17 Comment(0)
O
1

None of above work... except this one with recent commit message + stats

git log --graph --stat

More snippet ~/.gitconfig:

lg1 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all
lg2 = log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n''          %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all
lg = !"git lg1"
lg3 = log -10 --reverse --abbrev-commit
Opuscule answered 16/9, 2021 at 8:37 Comment(1)
When using --graph you can't get results in reverse order out of the box, as git doesn't allow --reverse on top of --graph. See my answer for a solutionChicane
E
0

You could create a bashrc function (assuming you are on a unixy os)

function git_logr {

    git log --reverse

}
Emogene answered 24/2, 2014 at 20:27 Comment(1)
Easier approach would be to add an alias for something this simple: git config --global alias.logr 'log --reverse' Invoke using: git logr <additional arguments>Tortfeasor

© 2022 - 2024 — McMap. All rights reserved.