【问题标题】:A git log history graph with one line per commit, in color, with dates一个 git 日志历史图表,每次提交一行,颜色,日期
【发布时间】:2011-09-15 11:33:03
【问题描述】:

我需要有如下格式:

git log --decorate --graph --oneline --date-order

但我也需要它:

  1. 包含日期(短)
  2. 拥有相同的颜色

我试过了:

git log --decorate --graph --oneline --date-order \
--date=short --pretty=format:"%h %ad %s"

但它更难阅读(没有颜色)并且不包括分支/标签


最接近的简单解决方案是(感谢VonC):

git log --graph --pretty=format:'%C(yellow)%h%Creset \
-%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' \
--abbrev-commit --date=short

【问题讨论】:

  • 您可以使用 VonC 的示例,但对于 refspec 使用 %C(auto)%d 来自动着色参考
  • 将答案附加到一个人的问题中是令人困惑的。通过接受正确的答案更好地正确使用堆栈溢出。

标签: git git-log


【解决方案1】:

你可以试试:

alias.lgb=log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=short --branches

它有不同的颜色,但你可以很容易地改变它们。

例如:

git log --graph --pretty=format:'%Cred%h -%d %s (%cr) <%an>%Creset' --abbrev-commit --date=short --branches

【讨论】:

  • 注意:使用%ad 而不是%an 来取回你的短日期。
  • 这看起来很不错,除了分支没有不同的颜色,具体取决于它们是本地还是远程,并且标签的颜色与分支相同。区分这些非常有用。 HEAD 也是浅蓝色 - 显示您当前的位置
  • @NickSoft:关于标签和分支的颜色问题,请参阅stackoverflow.com/questions/5889878/color-in-git-log/…
  • 他们说这是不可能的,这对我来说不太适用。使用 --oneline 而不是自定义格式时有没有办法添加日期?
  • @NickSoft:这似乎不可能,至少据我所知。
【解决方案2】:

嗯,“不可能”意味着没有简单的方法,我必须自己做。我太担心我总是让事情变得艰难,而实际上有更简单的方法。

这是一个 bash+php 脚本。我尝试使用 sed 来实现,但失败了。

我将此脚本命名为 git-gd 并将其放在路径 /usr/local/bin/ 中的 bin 目录中,并将其与 git 一起使用:git gdgit gd &lt;options&gt;

#!/bin/bash

GIT="/usr/local/bin/git"
PHP="/bin/php"
GIT_DATES="$GIT log --date=short --abbrev-commit --pretty=format:%C(yellow)%h_%C(green)[%ad]%Creset --branches --color=always $*"
#if you have have alias g
GIT_GRAPH="$GIT g --color=always"
#or
#GIT_GRAPH="$GIT log --decorate --graph --oneline --date-order --color=always"
PHP_SCRIPT='
  $exps = explode("\n", $_SERVER["argv"][1]);
  $lines = file("php://stdin");
  $s = array();
  $r=$s;
  foreach($exps as $exp){
   $exp = trim($exp);
   list($commit,)=explode("_", $exp);
   $s[] = $commit;
   $r[] = str_replace("_", " ", $exp);
  }
  foreach($lines as $line){
    $line = str_replace($s, $r, $line);
    echo $line ;
  }
  '

DATES=`$GIT_DATES`
$GIT_GRAPH $* |$PHP -r "$PHP_SCRIPT" -- "$DATES"

我会等待更简单的解决方案,我会接受我自己的答案

【讨论】:

    【解决方案3】:

    在非古代版本的 git 中,您可以配置 git log 以启用装饰:

    git config --global log.decorate full
    

    【讨论】:

    • log.decorate=full 导致打印引用名称及其前缀(refs/heads/ 等);我发现log.decorate=short 更有用。
    【解决方案4】:

    此外,在您的 git 配置中,您可以像这样添加两行:

    [format]
      pretty = %Cblue%h%Creset %Cgreen[%ar]%Creset (%an) %s 
    

    这只是意味着你可以输入 git log 并且它总是会被格式化。

    【讨论】:

      【解决方案5】:

      整齐地记录您的提交并格式化使用

      git log --pretty=format:"%h - %an, %ar : %s"  
      
      • 查看分支上的提交

      【讨论】:

        猜你喜欢
        • 2011-08-10
        • 1970-01-01
        • 2013-11-27
        • 1970-01-01
        • 1970-01-01
        • 2015-01-13
        • 1970-01-01
        • 2011-06-05
        • 2011-04-18
        相关资源
        最近更新 更多