包含作者和日期的 git log 的最短输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1441010/
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
The shortest possible output from git log containing author and date
提问by Jesper R?nn-Jensen
How can I show a git log output with (at least) this information:
如何显示带有(至少)以下信息的 git log 输出:
* author
* commit date
* change
I want it compressed to one line per log entry. What's the shortest possible format for that?
我希望每个日志条目将其压缩为一行。什么是最短的格式?
(tried --format=oneline
but that does not show the date)
(试过,--format=oneline
但不显示日期)
回答by Jesper R?nn-Jensen
git log --pretty=format:"%h%x09%an%x09%ad%x09%s"
git log --pretty=format:"%h%x09%an%x09%ad%x09%s"
did the job. This 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
93f1526 jesper Tue Nov 25 09:45:56 2008 +0000 adding time.ZONE.now as time zone
2f0f8c1 tobias Tue Nov 25 03:07:02 2008 +0000 Timezone configured in environment
a33c1dc jesper Tue Nov 25 01:26:18 2008 +0000 updated to most recent will_pagina
Inspired by stackoverflow question: "git log output like svn ls -v", i found out that I could add the exact params I needed.
受到stackoverflow 问题的启发:“git log output like svn ls -v”,我发现我可以添加我需要的确切参数。
To shorten the date (not showing the time) use --date=short
缩短日期(不显示时间)使用 --date=short
In case you were curious what the different options were:%h
= abbreviated commit hash%x09
= tab (character for code 9)%an
= author name%ad
= author date (format respects --date= option)%s
= subject
From kernel.org/pub/software/scm/git/docs/git-log.html(PRETTY FORMATS section) by comment of Vivek.
如果您想知道不同的选项是什么:%h
= 缩写提交哈希%x09
= 制表符(代码 9 的字符)%an
= 作者姓名%ad
= 作者日期(格式方面 --date= 选项)%s
= 主题
来自kernel.org/pub/software/scm /git/docs/git-log.html(漂亮格式部分)由 Vivek 评论。
回答by andsens
I use these two .gitconfig settings:
我使用这两个 .gitconfig 设置:
[log]
date = relative
[format]
pretty = format:%h %Cblue%ad%Creset %ae %Cgreen%s%Creset
%ad is the author date, which can be overidden by --date
or the option specified in the [log] stanza in .gitconfig.
I like the relative date because it gives an immediate feeling of when stuff was comitted.
Output looks like this:
%ad 是作者日期,它可以被 .gitconfig--date
的 [log] 节中指定的选项或选项覆盖。我喜欢相对日期,因为它可以让人们立即感受到提交的时间。输出如下所示:
6c3e1a2 2 hours ago [email protected] lsof is a dependency now.
0754f18 11 hours ago [email protected] Properly unmount, so detaching works.
336a3ac 13 hours ago [email protected] Show ami registration command if auto register fails
be2ad45 17 hours ago [email protected] Fixes #6. Sao Paolo region is included as well.
5aed68e 17 hours ago [email protected] Shorten while loops
This is all of course in color, so it is easy to distinguish the various parts of a log line.
Also it is the default when typing git log
because of the [format] section.
这当然都是彩色的,因此很容易区分日志线的各个部分。git log
由于 [format] 部分,它也是键入时的默认值。
2014 UPDATE: Since git now supports padding I have a nice amendment to the version above:
2014 更新:由于 git 现在支持填充,我对上面的版本有一个很好的修正:
pretty = format:%C(yellow)%h %Cblue%>(12)%ad %Cgreen%<(7)%aN%Cred%d %Creset%s
This right aligns the relative dates and left aligns committer names, meaning you get a column-like look that is easy on the eyes.
这个右边对齐相对日期,左边对齐提交者名称,这意味着你会得到一个很容易看的列状外观。
Screenshot截屏2016 UPDATE: Since GPG commit signing is becoming a thing, I thought I'd update this post with a version that includes signature verification (in the screenshot it's the magenta letter right after the commit). A short explanation of the flag:
2016 年更新:由于 GPG 提交签名正在成为一种东西,我想我会用一个包含签名验证的版本更新这篇文章(在屏幕截图中,它是提交后的洋红色字母)。标志的简短说明:
%G?: show "G" for a good (valid) signature, "B" for a bad signature, "U" for a good signature with unknown validity and "N" for no signature
%G?:显示“G”表示好的(有效)签名,“B”表示错误的签名,“U”表示有效性未知的好签名,“N”表示没有签名
Other changes include:
其他变化包括:
- colors are now removed if the output is to something other than the tty (which is useful for grepping etc.)
git log -g
now contains the reflog selector.- Save 2 parens on refnames and put them at the end (to preserve column alignment)
- Truncate relative dates if they are too long (e.g.
3 years, 4..
) - Truncate commiter names (might be a little short for some ppl, just change the
%<(7,trunc)
or check out the git .mailmap featureto shorten commiter names)
- 如果输出是 tty 以外的其他内容(这对于 grepping 等很有用),则颜色现在将被删除
git log -g
现在包含 reflog 选择器。- 在 refnames 上保存 2 个括号并将它们放在最后(以保持列对齐)
- 如果相对日期太长,则截断它们(例如
3 years, 4..
) - 截断提交者名称(对于某些人来说可能有点短,只需更改
%<(7,trunc)
或查看git .mailmap 功能即可缩短提交者名称)
Here's the config:
这是配置:
pretty = format:%C(auto,yellow)%h%C(auto,magenta)% G? %C(auto,blue)%>(12,trunc)%ad %C(auto,green)%<(7,trunc)%aN%C(auto,reset)%s%C(auto,red)% gD% D
All in all column alignment is now preserved a lot better at the expense of some (hopefully) useless characters. Feel free to edit if you have any improvements, I'd love to make the message color depend on whether a commit is signed, but it doesn't seem like that is possible atm.
总而言之,现在以牺牲一些(希望)无用字符为代价,更好地保留了列对齐。如果您有任何改进,请随时进行编辑,我想让消息颜色取决于提交是否已签名,但在 atm 中似乎不可能。
Screenshot截屏回答by knittl
git log --pretty=format:"%H %an %ad"
use --date=
to set a date format
用于--date=
设置日期格式
git log --pretty=format:"%H %an %ad" --date=short
回答by Hannes Schneidermayer
回答by Joe Generic
tigis a possible alternative to using the git logcommand, available on the major open source *nix distributions.
tig是使用git log命令的可能替代方法,可在主要的开源 *nix 发行版上使用。
On debian or ubuntutry installing and running as follows:
在debian 或 ubuntu 上尝试安装和运行如下:
$ sudo apt-get install tig
(tig gets installed)
(tig被安装)
$ tig
(log is displayed in pager as follows, with current commit's hash displayed at the bottom)
(日志显示在分页器中如下,当前提交的哈希显示在底部)
2010-03-17 01:07 ndesigner changes to sponsors list
2010-03-17 00:19 rcoder Raise 404 when an invalid year is specified.
2010-03-17 00:06 rcoder Sponsors page now shows sponsors' level.
-------------------------- skip some lines ---------------------------------
[main] 531f35e925f53adeb2146dcfc9c6a6ef24e93619 - commit 1 of 32 (100%)
Since markdown doesn't support text coloring, imagine: column 1: blue; column 2: green; column 3: default text color. Last line, highlighted. Hit Qor qto exit.
由于 Markdown 不支持文本着色,想象一下:第 1 列:蓝色;第 2 列:绿色;第 3 列:默认文本颜色。最后一行,突出显示。按Q或q退出。
tig
justifies the columns without ragged edges, which an ascii tab (%x09) doesn't guarantee.
tig
对齐没有参差不齐的边缘的列,这是 ascii 选项卡 (%x09) 不能保证的。
For a short date formathit capital D(note: lowercase dopens a diff view.) Configure it permanently by adding show-date = short
to ~/.tigrc; or in a [tig]
section in .git/configure or ~/.gitconfig.
对于短日期格式,请按大写D(注意:小写d打开差异视图。)通过添加show-date = short
到 ~/.tigrc; 来永久配置它;或在[tig]
.git/configure 或 ~/.gitconfig中的一个部分。
To see an entire change:
要查看整个更改:
- hit Enter. A sub pane will open in the lower half of the window.
- use k, jkeys to scroll the change in the sub pane.
- at the same time, use the up, downkeys to move from commit to commit.
- 点击Enter。将在窗口的下半部分打开一个子窗格。
- 使用k, j键在子窗格中滚动更改。
- 同时,使用up、down键从提交移动到提交。
Since tig is separate from git and apparently *nix specific, it probably requires cygwin to install on windows. But for fedoraI believe the install commands are $ su
, (enter root password)
, # yum install tig
. For freebsdtry % su
, (enter root password)
, # pkg_add -r tig
.
由于 tig 与 git 分开并且显然是 *nix 特定的,因此它可能需要在 Windows 上安装 cygwin。但是对于Fedora,我相信安装命令是$ su
, (enter root password)
, # yum install tig
。对于freebsd试试% su
, (enter root password)
, # pkg_add -r tig
.
By the way, tig is good for a lot more than a quick view of the log: Screenshots& Manual
回答by Sean
git log --pretty=format:'%h %ad %s (%an)' --date=short
or
或者
git log --pretty=format:'%h %ad %s | %an' --date=short
...riffing on cdunn2001's answer above: I'd lose the author's e=mail and include just the author's name, as per Jesper and knittl, but in keeping with cdunn2001's idea of maintaining output in columns of constant width for ease of reading (great idea!). In lieu of a separate left justified column for author name, however, I wrap that flag at the end of the command with a parentheses or offset it with a pipe. (Could really be any character that serves as a visual aid in reading the output...albeit might make sense to avoid back or forward slashes in order to reduce confusing the output with a directory or something.)
...重复上面 cdunn2001 的回答:我会丢失作者的电子邮件 = 邮件,只包含作者的姓名,根据 Jesper 和 knittl,但与 cdunn2001 的想法保持一致,即以恒定宽度的列保持输出以便于阅读(好点子!)。然而,为了代替作者姓名的单独左对齐列,我在命令末尾用括号包装该标志或用管道偏移它。(真的可以是任何在阅读输出时用作视觉辅助的字符......尽管避免使用反斜杠或正斜杠以减少输出与目录或其他东西的混淆可能是有意义的。)
Sample output:
示例输出:
6fdd155 2015-08-10 Fixes casting error in doSave | John Doe
c4f4032 2015-08-10 Fix for IE save. Add help button. | Jane
29a24a6 2015-08-10 Fixes bug in Course | Mac
回答by Míra
Use predefined git alias, i.e.:
使用预定义的 git 别名,即:
$ git work
Created once by command:
通过命令创建一次:
$ git config --global alias.work 'log --pretty=format:"%h%x09%an%x09%ad%x09%s"'
https://git-scm.com/book/tr/v2/Git-Basics-Git-Aliases
https://git-scm.com/book/tr/v2/Git-Basics-Git-Aliases
Or more colored with graph:
或更多颜色的图表:
$ git config --global alias.work 'log --pretty=format:"%C(yellow)%h %ar %C(auto)%d %Creset %s , %Cblue%cn" --graph --all'
回答by cdunn2001
git log --pretty=format:'%h %ad %s%x09%ae' --date=short
Result:
结果:
e17bae5 2011-09-30 Integrate from development -> main [email protected]
eaead2c 2011-09-30 More stuff that is not worth mentioning [email protected]
eb6a336 2011-09-22 Merge branch 'freebase' into development [email protected]
Constant-width stuff is first. The least important part -- the email domain -- is last and easy to filter.
等宽的东西是第一位的。最不重要的部分 - 电子邮件域 - 最后且易于过滤。
回答by Sam Hasler
To show the commits I have staged that are ready to push I do
为了显示我已经准备好推送的提交,我做
git log remotes/trunk~4..HEAD --pretty=format:"%C(yellow)%h%C(white) %ad %aN%x09%d%x09%s" --date=short | awk -F'\t' '{gsub(/[, ]/,"",);gsub(/HEAD/, "3[1;36mH3[00m",);gsub(/master/, "3[1;32mm3[00m",);gsub(/trunk/, "3[1;31mt3[00m",);print "\t" gensub(/([\(\)])/, "3[0;33m\13[00m","g",) }' | less -eiFRXS
The output looks something like:
输出类似于:
ef87da7 2013-01-17 haslers (Hm)Fix NPE in Frobble
8f6d80f 2013-01-17 haslers Refactor Frobble
815813b 2013-01-17 haslers (t)Add Wibble to Frobble
3616373 2013-01-17 haslers Add Foo to Frobble
3b5ccf0 2013-01-17 haslers Add Bar to Frobble
a1db9ef 2013-01-17 haslers Add Frobble Widget
Where the first column appears in yellow, and the 'H' 'm' and 't' in parentesis show the HEAD, master and trunk and appear in their usual "--decorate" colors
第一列显示为黄色,括号中的 'H' 'm' 和 't' 显示 HEAD、master 和 trunk 并以它们通常的“--decorate”颜色显示
Here it is with line breaks so you can see what it's doing:
这里有换行符,所以你可以看到它在做什么:
git log remotes/trunk~4..HEAD --date=short
--pretty=format:"%C(yellow)%h%C(white) %ad %aN%x09%d%x09%s"
| awk -F'\t' '{
gsub(/[, ]/,"",);
gsub(/HEAD/, "3[1;36mH3[00m",);
gsub(/master/, "3[1;32mm3[00m",);
gsub(/trunk/, "3[1;31mt3[00m",);
print "\t" gensub(/([\(\)])/, "3[0;33m\13[00m","g",) }'
I have aliased to "staged" with:
我已别名为“上演”:
git config alias.staged '!git log remotes/trunk~4..HEAD --date=short --pretty=format:"%C(yellow)%h%C(white) %ad %aN%x09%d%x09%s" | awk -F"\t" "{gsub(/[, ]/,\"\",$2);gsub(/HEAD/, \"3[1;36mH3[00m\",$2);gsub(/master/, \"3[1;32mm3[00m\",$2);gsub(/trunk/, \"3[1;31mt3[00m\",$2);print $1 \"\t\" gensub(/([\(\)])/, \"3[0;33m\\3[00m\",\"g\",$2) $3}"'
(Is there an easier way to escape that? it was a bit tricky to work out what needed escaping)
(有没有更简单的方法来逃避它?找出需要逃避的东西有点棘手)
回答by palik
All aforementioned suggestions use %s
placeholder for subject. I'll recommend to use %B
because %s
formatting preserves new linesand multiple lines commit message appears squashed.
所有上述建议都使用%s
占位符作为主题。我会推荐使用,%B
因为%s
格式会保留新行并且多行提交消息会被压扁。
git log --pretty=format:"%h%x09%an%x09%ai%x09%B"