【问题标题】:How to configure 'git log' to show 'commit date'如何配置“git log”以显示“提交日期”
【发布时间】:2012-12-23 23:34:06
【问题描述】:

如何将git log 配置为显示commit date 而不是author date

【问题讨论】:

  • @Colleen 每个提交都有两个关联的日期 - AuthorDate 和 CommitDate(以git show --pretty=fuller HEAD 为例)。对于本地开发,这些通常是相同的,但对于通过电子邮件或其他机制添加的补丁,它们可能不同,其中 AuthorDate 是补丁的生成日期,而 CommitDate 是它实际应用于存储库的日期。

标签: git git-log


【解决方案1】:

pretty print 日期有几个选项。可能最简单的方法是只使用一种预烘焙的--pretty 格式,例如git log --pretty=fuller - 这将显示两个日期。如果您只想查看一个日期,但将其设为提交日期,您可以使用git log --format=<some stuff>。所有用于定义格式的allowable codes 都记录在git help log 中。提交日期是%cd%cD%cr%ct%ci 之一,具体取决于您喜欢的格式。

如果您想经常做某事,请将其放在别名中或编写辅助脚本以节省打字时间。

【讨论】:

    【解决方案2】:

    您可以使用--pretty=format 并使用%cr 相对于提交日期。

    例如:

    $ git log --graph --pretty=format:'%C(auto)%h%d (%cr) %cn <%ce> %s'
    

    您可以在 git 中定义一个别名以使其更易于使用。我的.gitconfig 中有以下内容:

    [alias]
    # see `git help log` for detailed help.
    #   %h: abbreviated commit hash
    #   %d: ref names, like the --decorate option of git-log(1)
    #   %cn: commiter name
    #   %ce: committer email
    #   %cr: committer date, relative
    #   %ci: committer date, ISO 8601-like format
    #   %an: author name
    #   %ae: author email
    #   %ar: author date, relative
    #   %ai: author date, ISO 8601-like format
    #   %s: subject
    # my awesome git log replacement
    lol  = log --graph --pretty=format:\"%C(auto)%h%d%Creset %C(cyan)(%cr)%Creset %C(green)%cn <%ce>%Creset %s\"
    # same as above, but ISO date
    lold = log --graph --pretty=format:\"%C(auto)%h%d%Creset %C(cyan)(%ci)%Creset %C(green)%cn <%ce>%Creset %s\"
    # using build-in standards
    lol2 = log --oneline --graph --decorate
    # shows branches and their last commits
    lol3 = log --all --graph --decorate --oneline --simplify-by-decoration
    

    在 Linux 或类似系统上,您可以使用单引号 ' 而不是双引号 "

    [alias]
    lol = log --graph --pretty=format:'%C(auto)%h%d%Creset %C(cyan)(%cr)%Creset %C(green)%cn <%ce>%Creset %s'
    

    有了这个,只需运行 git lol 或其他变体即可查看漂亮的输出。

    这是git lol --simplify-by-decoration的输出:

    • 看起来不错。 :)
    • lollog 更容易输入,而且听起来也更好。
      • 如果您需要,还可以访问常规的git log
    • 您的眼睛可以通过不同的颜色快速扫描内容。
    • 姓名和电子邮件对于有许多贡献者的大型项目/组织非常有用。
    • 对 hash/ref 使用默认着色,因为它已经很不错了。

    这是git lold 的输出,日期为 ISO 格式。有助于查看提交的确切日期/时间,并且能够轻松查看贡献者的时区。

    编辑 2020-06:添加了屏幕截图。更新为将%C(auto)(自动/默认着色)用于%h(提交哈希)和%d(参考名称)。除了电子邮件之外,还添加了%cn(提交者姓名)。

    【讨论】:

    • 我收到一个解析错误:git log --graph --pretty=format:\"%C(yellow)%h%Creset%C(cyan)%C(bold)%d%Creset %C(cyan)(%cr)%Creset %C(green)%ce%Creset %s\" bash: syntax error near unexpected token `('
    • @frakman1 — 您需要取消转义 "s 以使上述行在终端中运行
    • 更正行:git log --graph --pretty=format:"%C(yellow)%h%Creset%C(cyan)%C(bold)%d%Creset %C(cyan )(%cr)%Creset %C(green)%ce%Creset %s"
    【解决方案3】:

    我更喜欢这种格式,不包括作者姓名,包括实际提交日期。

    git log --graph --pretty=format:"%C(yellow)%h%x09%Creset%C(cyan)%C(bold)%ad%Creset  %C(green)%Creset %s" --date=short
    

    【讨论】:

    • '实际日期' 作者 创建了该提交内容的第一个版本。如果它已被重新设置基准或以其他方式重新提交,则您所看到的最终提交日期将在“%c”格式词干中找到。 '--short-date' 选项与 %ai 和 %ci 的 'iso' 日期格式输出同义
    【解决方案4】:

    可能对某人有用。我正在寻找带有作者姓名的日期和时间标记。

    git log --graph --pretty=format:"%C(yellow)%h%x09%Creset%C(cyan)%C(bold)%ad%Creset %C(yellow)%cn%Creset  %C(green)%Creset %s" --date=default
    

    【讨论】:

      猜你喜欢
      • 2015-04-02
      • 2021-04-29
      • 2019-12-28
      • 2015-10-05
      • 1970-01-01
      • 1970-01-01
      • 2015-01-13
      • 2015-10-23
      • 2017-11-25
      相关资源
      最近更新 更多