483

The git-reflog command doesn't by default show a date alongside each entry, which strikes me as a strange oversight; I think this would be very helpful.

Are there any command-line options, or other tweaks, which can be employed to cause it to show when each reflog entry was added? The manpage isn't forthcoming...

0

8 Answers 8

674

Per the man page, you can use git log options, e.g.,

git reflog --pretty=short

git reflog --date=iso
5
  • 18
    This replaces the head number (or whatever the correct term is) with the date. Can you have both? Commented Jul 9, 2014 at 10:23
  • 16
    @Marco it seems you would have to use a custom format: git reflog --format='%C(auto)%h %<|(17)%gd %C(blue)%ci%C(reset) %s'. I have added an alias for this: github.com/blueyed/dotfiles/commit/…
    – blueyed
    Commented Nov 23, 2014 at 12:13
  • 4
    @blueyed Not quite the same - the --date=iso in the reflog command shows when that reflog entry was created, not the time of the commit. Still appreciate your alias, as I've used it to make a prettier reflog. Commented Jul 31, 2019 at 23:04
  • 3
    You can combine both: git reflog --date=iso --pretty=short
    – Herz3h
    Commented Sep 11, 2020 at 12:55
  • None of these comments attempting to answer "can you have both" actually give both. @blueyed's comment gives the reflog selector in index format HEAD@{2} along with the /commit/ date. And @Herz3h's comment gives the reflog selector in timestamp format HEAD@{2024-02-09 15:28:42 -0800} but omits the index format. What I want is the reflog selector in both timestamp format and index format.
    – jbyler
    Commented Feb 10 at 0:24
48

You can use the --walk-reflogs variant of git log:

git log -g

This is rather verbose by default, and prints the date among other things. You can format it with the standard --pretty= flag.

You can also use the reflog command directly with the --pretty= flag to format the output.

git reflog --pretty='%cd %h %gd %gs'

In the format above, %cd shows the commit date to the left of the normal reflog output.

4
  • 15
    %cd, shows the date of the commit the reflog points to, unfortunately, which is not what I (or the OP) is after: we want the date of the reflog entry.
    – Thanatos
    Commented Apr 21, 2014 at 23:12
  • 12
    git log --walk-reflogs --date=iso just made my day Commented Sep 13, 2016 at 17:41
  • 1
    can git reflog --pretty='%cd %h %gd %gs' be colorful?
    – alper
    Commented Nov 5, 2020 at 10:53
  • Perfection itself! thank you! Commented Oct 18, 2022 at 19:02
24

You can use a custom format, e.g.,

git reflog --format='%C(auto)%h %<|(20)%gd %C(blue)%cr%C(reset) %gs (%s)'

In the above format, %h is the commit hash, %cr is the relative committer date, %gs is the reflog subject, and, %s is the commit subject. Look at the git-log docs for other possible placeholders. For instance, using %ci instead of %cr will show absolute commit dates.

You can save this in your ~/.gitconfig using a custom pretty format and refer to it via an alias:

[alias]
    rl = reflog --pretty=reflog
[pretty]
    reflog = %C(auto)%h %<|(20)%gd %C(blue)%cr%C(reset) %gs (%s)
3
  • 1
    The problem with this is that %gd doesn't show the same date as @{now} does. When searching the reflog, knowing the exact time can be really important ("I know it was in the right state at 8:57" for example).
    – ErikE
    Commented Jul 21, 2015 at 19:50
  • 2
    @ErikE - Just change the cr to ci to get the full timestamp of each action: git reflog --format='%C(auto)%h %<|(20)%gd %C(blue)%ci%C(reset) %gs (%s)'
    – n1k31t4
    Commented Oct 9, 2019 at 8:50
  • instead of (20)%gd ; (17)%gd seems like better alignment
    – alper
    Commented Nov 5, 2020 at 11:06
17

enter image description here

If you just want to see the dates relative to the current time, you can use the following command:

git reflog --date=relative

To save some typing, you can create a git alias for it (rl - short for reflog):

git config --global alias.rl "reflog --date=relative"

And use the alias instead, like so (the -10 at the end is to show the last 10 actions):

git rl -10
5
  • How can we show the head numbers like HEAD@{16} for all ?
    – alper
    Commented May 25, 2022 at 14:14
  • @alper Just use git reflog for that format.
    – Wenfang Du
    Commented May 25, 2022 at 14:22
  • I want to have git reflog format while showing it as sorted like with (--date=relative) flag
    – alper
    Commented May 25, 2022 at 14:24
  • @alper I'd like to know too :).
    – Wenfang Du
    Commented May 26, 2022 at 2:23
  • I asked it as a question (stackoverflow.com/questions/72389262/…) , feel free to edit :-)
    – alper
    Commented May 26, 2022 at 8:57
12

Tell git in what format, either counted reflog entries or timed reflog entries, i.e.

git reflog @{now}    
git reflog @{0}
2
  • The programmer in me doesn't like the "natural language" dates inside the {}, but happily this technique also works with --date=iso.
    – mwfearnley
    Commented Feb 1, 2019 at 20:59
  • 1
    Yes but I want to see BOTH formats in the same output. I'm trying to create a pretty format that gives BOTH the "index format" and the "timestamp format" of the reflog selector. Maybe that's not possible.
    – jbyler
    Commented Feb 10 at 0:26
6

Note git 2.10 (Q3 2016) improves the documentation about date with git reflog.

See commit 642833d, commit 1a2a1e8 (27 Jul 2016), and commit d38c7b2, commit 522259d, commit 83c9f95, commit 2b68222 (22 Jul 2016) by Jeff King (peff).
Helped-by: Jeff King (peff).
(Merged by Junio C Hamano -- gitster -- in commit 0d32799, 08 Aug 2016)

The rev-list options is updated:

The reflog designator in the output may be shown as ref@{Nth} (where Nth is the reverse-chronological index in the reflog) or as ref@{timestamp} (with the timestamp for that entry), depending on a few rules.

It includes: - an update about --date=raw:

shows the date as seconds since the epoch (1970-01-01 00:00:00 UTC), followed by a space, and then the timezone as an offset from UTC (a + or - with four digits; the first two are hours, and the second two are minutes).
I.e., as if the timestamp were formatted with strftime("%s %z")).
Note that the -local option does not affect the seconds-since-epoch value (which is always measured in UTC), but does switch the accompanying timezone value.

And a new option: --date=unix

shows the date as a Unix epoch timestamp (seconds since 1970).
As with --raw, this is always in UTC and therefore -local has no effect.

5

Format the git reflog output as the glol with ohmyzsh:

git reflog --pretty='%Cred%h%Creset -%C(auto)%d%Creset %gs %Cgreen(%cr) %C(bold blue)<%an>%Creset'

Format the git reflog output as the glod with ohmyzsh:

git reflog --pretty='%Cred%h%Creset -%C(auto)%d%Creset %gs %Cgreen(%ad) %C(bold blue)<%an>%Creset'

I made a custom plugin git-mnz for ohmyzsh with these two aliases grlol, grlod and some other aliases and functions.

** Sorry if didn't sent a PR to the git plugin, but there are hundreds PR waiting to be merged..

3

git reflog --pretty=medium

... but they make it really nasty to find out (there's no bash completion for git-log options, the log man page doesn't document the placeholder strings for various pretty built-in profiles, plus there are still some differences from the default git-log format, e.g. you'd have to pass --abbrev)

Not the answer you're looking for? Browse other questions tagged or ask your own question.