An alias for git's --pretty option string format

我是研究僧i 提交于 2019-12-12 20:34:20

问题


I know I can set an alias for git log invocation with lots of parameters.

For example, I can use this:

git config --global alias.lg "log --graph --pretty=format:'%h -%d %s (%cr) <%an>'"

to alias git log --graph --pretty=format:'%h -%d %s (%cr) <%an>' with much shorter git lg.

Is it possible to set an alias for a --pretty=format: string alone? So that I can type

git log --pretty=my_aliased_format


回答1:


The git log documentation has this to say about the --pretty[=<format>] and --format=<format> options (the --format one has a required <format> name while --pretty has an optional one). This text is buried fairly far in, under the section PRETTY FORMATS:

There are several built-in formats, and you can define additional formats by setting a pretty.<name> config option to either another format name, or a format: string, as described below (see git- config(1)). ...

Hence:

$ git config pretty.foo 'bar %H baz'              # you might want --global here
$ git log --format=foo | head -3
bar b5101f929789889c2e536d915698f58d5c5c6b7a baz
bar a562a119833b7202d5c9b9069d1abb40c1f9b59a baz
bar 7fa92ba40abbe4236226e7d91e664bbeab8c43f2 baz

Simply write your own format from the directives listed in that section, give it a name, and put that name into your configuration (local or global) and then --format=<name> will access it.

It's more typical and conventional to set lg as an alias, as in your earlier example, but this works well too.



来源:https://stackoverflow.com/questions/54643839/an-alias-for-gits-pretty-option-string-format

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!