我喜欢以下命令打印 git 日志的方式:
git log --oneline --decorate --graph
每当我使用 git log 时,我想将其设置为默认格式。有没有办法编辑
~/.gitconfig
以默认启用oneline、decorate和graph?
是的,我知道我可以将这些选项别名为另一个 git 命令别名,但我宁愿默认情况下仅使用这些选项打印日志。
Git 允许您默认激活
--oneline
和 --decorate
,例如 log
、show
等:
git config --global format.pretty oneline
git config --global log.decorate short
但是,从 v2.1.0 v2.2.2 开始,Git 默认不允许您激活
--graph
。解决这个问题的一种方法(改编自这个超级用户答案)是在您的.<shell>rc
文件中定义以下函数:
git() {
if [ "$1" = "log" ]
then
command git log --graph "${@:2}";
else
command git "$@";
fi;
}
一个警告(由
他的评论中的
hvd
指出):如果您在git
和log
之间指定选项,如中所示
git -c log.showroot=false log -p
然后,因为第一个参数是
-c
而不是 log
,所以 --oneline --decorate --graph
标志将不会被使用。
显示完整提交哈希的
git config --global format.pretty oneline
的替代方案是定义您自己的格式。
以下将:
git config --global format.pretty "%C(auto)%h %d %s"
比较:
git config --global format.pretty oneline
输出:
59264eb68e610ef8edb99c364be9f2164d80ca14 (HEAD -> migration-service, origin/master, master) Merged PR 13: #52 Implement My library
f970b40b588cc9aae48f0e0953a538872f742843 Initial Commit
git config --global format.pretty oneline
输出:
59264eb (HEAD -> migration-service, origin/master, master) Merged PR 13: #52 Implement My library
f970b40 Initial Commit
有关格式字符串的更多信息,请参阅 Git dacs,网址为 https://git-scm.com/docs/pretty-formats