The shortest possible output from git log containing author and date

前端 未结 13 1662
我在风中等你
我在风中等你 2020-12-02 03:12

How can I show a git log output with (at least) this information:

* author
* commit date
* change

I want it compressed to one line per log

相关标签:
13条回答
  • 2020-12-02 03:55

    I use these two .gitconfig settings:

    [log]
      date = relative
    [format]
      pretty = format:%h %Cblue%ad%Creset %ae %Cgreen%s%Creset
    

    %ad is the author date, which can be overidden by --date or the option specified in the [log] stanza in .gitconfig. I like the relative date because it gives an immediate feeling of when stuff was comitted. Output looks like this:

    6c3e1a2 2 hours ago you@me.com lsof is a dependency now.
    0754f18 11 hours ago you@me.com Properly unmount, so detaching works.
    336a3ac 13 hours ago you@me.com Show ami registration command if auto register fails
    be2ad45 17 hours ago you@me.com Fixes #6. Sao Paolo region is included as well.
    5aed68e 17 hours ago you@me.com Shorten while loops
    

    This is all of course in color, so it is easy to distinguish the various parts of a log line. Also it is the default when typing git log because of the [format] section.

    2014 UPDATE: Since git now supports padding I have a nice amendment to the version above:

    pretty = format:%C(yellow)%h %Cblue%>(12)%ad %Cgreen%<(7)%aN%Cred%d %Creset%s
    

    This right aligns the relative dates and left aligns committer names, meaning you get a column-like look that is easy on the eyes.

    Screenshot

      

    2016 UPDATE: Since GPG commit signing is becoming a thing, I thought I'd update this post with a version that includes signature verification (in the screenshot it's the magenta letter right after the commit). A short explanation of the flag:

    %G?: show "G" for a good (valid) signature, "B" for a bad signature, "U" for a good signature with unknown validity and "N" for no signature

    Other changes include:

    • colors are now removed if the output is to something other than the tty (which is useful for grepping etc.)
    • git log -g now contains the reflog selector.
    • Save 2 parens on refnames and put them at the end (to preserve column alignment)
    • Truncate relative dates if they are too long (e.g. 3 years, 4..)
    • Truncate commiter names (might be a little short for some ppl, just change the %<(7,trunc) or check out the git .mailmap feature to shorten commiter names)

    Here's the config:

    pretty = format:%C(auto,yellow)%h%C(auto,magenta)% G? %C(auto,blue)%>(12,trunc)%ad %C(auto,green)%<(7,trunc)%aN%C(auto,reset)%s%C(auto,red)% gD% D
    

    All in all column alignment is now preserved a lot better at the expense of some (hopefully) useless characters. Feel free to edit if you have any improvements, I'd love to make the message color depend on whether a commit is signed, but it doesn't seem like that is possible atm.

    Screenshot

    0 讨论(0)
  • 2020-12-02 03:57

    Note the -10 at the end, to show only the last 10 entries.

    Use predefined git alias (hs - short for history):

    git hs
    

    Created once by command:

    git config --global alias.hs "log --pretty='%C(yellow)%h %C(cyan)%ad %Cblue%an%C(auto)%d %Creset%s' --date=relative --date-order --graph"
    

    %h = abbreviated commit hash
    %ad = author date (format respects --date= option, so you can adjust it later)
    %an = author name
    %d = ref names
    %s = subject

    Reference: https://git-scm.com/docs/git-log#_pretty_formats

    0 讨论(0)
  • 2020-12-02 03:58
    git log --pretty=format:"%H %an %ad"
    

    use --date= to set a date format

    git log --pretty=format:"%H %an %ad" --date=short
    
    0 讨论(0)
  • 2020-12-02 04:03

    Use predefined git alias, i.e.:

    $ git work
    

    Created once by command:

    $ git config --global alias.work 'log --pretty=format:"%h%x09%an%x09%ad%x09%s"'
    

    https://git-scm.com/book/tr/v2/Git-Basics-Git-Aliases

    Or more colored with graph:

    $ git config --global alias.work 'log --pretty=format:"%C(yellow)%h %ar %C(auto)%d %Creset %s , %Cblue%cn" --graph --all'
    

    0 讨论(0)
  • 2020-12-02 04:03

    Run this in project folder:

    $ git log --pretty=format:"%C(yellow)%h %ar %C(auto)%d %Creset %s , %Cblue%cn" --graph --all
    

    And if you like, add this line to your ~/.gitconfig:

    [alias]
        ...
        list = log --pretty=format:\"%C(yellow)%h %ar %C(auto)%d %Creset %s, %Cblue%cn\" --graph --all
    
    0 讨论(0)
  • 2020-12-02 04:04

    To show the commits I have staged that are ready to push I do

    git log remotes/trunk~4..HEAD --pretty=format:"%C(yellow)%h%C(white) %ad %aN%x09%d%x09%s" --date=short | awk -F'\t' '{gsub(/[, ]/,"",$2);gsub(/HEAD/, "\033[1;36mH\033[00m",$2);gsub(/master/, "\033[1;32mm\033[00m",$2);gsub(/trunk/, "\033[1;31mt\033[00m",$2);print $1 "\t" gensub(/([\(\)])/, "\033[0;33m\\1\033[00m","g",$2) $3}' | less -eiFRXS
    

    The output looks something like:

    ef87da7 2013-01-17 haslers      (Hm)Fix NPE in Frobble
    8f6d80f 2013-01-17 haslers      Refactor Frobble
    815813b 2013-01-17 haslers      (t)Add Wibble to Frobble
    3616373 2013-01-17 haslers      Add Foo to Frobble
    3b5ccf0 2013-01-17 haslers      Add Bar to Frobble
    a1db9ef 2013-01-17 haslers      Add Frobble Widget
    

    Where the first column appears in yellow, and the 'H' 'm' and 't' in parentesis show the HEAD, master and trunk and appear in their usual "--decorate" colors

    Here it is with line breaks so you can see what it's doing:

    git log remotes/trunk~4..HEAD --date=short
        --pretty=format:"%C(yellow)%h%C(white) %ad %aN%x09%d%x09%s"
        | awk -F'\t' '{
             gsub(/[, ]/,"",$2);
             gsub(/HEAD/, "\033[1;36mH\033[00m",$2);
             gsub(/master/, "\033[1;32mm\033[00m",$2);
             gsub(/trunk/, "\033[1;31mt\033[00m",$2);
             print $1 "\t" gensub(/([\(\)])/, "\033[0;33m\\1\033[00m","g",$2) $3}'
    

    I have aliased to "staged" with:

    git config alias.staged '!git log remotes/trunk~4..HEAD --date=short --pretty=format:"%C(yellow)%h%C(white) %ad %aN%x09%d%x09%s" | awk -F"\t" "{gsub(/[, ]/,\"\",\$2);gsub(/HEAD/, \"\033[1;36mH\033[00m\",\$2);gsub(/master/, \"\033[1;32mm\033[00m\",\$2);gsub(/trunk/, \"\033[1;31mt\033[00m\",\$2);print \$1 \"\t\" gensub(/([\(\)])/, \"\033[0;33m\\\\\1\033[00m\",\"g\",\$2) \$3}"'
    

    (Is there an easier way to escape that? it was a bit tricky to work out what needed escaping)

    0 讨论(0)
提交回复
热议问题