git log
这是Git中最常用的命令,比如输入 git log时,输出以下内容:
commit 5508fde9083465ff2dded7c005cc404bbdebae12
Author: hmx <417688867@qq.com>
Date: Mon Mar 27 10:18:57 2017 +0800
add new file
看到日期格式很不爽,那么我们怎么改成熟悉的格式呢,直接上代码。
git config --global alias.logd 'log --pretty=format:"%C(yellow)commit %H %n%CresetAuthor: %an <%ae> %nDate: %ai %n%n %s %n%n"'
然后输入 git logd,这时控制台打印出以下信息了:
commit 5508fde9083465ff2dded7c005cc404bbdebae12
Author: hmx <417688867@qq.com>
Date: 2017-03-27 10:18:57 +0800
add new file
发现日期变了,变的熟悉了,那么我们解析以上的代码以及参考以下的参数,来更改自己想要的格式。
alias:别名,这里我们起了个别名,输入命令时 git 跟着我们所起的别名就可以了
pretty:使用其他格式显示历史提交信,可选项有:oneline,short,medium,full,fuller,email,raw以及format:<string>,默认为medium,如:
1. --pretty=oneline:一行显示,只显示哈希值和提交说明(--online本身也可以作为单独的属性)
2. --pretty=format:” ":控制显示的记录格式,如:
- %H 提交对象(commit)的完整哈希字串
- %h 提交对象的简短哈希字串
- %T 树对象(tree)的完整哈希字串
- %t 树对象的简短哈希字串
- %P 父对象(parent)的完整哈希字串
- %p 父对象的简短哈希字串
- %an 作者(author)的名字
- %ae 作者的电子邮件地址
- %ad 作者修订日期(可以用 -date= 选项定制格式)
- %ar 作者修订日期,按多久以前的方式显示
- %cn 提交者(committer)的名字
- 作者和提交者的区别不知道是啥?
- 作者与提交者的关系:作者是程序的修改者,提交者是代码提交人(自己的修改不提交是怎么能让别人拉下来再提交的?)
- 其实作者指的是实际作出修改的人,提交者指的是最后将此工作成果提交到仓库的人。所以,当你为某个项目发布补丁,然后某个核心成员将你的补丁并入项目时,你就是作者,而那个核心成员就是提交者(soga)
- %ce 提交者的电子邮件地址
- %cd 提交日期(可以用 -date= 选项定制格式)
- %cr 提交日期,按多久以前的方式显示
- %s 提交说明
- 带颜色的--pretty=format:” ",这个另外写出来分析
- 以这句为例:%Cred%h%Creset -%C(yellow)%d%Cblue %s %Cgreen(%cd) %C(bold blue)<%an>
- 它的效果是:
- 先断句:[%Cred%h][%Creset -][%C(yellow)%d ][%Cblue%s][%Cgreen(%cd)][%C(bold blue)<%an>]
- 然后就是很明显能得到的规律了
- 一个颜色+一个内容
- 颜色以%C开头,后边接几种颜色,还可以设置字体,如果要设置字体的话,要一块加个括号
- 能设置的颜色值包括:reset(默认的灰色),normal, black, red, green, yellow, blue, magenta, cyan, white.
- 字体属性则有bold, dim, ul, blink, reverse.
- 内容可以是占位元字符,也可以是直接显示的普通字符
- --date= (relative|local|default|iso|rfc|short|raw):定制后边如果出现%ad或%cd时的日期格式
- 有几个默认选项
- --date=relative:shows dates relative to the current time, e.g. "2 hours ago".
- --date=local:shows timestamps in user’s local timezone.
- --date=iso (or --date=iso8601):shows timestamps in ISO 8601 format.
- --date=rfc (or --date=rfc2822):shows timestamps in RFC 2822 format,often found in E-mail messages.
- --date=short:shows only date but not time, in YYYY-MM-DD format.这个挺好用
- --date=raw:shows the date in the internal raw git format %s %z format.
- --date=default:shows timestamps in the original timezone (either committer’s or author’s).
- 也可以自定义格式(需要git版本2.6.0以上),比如--date=format:'%Y-%m-%d %H:%M:%S' 会格式化成:2016-01-13 11:32:13,其他的格式化占位符如下:
- 有几个默认选项
# 下面参数定义列表
'%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).