如何使 git 日志输出仅在一行上显示日期和散列?

How to make git log output that just shows date and hash on one line?

我需要获取一组 github 的日期和提交结果,格式如下:

Date Commit
19 Mar 2015 b6959eafe0df6031485509c539e22eaf2780919c
1 Apr 2015 9a1f13339cc7d43930440c2350ea3060b72c8503
1 Apr 2015 1e76036421ca4d72c371182fc494af514911b099

我可以使用此命令获取日期:

git log | grep Date: | awk '{print   " "  " " }'

的提交
git log | grep commit | awk '{print }'

如何组合命令,以便在同一行中打印两个结果?

这是 git 日志的示例:

commit 1e76036421ca4d72c371182fc494af514911b099
Author: xxxx
Date:   Wed Apr 1 17:35:36 2015 +0200

    First web app commit

    First web app commit

commit 9a1f13339cc7d43930440c2350ea3060b72c8503
Author: xxxx
Date:   Wed Apr 1 17:28:42 2015 +0200

   change from app to website

commit b6959eafe0df6031485509c539e22eaf2780919c
Author: xxx
Date:   Thu Mar 19 18:58:33 2015 +0100

   First remote commit: hello world

    scheleton of the project now available

commit a41d419de46a59e72dbc52e5f39c9d8a8a9af72a
Author: xxxx
Date:   Thu Mar 19 17:31:16 2015 +0100

    Initial commit

您或多或少永远需要grep | awk,因为awk可以为您匹配。

所以grep Date: | awk '{print " " " " }'可以改写为awk '/Date:/ {print " " " " }'grep commit | awk '{print }'可以改写为awk '/commit/ {print }'.

这应该足以让您初步了解如何解决您的问题。

合并 awk 脚本。

git log | awk '
    # Print the header line (remove if you do not want it).
    BEGIN {print "Date Commit"}
    /^commit/ {
        # Save the current commit
        commit=
        # Skip to the next line
        next
    }
    /^Date:/ {
        # Print the fields from the current line and the saved commit
        print , , , commit
        # Clear the saved commit Just in Case
        commit=""
    }
'

据说您可以从 git log 本身获得 几乎 您想要的格式:

$ git log --format='%ad %H' --date=short
2015-03-19 b6959eafe0df6031485509c539e22eaf2780919c
2015-04-1 9a1f13339cc7d43930440c2350ea3060b72c8503
2015-04-1 1e76036421ca4d72c371182fc494af514911b099

如果需要,您可以使用 awk 将其转换为所需的输出。 (转换日期字段很简单,转换为月份名称需要更多 code/work。)

怎么样:

git log | awk '/^commit / { commit =  }
               /^Date: /  { printf "%s %s %s %s\n", , , , commit }'

捕获变量commit中的提交信息。当日期出现时,打印日期并提交信息。

grep 的输出通过管道传输到 awk 通常不是一个好主意; awk 完全有能力进行 grep 所做的过滤。

"git log" 命令功能强大,足以自行处理。尝试变体:

git log --date=iso --pretty=format:"date={%ad}, commit={%H}"

对我来说输出:

date={2014-12-12 14:14:23 -0800}, commit={75390af8bcd42c4dde6d841dc53f66a9ca91f460}
date={2014-12-09 18:53:32 -0800}, commit={8dcf9eb10611e6ac778e518cf37efb041c69f5b5}
date={2014-12-11 17:17:26 -0800}, commit={c3c1120b6dda6b317e1de5c7fe4eed7c8741ea1a}

要实现您指定的确切格式(尽管日期格式更合理一些),试试这个:

 ​git log --date=short --pretty=format:"%ad %H"

输出如下内容:

2015-08-18 1d0ca5a596f02958c4ba8ff269def3f235c64495
2015-08-18 d92fc3ebb62d2ca3e24322db2b669fdaebde7ea1
2015-08-18 6ba3970620e19699c2969b1eed5c479b07b8908a

您可能会找到格式漂亮的解决方案?

http://git-scm.com/docs/pretty-formats

这样就可以了

git log -5 --pretty=format:"%ad %H" | awk '{print , , , }'

10 Aug 2015 9fb4b1b5e7bcbc7f55e40ceae1758a7288673a51
10 Aug 2015 3d613862ca53787c97a42e8e2c37e09c8752b637
7 Aug 2015 727af2863dbe9f7b4da9189bc12f97a99aac5705
7 Aug 2015 1438b43b5755079bb3e62c819bfef2629d8336e9
7 Aug 2015 34303c19fb11482d66fc3c1102bc8d8b433af21a