Git 如何保存预设的 git log --format

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1441156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 03:51:22  来源:igfitidea点击:

Git how to save a preset git log --format

gitformattinglogging

提问by Jesper R?nn-Jensen

I really like the short git log format where I can see author, date and change description like this:

我真的很喜欢简短的 git log 格式,在那里我可以看到作者、日期和更改描述,如下所示:

git log --pretty=format:"%h%x09%an%x09%ad%x09%s"

Which outputs:

哪些输出:

  fbc3503 mads    Thu Dec 4 07:43:27 2008 +0000   show mobile if phone is null...   
  ec36490 jesper  Wed Nov 26 05:41:37 2008 +0000  Cleanup after [942]: Using timezon
  ae62afd tobias  Tue Nov 25 21:42:55 2008 +0000  Fixed #67 by adding time zone supp
  164be7e mads    Tue Nov 25 19:56:43 2008 +0000  fixed tests, and a 'unending appoi

(from stackoverflow question "link text")

(来自 stackoverflow 问题“链接文本”)

Now, the question is, how do I save this as a new format on my machine so I only have to write something like, for instance:

现在,问题是,如何在我的机器上将其另存为新格式,以便我只需要编写如下内容,例如:

git log --format=jespers_favourite

采纳答案by VonC

Considering the git log manual page mentions:

考虑到 git log 手册页提到:

--pretty[=<format>]
--format[=<format>]

Pretty-print the contents of the commit logs in a given format, where can be one of oneline, short, medium, full, fuller, email, raw and format:. When omitted, the format defaults to medium.

以给定的格式漂亮地打印提交日志的内容,其中可以是 oneline、short、medium、full、fuller、email、raw 和 format:。省略时,格式默认为中等。

the <format>can only have predefined values.
That only leaves you the possibility to define an aliasas a shortcut for that command.

<format>只能有预定义值。
那只会让您有可能将别名定义为该命令的快捷方式。

[alias]
        jespers_favourite = log --pretty=format:"%h%x09%an%x09%ad%x09%s"

or

或者

[alias]
        compactlog = log --pretty=format:"%h%x09%an%x09%ad%x09%s"

回答by tdbit

In newer versions of Git (confirmed with v1.7.8) it is possible to set named pretty-print log formatsusing git config pretty.named_format. These can be set at a machine-wide, user or file levelwith the <file-option>argument.

Git中的新版本(带v1.7.8确认),可以设置一个名为漂亮地打印日志格式使用git config pretty.named_format。这些可以使用参数在机器范围、用户或文件级别进行设置<file-option>

To create a log format called jespers_favouriteor the whole machine use --system

要创建称为jespers_favourite或整机使用的日志格式--system

git config --system pretty.jespers_favourite "%h%x09%an%x09%ad%x09%s"

For single user use '--global'

对于单用户使用“--global”

git config --global pretty.jespers_favourite "%h%x09%an%x09%ad%x09%s"

Leaving the <file-option>argument blank will default to setting the config file of the current repository, .git/configunless defined otherwise.

离开<file-option>参数留空将默认设置当前存储库的配置文件,.git/config除非另有定义。

回答by Marc D

You can configure the default pretty format using git-config. From the git-config documentation:

您可以使用 git-config 配置默认的漂亮格式。从 git-config 文档:

 format.pretty
           The default pretty format for log/show/whatchanged command, See git-log(1), git-show(1), git-whatchanged(1).

For example:

例如:

git config --add format.pretty fuller

git config --add format.pretty fuller

or original poster's desired format:

或原始海报所需的格式:

git config --add format.pretty "%h%x09%an%x09%ad%x09%s"

git config --add format.pretty "%h%x09%an%x09%ad%x09%s"

Like with other git config settings, format.pretty may be set at the global, system, or repository scope (default).

与其他 git 配置设置一样,format.pretty 可以设置在全局、系统或存储库范围(默认)。