我如何用(至少)这些信息显示git日志输出:

* author
* commit date
* change

我想把它压缩到每个日志条目一行。最短的格式是什么?

(try——format=oneline,但不显示日期)


当前回答

为了显示我已经准备好执行的提交

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(/[, ]/,"",$2);gsub(/HEAD/, "\033[1;36mH\033[00m",$2);gsub(/master/, "\033[1;32mm\033[00m",$2);gsub(/trunk/, "\033[1;31mt\033[00m",$2);print $1 "\t" gensub(/([\(\)])/, "\033[0;33m\\1\033[00m","g",$2) $3}' | less -eiFRXS

输出如下所示:

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

其中第一列出现在黄色,'H' 'm'和't'在括号中显示的头部,主人和主干,并出现在他们通常的“装饰”颜色

这里有换行符,你可以看到它在做什么:

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/, "\033[1;36mH\033[00m",$2);
         gsub(/master/, "\033[1;32mm\033[00m",$2);
         gsub(/trunk/, "\033[1;31mt\033[00m",$2);
         print $1 "\t" gensub(/([\(\)])/, "\033[0;33m\\1\033[00m","g",$2) $3}'

我用别名“staging”:

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/, \"\033[1;36mH\033[00m\",\$2);gsub(/master/, \"\033[1;32mm\033[00m\",\$2);gsub(/trunk/, \"\033[1;31mt\033[00m\",\$2);print \$1 \"\t\" gensub(/([\(\)])/, \"\033[0;33m\\\\\1\033[00m\",\"g\",\$2) \$3}"'

(有没有更简单的方法来避免这种情况?弄清楚需要逃脱的是什么有点棘手)

其他回答

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.)

样例输出:

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
git log --pretty=format:'%h %ad  %s%x09%ae' --date=short

结果:

e17bae5 2011-09-30  Integrate from development -> main      nixon@whitehouse.gov
eaead2c 2011-09-30  More stuff that is not worth mentioning bgates@apple.com
eb6a336 2011-09-22  Merge branch 'freebase' into development        jobs@nirvana.org

首先是固定宽度的内容。最不重要的部分——电子邮件域——是最后一个很容易过滤的部分。

看起来这就是你想要的:

git log --pretty=" %C(reset)%ad %C(Cyan)%an: %C(reset)%s"

(个人注意,你应该总是有提交哈希..)

前面提到的所有建议都使用%s占位符作为主题。我建议使用%B,因为%s格式保留新行,多行提交消息显示被压扁。

git log --pretty=format:"%h%x09%an%x09%ai%x09%B"

Tig可能是git log命令的替代品,在主要的开源*nix发行版中可用。

在debian或ubuntu上尝试按以下方式安装和运行:

$ sudo apt-get install tig

对于mac用户,brew可以拯救他们:

$ brew install tig

(安装tig)

$ tig

(日志在页导航中显示如下,当前提交的散列显示在底部)

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%)

由于markdown不支持文本着色,想象一下:列1:蓝色;第二栏:绿色;第3列:默认文本颜色。最后一行,高亮显示。按Q或Q退出。


Tig对没有粗糙边缘的列进行了校正,而ASCII制表符(%x09)并不能保证这一点。

对于短日期格式,请键入大写D(注意:小写D打开diff视图)。通过在~/.tigrc中添加show-date = short来永久配置它;或者在.git/configure或~/.gitconfig的[tig]部分中。

看到整个变化:

回车。一个子窗格将在窗口的下半部分打开。 使用k, j键滚动子窗格中的更改。 同时,使用上、下键从一个提交移动到另一个提交。

由于tig与git是分开的,而且显然是*nix专用的,它可能需要cygwin才能安装在windows上。但是对于fedora,我相信安装命令是$ su,(输入root密码),# yum install tig。对于freebsd尝试% su,(输入root密码),# pkg_add -r tig。


顺便说一下,tig的好处不仅仅是快速查看日志: 截图& 手册