Git Log issue: date is not showing when oneline option in used
Asked Answered
F

4

11

I see lots of great answers in Pretty git branch graphs that show the oneline option for git log being used with dates. However, when I run these commands my output is different, I don't see the dates. The options --oneline and --date=<relative or iso> seem to be incompatible.

Result for git log --date=iso:

enter image description here

Result for git log --date=iso --oneline:

enter image description here

I tried this on three different computers with similar results.

Update:

The linked question asks "How to commit date". Perhaps they mean "How to show the commit date", but as it stands the question is not clear. Also, in the possible duplicate the OP asks about several options simultaneously. I am specifically asking how to show or print dates (author date or commit date) when using the oneline option. The other OP is also concerned with the size of the commit history, which is also outside the scope of my question.

Perhaps the other question could be edited to match this question, but it didn't come up when I searched for this issue (although I didn't use the git-log tag specifically, because I didn't notice that tag until I was searching for appropriate tags for my question).

Also, I know of no other effective way to show the results of printed formatting without using screenshots, especially because they have color. For what it's worth, the linked question also uses screenshots.

Fistic answered 2/1, 2019 at 17:21 Comment(4)
The --oneline format does not select any date formatting options, so the --date setting for those format options has no effect. Use a different format, that has a date-formatting option in it. (Note: --oneline is exactly the same as --pretty=oneline --abbrev-commit.)Sectarianism
Possible duplicate of How to commit date in "git log --all --graph --oneline --simplify-by-decoration"Grayson
stackoverflow.com/search?q=%5Bgit-log%5D+oneline+dateGrayson
After carefully researching and working out the question, I realized that I actually wanted reflog, which is fine for my purposes with a simple git reflog --date=iso... dohFistic
H
13

Should add the date in the format.

For instance:

git log --pretty=format:"%h %s %an %ad" --date=relative

Where %ad means "author date" using --date option value

Hardcastle answered 2/1, 2019 at 17:27 Comment(3)
Loses the (HEAD -> master) notation, and the colors.Dyslalia
If you add %d you'll get the ref decoration back (eg (HEAD -> main). Prefix it with %C(auto) to have the decorations in color, making them much easier to spot when scanning pages of history.Dijon
You can replace %ad" --date=relative with %ar".Dijon
E
11

This one will add the date in "YYYY-MM-DD" format, in blue, between the commit hash and the description:

git log --pretty=format:'%C(auto)%h%C(blue) %<|(19)%as%C(auto)%d %s'

You can make it an alias gl with

alias gl="git log --pretty=format:'%C(auto)%h%C(blue) %<|(19)%as%C(auto)%d %s'"

enter image description here

I added the %<|(19) which instructs the next format operator (%as which stands for YYYY-MM-DD) to occupy all space up to the 19th column in the terminal, and be left-aligned in that space. You don't need this for my example, because %as==YYYY-MM-DD has a fixed-width anyway. But you may want to try alternative date formats like %ar, %ad, or %aD. For more information on using custom formatting, check man git-log and search for PRETTY FORMATS.

Elongation answered 4/12, 2021 at 5:34 Comment(1)
Nice! Colors improve the read ability muchDelossantos
T
3

You indeed cannot use these two options together. You could, however, emulate this behavior by explicitly stating the format:

$ git log --format='%h (%ai) %s'
Terreverte answered 2/1, 2019 at 17:26 Comment(2)
Loses the (HEAD -> master) notation, and the colors.Dyslalia
@Dyslalia See my answer. It has colors and decorators exactly the same as --onelineNowise
N
3
git log --pretty=format:'%C(auto)%h%d %s %aN %ad'

That has the same formatting (colors and information) plus adds the Author name and date (%aN %ad) to the end unformatted (you can move them wherever you like). I spent over an hour trying to figure out how to get %d (refs like branches, tags, etc.) to show up with different colors depending on remote, local, type, etc., but it simply needed %C(auto) in front of it all to copy the formatting that --oneline uses. Since author and date aren't in --oneline, they're just the normal text color for your terminal. You can add whatever separators and coloring you want. See https://www.git-scm.com/docs/git-log#_pretty_formats for a reference to all the formats you can use.

You can add --date=xxx to specify date format.

Nowise answered 2/7, 2020 at 16:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.