git git日志格式化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7430093/
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 06:00:42  来源:igfitidea点击:

git log formatting

gitformatlogging

提问by NickSoft

I need to have format like:

我需要有这样的格式:

git log --decorate --graph --oneline --date-order

but I need it also:

但我也需要它:

  1. to contain date (short)
  2. to have the same colors
  1. 包含日期(短)
  2. 有相同的颜色

I tried:

我试过:

git log --decorate --graph --oneline --date-order \
--date=short --pretty=format:"%h %ad %s"

but it's harder to read (no colors) and does not include branches/tags

但它更难阅读(没有颜色)并且不包括分支/标签



The closest simple solution is(thanks VonC):

最接近的简单解决方案是(感谢VonC):

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

采纳答案by VonC

You can try:

你可以试试:

alias.lgb=log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=short --branches

It has different color, but you can change them easily.

它有不同的颜色,但您可以轻松更改它们。

for instance:

例如:

git log --graph --pretty=format:'%Cred%h -%d %s (%cr) <%an>%Creset' --abbrev-commit --date=short --branches

回答by NickSoft

Well, "impossible" means that there is no easy way and I'll have to do it myself. I worried too much that I always make things the hard way, when there is actually easier way.

嗯,“不可能”意味着没有简单的方法,我必须自己做。我太担心我总是让事情变得困难,而实际上有更简单的方法。

Here is a bash+php script. I tried to make it with sed, but I failed.

这是一个 bash+php 脚本。我试图用 sed 制作它,但我失败了。

I named this script git-gd and put it in a bin directory that's in path /usr/local/bin/and I use it with git: git gdor git gd <options>

我将此脚本命名为 git-gd 并将其放在路径中的 bin 目录中,/usr/local/bin/然后将其与 git 一起使用:git gdgit gd <options>

#!/bin/bash

GIT="/usr/local/bin/git"
PHP="/bin/php"
GIT_DATES="$GIT log --date=short --abbrev-commit --pretty=format:%C(yellow)%h_%C(green)[%ad]%Creset --branches --color=always $*"
#if you have have alias g
GIT_GRAPH="$GIT g --color=always"
#or
#GIT_GRAPH="$GIT log --decorate --graph --oneline --date-order --color=always"
PHP_SCRIPT='
  $exps = explode("\n", $_SERVER["argv"][1]);
  $lines = file("php://stdin");
  $s = array();
  $r=$s;
  foreach($exps as $exp){
   $exp = trim($exp);
   list($commit,)=explode("_", $exp);
   $s[] = $commit;
   $r[] = str_replace("_", " ", $exp);
  }
  foreach($lines as $line){
    $line = str_replace($s, $r, $line);
    echo $line ;
  }
  '

DATES=`$GIT_DATES`
$GIT_GRAPH $* |$PHP -r "$PHP_SCRIPT" -- "$DATES"

I'll wait a bit for simpler solution and I'll accept my own answer

我会等待更简单的解决方案,我会接受我自己的答案

回答by Henrik Gustafsson

In non-ancient versions of git you can configure git logto enable decorations thusly:

在非古代版本的 git 中,您可以配置git log以启用装饰:

git config --global log.decorate full

回答by Chris

In addition in your git config you can add two lines like this:

此外,在您的 git 配置中,您可以添加如下两行:

[format]
  pretty = %Cblue%h%Creset %Cgreen[%ar]%Creset (%an) %s 

This just means you can type git log and it will always be formatted.

这只是意味着你可以输入 git log 并且它总是会被格式化。