分享 个性化你的 Git Log 的输出格式

hisea · January 25, 2012 · Last by zchrissirhcz-github replied at July 12, 2020 · 67851 hits

广告:http://hisea.me/p/git-log-output-formats

git 已经变成了很多程序员日常工具之一。
git log 是查看 git 历史的好工具,不过默认的格式并不是特别的直观。
很多时候想要更简便的输出更多或者更少的信息,这里列出几个 git log 的 format。
可以根据自己的需要定制。

git log 命令可一接受一个--pretty 选项,来确定输出的格式。
如果我们只想输出 hash.

git log --pretty=format:"%h" 

git 用各种 placeholder 来决定各种显示内容: 下面内容来自这里

  • %H: commit hash
  • %h: 缩短的 commit hash
  • %T: tree hash
  • %t: 缩短的 tree hash
  • %P: parent hashes
  • %p: 缩短的 parent hashes
  • %an: 作者名字
  • %aN: mailmap 的作者名字 (.mailmap 对应,详情参照git-shortlog(1)或者git-blame(1))
  • %ae: 作者邮箱
  • %aE: 作者邮箱 (.mailmap 对应,详情参照git-shortlog(1)或者git-blame(1))
  • %ad: 日期 (--date= 制定的格式)
  • %aD: 日期,RFC2822 格式
  • %ar: 日期,相对格式 (1 day ago)
  • %at: 日期,UNIX timestamp
  • %ai: 日期,ISO 8601 格式
  • %cn: 提交者名字
  • %cN: 提交者名字 (.mailmap 对应,详情参照git-shortlog(1)或者git-blame(1))
  • %ce: 提交者 email
  • %cE: 提交者 email (.mailmap 对应,详情参照git-shortlog(1)或者git-blame(1))
  • %cd: 提交日期 (--date= 制定的格式)
  • %cD: 提交日期,RFC2822 格式
  • %cr: 提交日期,相对格式 (1 day ago)
  • %ct: 提交日期,UNIX timestamp
  • %ci: 提交日期,ISO 8601 格式
  • %d: ref 名称
  • %e: encoding
  • %s: commit 信息标题
  • %f: sanitized subject line, suitable for a filename
  • %b: commit 信息内容
  • %N: commit notes
  • %gD: reflog selector, e.g., refs/stash@{1}
  • %gd: shortened reflog selector, e.g., stash@{1}
  • %gs: reflog subject
  • %Cred: 切换到红色
  • %Cgreen: 切换到绿色
  • %Cblue: 切换到蓝色
  • %Creset: 重设颜色
  • %C(...): 制定颜色,as described in color.branch.* config option
  • %m: left, right or boundary mark
  • %n: 换行
  • %%: a raw %
  • %x00: print a byte from a hex code
  • %w([[,[,]]]): switch line wrapping, like the -w option of git-shortlog(1).

除此之外, --graph 选项可以显示 branch 的 ascii 图例。

如果你自己定制了一个喜欢的输出方案,可以保存到 git config,或者设置 alias 以便日后使用。
~/.gitconfig 中加入:

[alias]
    lg = log --graph 

或者运行:

git config --global alias.lg "log --graph"

最后来一个别人分享的例子,稍微有些慢,但是可以看下 git log 定制效果,效果很酷。。

git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset' --abbrev-commit --date=relative

好文,前两天同事问查找某些 commit 内容包括某个特定字符的东西的时候不知道怎么弄,看了这个搞定,谢谢

# git shortlog --format='%H|%cn|%s' | grep '#2230'

哈哈,最近用 git 上瘾了。 前两天要比较两个文件夹的不同。以前会用 beyond compare 类工具。但当时我第一个想到的是 git diff

我的——

自从有了 tig... 就很少用 log 了...

#3 楼 @fredwu 除了树形,其他基本一致,话说你那个树是怎么搞的

git log --pretty=format:\"%h %ad | %s%d [%an]\" --graph --date=short 这个不错。做成 alias 就好用了

#4 楼 @poshboytl 自从用了 tig 已经完全不用 log 了。

#5 楼 @raecoo --graph 这个就可以显示树形

#7 楼 @lgn21st #4 楼 @poshboytl 是的,我也很喜欢 tig, 使用 log 大多数时候就是 pipe 到另外一个程序进行处理,搜索统计什么的。

#4 楼 @poshboytl 试验了下 tig,真不错

试用了一下,很赞

mark. tks!

不错

很好,正在用到的一个 shell 分享下。

#查看当前目录每个文件的最后提交者。
git ls-tree -r --name-only HEAD | while read filename; do
  echo "$(git log -1 --format="%an %ae" -- $filename) $filename"
done
ibugs in git cherry-pick 最佳实践 mention this topic. 13 Apr 14:30

format 建议换成 tformat,因为 format 是在行与行之间添加换行符,而 tformat 在每行末尾添加换行符。 假如需要统计行数,git hist | wc -l,如果 git hist 是 alias 用的 format 则比实际行数少一行,而 tformat 配置下,得到和实际行数相同的结果。 https://stackoverflow.com/questions/9007181/custom-log-format-omits-newline-at-end-of-output

You need to Sign in before reply, if you don't have an account, please Sign up first.