zoukankan      html  css  js  c++  java
  • git-定制属于你的log格式

    软件版本:
     

       操作系统:ubuntu10.04
        内核版本:Linux version 2.6.32-36-generic
        git 版本:git version 1.7.0.4

    1. 查看提交历史

        命令:git log

    2. 选项 --pretty[=<format>]
    1)介绍
        除了默认格式查看提交历史以外,还可以通过 --pretty[=<format>] 或者 --format[=<format>] 制定输出格式。<format> 可以是 noeline, short, medium, full, fuller, email, raw, format: 中的一个,他们各自的输出格式可以参考 git log --help 或者(3)。接下来我们主要是讨论 format: 的使用方法。

    2)定制提交格式 --pretty=format:
        --pretty=format:"format" 的用法就想 printf 的用法一样,只是换行从 变为 %n 。下面列出一些常用的占位符,分别来自于(1)(2),更详细请参考(3)或者 git log --help
    选项       说明
    
    %H   提交对象(commit)的完整哈希字串
    %h   提交对象的简短哈希字串
    %T   树对象(tree)的完整哈希字串
    %t   树对象的简短哈希字串
    %P   父对象(parent)的完整哈希字串
    %p   父对象的简短哈希字串
    %an  作者(author)的名字
    %_ae  作者的电子邮件地址 (由于新浪博客显示问题,请去除
    %_ae 中的 _ )
    %_ad  作者修订日期(可以用 -date= 选项定制格式)(由于新浪博客显示问题,请去除 % ad 中的 _ )
    %ar  作者修订日期,按多久以前的方式显示
    %cn  提交者(committer)的名字
    %_ce 提交者的电子邮件地址
    (由于新浪博客显示问题,请去除 %_ce 中的 _ )
    %_cd  提交日期 (由于新浪博客显示问题,请去除 %_cd 中的 _ )
    %cr  提交日期,按多久以前的方式显示
    %d:  ref名称

    %s:  提交的信息标题
    %b:  提交的信息内容
    %Cred: 切换到红色
    %Cgreen: 切换到绿色
    %_Cblue: 切换到蓝色 (由于新浪博客显示问题,请去除 %_Cblue 中的 _)
    %Creset: 重设颜色
    %C(...): 制定颜色, as described in color.branch.* config option
    %n:  换行

        注意:作者(author)_和_提交者(committer)_之间究竟有何差别,其实作者指的是实际作出修改的人,提交者指的是最后将此工作成果提交到仓库的人。所以,当你为某个项目发布补丁,然后某个核心成员将你的补丁并入项目时,你就是作者,而那个核心成员就是提交者。引用来自(1)

        例如,我们只需要输出完成的哈希值以及提交的信息标题,就可以使用命令:

    $git log --pretty=format:"%H %n %s"

    38896a27dc8ab4f47883bd67d300f6145f0478b7
     commit write 2
    7d3e94cf22b17f7d097479ae8d9367e42509bb3f
     Initial commit

    3)定制专属于你的 log 格式

        根据上面的说明,我们可以定制属于自己 log 格式:

    $git config --global alias.lg "log --graph --pretty=format:'%Cred%H%Creset @%C(yellow)%d%Creset %n Author: %cn <%_ce> %n Date: %_cd %_Cblue(%cr)%Creset %n %n Commit subject: %Cgreen%s%Creset %n'"
        注:命令中 %_ce、%_cd、%_Cblue 分别去除其下划线 _ 。
    $git lg

    * 38896a27dc8ab4f47883bd67d300f6145f0478b7 @ (HEAD, master)
    |  Author: Eddy <xxx@xxx.com>
    |  Date: Thu Feb 2 17:06:06 2012 +0800 (8 days ago)

    |  Commit subject: commit write 2

    * 7d3e94cf22b17f7d097479ae8d9367e42509bb3f @
       Author: Eddy <xxx@xxx.com>
       Date: Thu Feb 2 17:05:00 2012 +0800 (8 days ago)
      
       Commit subject: Initial commit

    4)其它选项
        git log 支持的选项有很多,详细请参考 git log --help ,常用的有(引用(1)中的表2-2):

    选项   说明
    -p     按补丁格式显示每个更新之间的差异。
    --stat 显示每次更新的文件修改统计信息。
    --shortstat 只显示 --stat 中最后的行数修改添加移除统计。
    --name-only 仅在提交信息后显示已修改的文件清单。
    --name-status 显示新增、修改、删除的文件清单。
    --abbrev-commit 仅显示 SHA-1 的前几个字符,而非所有的 40 个字符。
    --relative-date 使用较短的相对时间显示(比如,“2 weeks ago”)。
    --graph 显示 ASCII 图形表示的分支合并历史。
    --pretty 使用其他格式显示历史提交信息。可用的选项包括 oneline,short,full,fuller 和 format(后跟指定格式)。

    参考资料:
    (1) http://progit.org/book/zh/ch2-3.html
    (2) http://ruby-china.org/topics/939
    (3) http://linux.die.net/man/1/git-show
  • 相关阅读:
    49. 字母异位词分组
    73. 矩阵置零
    Razor语法问题(foreach里面嵌套if)
    多线程问题
    Get json formatted string from web by sending HttpWebRequest and then deserialize it to get needed data
    How to execute tons of tasks parallelly with TPL method?
    How to sort the dictionary by the value field
    How to customize the console applicaton
    What is the difference for delete/truncate/drop
    How to call C/C++ sytle function from C# solution?
  • 原文地址:https://www.cnblogs.com/kubimiantiao/p/4226661.html
Copyright © 2011-2022 走看看