diy1818

Good children, Constantly learning, Constantly growing.

git config

git多颜色输出

git默认的输出是单一颜色的,不仅不够美观,也不容易阅读。实际上,git本身就支持用多种颜色来显示其输出的信息,只需在命令行中运行以下命令来修改git的设置,即可开启多颜色输出:

git config --global color.status auto
    git config --global color.diff auto
    git config --global color.branch auto
    git config --global color.interactive auto

执行以上命令后,git的status, diff和branch等诸命令的输出就都是带有颜色的了。见下图示例。

git-status

自定义log格式

完成上述步骤后,git log 命令的输出虽然有了点颜色,但还是显得枯燥(见下图)。

git-default-output

不要紧,强大的git提供了自定义log格式的功能,尝试输入以下命令:

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

你将看到类似下图的输出:

git-pretty-log

怎么样,不赖吧?不过,每次查看log都输出这么一长串的命令,实在是不太现实。咱们来通过git的命令别名来解决这个问题。输入以下命令:

git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'"

上述命令将创建一个命令别名 lg,每次你使用命令 git lg 就相当于输入了刚才那一长串命令。现在,如果想看美观的多颜色输出,就使用 git lg,如果想看普通的log输出,就使用 git log,二者互不干扰。

如果你想让log输出某些特定的信息,可以自己调整 --pretty 参数的值,例如下面的命令将只显示commit的hash,提交时间,提交者姓名:

git log --pretty=format:'%h %ar %an'

把format后面单引号中的内容替换为你想要的格式,即可实现自定义的log输出格式。这里的%h, %ar等是一些git预定义的占位符,完整的列表如下:

%Hcommit hash
%hcommit的短hash
%Ttree hash
%ttree的短hash
%Pparent hashes
%pparent的短hashes
%an作者名字
%aNmailmap中对应的作者名字 (.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 格式
%dref名称
%eencoding
%scommit信息标题
%f过滤commit信息的标题使之可以作为文件名
%bcommit信息内容
%Ncommit notes
%gDreflog selector, e.g., refs/stash@{1}
%gdshortened reflog selector, e.g., stash@{1}
%gsreflog subject
%Cred切换到红色
%Cgreen切换到绿色
%Cblue切换到蓝色
%Creset重设颜色
%C(…)制定颜色, as described in color.branch.* config option
%mleft, right or boundary mark
%n换行
%%a raw %
%x00print a byte from a hex code
%w([<w>[,<i1>[,<i2>]]])switch line wrapping, like the -w option of git-shortlog(1).