Git - log

git 是一款非常牛逼的分布式代码管理工具,Linux 之父 linus torvalds 所编写,是目前全世界最流行的代码管理工具,全世界最大的代码托管平台 Github 就是基于 git 做的,无数的开源项目使用 git ,如果不会 git 简直无法在开源世界立足,因为最近在看 《pro git》 这本书,所以我会写几篇 git 的文章,这篇文章主要介绍 git log 子命令的一些用法

log 基础

如果想查看一个 git 管理的项目的提交记录,可以通过 log 子命令来查看,为了演示,就把 Flask 项目的源码拉下来演示

$ git clone https://github.com/pallets/flask.git

# 最近 Github 被墙的厉害,最好通过科学上网去 clone

进入项目,直接通过 git log 可以查看所有的历史提交

$ cd flask

$ git log
commit 6efea346dd54fa9adb65fc044d72e5ec8bdf4018
Merge: 1add1f8 ed17bc1
Author: David Lord <davidism@gmail.com>
Date: Thu Mar 16 13:43:01 2017 -0700

Merge pull request #2209 from svenstaro/print-stacktrace-on-cli-error

Print a stacktrace on CLI error (closes #2208)

commit ed17bc171046a15f03c890687db8eb9652513bd9
Author: Sven-Hendrik Haase <svenstaro@gmail.com>
Date: Thu Mar 16 20:56:12 2017 +0100

Add test to showcase that printing a traceback works

commit ad42d88fb2cda12a21c4fb6f002f425f233d1fe3
Author: Sven-Hendrik Haase <svenstaro@gmail.com>
Date: Thu Mar 16 14:42:09 2017 +0100

Remove useless pass

# ignore

follow

通过 --follow $FILE 选项可以查看所有提交历史中,包含指定文件更改的记录

$ git log --follow Makefile #查看 Makefile 的更改

commit a4ed3d28066bb1625c15fc6a89c1533535dc7879
Author: Markus Unterwaditzer <markus@unterwaditzer.net>
Date: Wed Nov 2 17:56:59 2016 +0100

Use tox from make test

commit fd1a355899691215bda059f410256e6fff470955
Author: Armin Ronacher <armin.ronacher@active-4.com>
Date: Thu Jun 2 09:44:41 2016 +0200

Added test-requirements.txt. Refs #1835

commit 523e27118359425048541d92892f20ee048c0b76
Author: Armin Ronacher <armin.ronacher@active-4.com>
Date: Thu May 26 20:07:52 2016 +0200

Implemented simplified CLI interface

感觉输出的条目太多了,我只想看最后一条怎么办?在选项前指定想要查看的行数 -number

$ git log -1 --follow Makefile

commit a4ed3d28066bb1625c15fc6a89c1533535dc7879
Author: Markus Unterwaditzer <markus@unterwaditzer.net>
Date: Wed Nov 2 17:56:59 2016 +0100

Use tox from make test

merges

通过 --merges 显示合并的提交

$ git log -1 --merges

commit 6efea346dd54fa9adb65fc044d72e5ec8bdf4018
Merge: 1add1f8 ed17bc1
Author: David Lord <davidism@gmail.com>
Date: Thu Mar 16 13:43:01 2017 -0700

Merge pull request #2209 from svenstaro/print-stacktrace-on-cli-error

Print a stacktrace on CLI error (closes #2208)

no-merges

通过--no-merges 显示不是合并的提交

$ git log -1 --no-merges

commit ed17bc171046a15f03c890687db8eb9652513bd9
Author: Sven-Hendrik Haase <svenstaro@gmail.com>
Date: Thu Mar 16 20:56:12 2017 +0100

Add test to showcase that printing a traceback works

author

通过--author 过滤出对应作者的提交,我们看一下 Flask 作者Armin Ronacher 的提交

$ git log --author='Armin Ronacher' -1

commit c9b33d0e860e347f1ed46eebadbfef4f5422b6da
Author: Armin Ronacher <armin.ronacher@active-4.com>
Date: Sun Jan 29 12:26:52 2017 +0100

Convert Flask.run into a noop when run from the CLI

stat

通过--stat 显示对比上一次提交,文件的增减

$ git log --stat -3

commit 6efea346dd54fa9adb65fc044d72e5ec8bdf4018
Merge: 1add1f8 ed17bc1
Author: David Lord <davidism@gmail.com>
Date: Thu Mar 16 13:43:01 2017 -0700

Merge pull request #2209 from svenstaro/print-stacktrace-on-cli-error

Print a stacktrace on CLI error (closes #2208)

commit ed17bc171046a15f03c890687db8eb9652513bd9
Author: Sven-Hendrik Haase <svenstaro@gmail.com>
Date: Thu Mar 16 20:56:12 2017 +0100

Add test to showcase that printing a traceback works

tests/test_cli.py | 17 +++++++++++++++++
1 file changed, 17 insertions(+)

commit ad42d88fb2cda12a21c4fb6f002f425f233d1fe3
Author: Sven-Hendrik Haase <svenstaro@gmail.com>
Date: Thu Mar 16 14:42:09 2017 +0100

Remove useless pass

flask/cli.py | 1 -
1 file changed, 1 deletion(-)

since

通过--since 显示某天之后的提交

$ git log --since '2 week ago'

commit 6efea346dd54fa9adb65fc044d72e5ec8bdf4018
Merge: 1add1f8 ed17bc1
Author: David Lord <davidism@gmail.com>
Date: Thu Mar 16 13:43:01 2017 -0700

Merge pull request #2209 from svenstaro/print-stacktrace-on-cli-error

Print a stacktrace on CLI error (closes #2208)

commit ed17bc171046a15f03c890687db8eb9652513bd9
Author: Sven-Hendrik Haase <svenstaro@gmail.com>
Date: Thu Mar 16 20:56:12 2017 +0100

Add test to showcase that printing a traceback works

commit ad42d88fb2cda12a21c4fb6f002f425f233d1fe3
Author: Sven-Hendrik Haase <svenstaro@gmail.com>
Date: Thu Mar 16 14:42:09 2017 +0100

Remove useless pass

# ignore

since 可以在很多种格式下工作,例如 git log --since 2015-7-11git log --since 2.weeks

until

通过—until 显示某天之前的提交,和since 相反

$ git log --until 6.year.11.month.17.day

commit 44b42e0fbd93d86e0f4e929bda8e5fb63e81035d
Author: Armin Ronacher <armin.ronacher@active-4.com>
Date: Tue Apr 6 16:16:34 2010 +0200

Added a README file for MiniTwit.

commit 4ec7d2a0d8eac4f915dc0d38a886cd57045bb0c4
Author: Armin Ronacher <armin.ronacher@active-4.com>
Date: Tue Apr 6 16:02:14 2010 +0200

Started working on documentation.

So far just the stable parts :)

commit b15ad394279fc3b7f998fa56857f334a7c0156f6
Author: Armin Ronacher <armin.ronacher@active-4.com>
Date: Tue Apr 6 13:23:18 2010 +0200

Added setup.py and README

commit 33850c0ebd23ae615e6823993d441f46d80b1ff0
Author: Armin Ronacher <armin.ronacher@active-4.com>
Date: Tue Apr 6 13:12:57 2010 +0200

Initial checkin of stuff that exists so far.

显示diff

也可以通过 -p 选项来显示每一次提交之间的 diff

$ git log -p -2

commit 6efea346dd54fa9adb65fc044d72e5ec8bdf4018
Merge: 1add1f8 ed17bc1
Author: David Lord <davidism@gmail.com>
Date: Thu Mar 16 13:43:01 2017 -0700

Merge pull request #2209 from svenstaro/print-stacktrace-on-cli-error

Print a stacktrace on CLI error (closes #2208)

commit ed17bc171046a15f03c890687db8eb9652513bd9
Author: Sven-Hendrik Haase <svenstaro@gmail.com>
Date: Thu Mar 16 20:56:12 2017 +0100

Add test to showcase that printing a traceback works

diff --git a/tests/test_cli.py b/tests/test_cli.py
index 313a34d..82c69f9 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -191,3 +191,20 @@ def test_flaskgroup():
result = runner.invoke(cli, ['test'])
assert result.exit_code == 0
assert result.output == 'flaskgroup\n'
+
+
+def test_print_exceptions():
+ """Print the stacktrace if the CLI."""
+ def create_app(info):
+ raise Exception("oh no")
+ return Flask("flaskgroup")
+
+ @click.group(cls=FlaskGroup, create_app=create_app)
+ def cli(**params):
+ pass
+
+ runner = CliRunner()
+ result = runner.invoke(cli, ['--help'])
+ assert result.exit_code == 0
+ assert 'Exception: oh no' in result.output
+ assert 'Traceback' in result.output

搜索名称

通过 grep 选项,可以通过提交名称来过滤

$ git log --grep fix -3

commit c43560777a3efeaeaf0eb47568171f04103dc363
Author: Grey Li <withlihui@gmail.com>
Date: Sat Mar 4 18:29:04 2017 +0800

Add tips for debug config with flask cli (#2196)

* Add tips for debug config with flask cli

`app.debug` and `app.config['DEBUG']` are not compatible with the `flask` script.

* Grammar fix

* Grammar fix

commit bb0e755c808a8541192982ba7b86308b68ff7657
Author: Jeff Widman <jeff@jeffwidman.com>
Date: Sat Feb 11 01:43:11 2017 -0800

Migrate various docs links to https (#2180)

Also fixed a few outdated links

commit 46c1383919454ae281967316d6d6fb33bce9b773
Author: wgwz <klawlor419@gmail.com>
Date: Sat Dec 31 12:37:39 2016 -0500

搜索字符串

通过 -S 选项,可以过滤出指定字符串的修改的提交,可以配合 -p 来查看

$ git log -S 'app.run()' -p -1

组合使用

git log 子命令大多数情况下都可以指定文件,查看这个文件的历史提交,如果不指定文件的情况下,则查看这个 repo 的历史提交

我们也可以组合上面这些选项使用

例如:找出Armin Ronacher 在六年 十一月 十八天 前对 setup.py 的提交

$ git log --author='Armin Ronacher' --stat --until 6.year.11.month.18.day setup.py

commit b15ad394279fc3b7f998fa56857f334a7c0156f6
Author: Armin Ronacher <armin.ronacher@active-4.com>
Date: Tue Apr 6 13:23:18 2010 +0200

Added setup.py and README

setup.py | 19 +++++++++++++++++++
1 file changed, 19 insertions(+)

更漂亮的输出

通过 pretty 选项可以让 log 的输出更加的符合我们的需求

oneline

输出格式如下

每个提交只输出一行,更加简洁

$ git log --pretty=oneline -2

6efea346dd54fa9adb65fc044d72e5ec8bdf4018 Merge pull request #2209 from svenstaro/print-stacktrace-on-cli-error
ed17bc171046a15f03c890687db8eb9652513bd9 Add test to showcase that printing a traceback works

short

输出格式如下

commit
Author:

输出简短的信息,默认就是 short

$ git log --pretty=short -2

commit 6efea346dd54fa9adb65fc044d72e5ec8bdf4018
Merge: 1add1f8 ed17bc1
Author: David Lord <davidism@gmail.com>

Merge pull request #2209 from svenstaro/print-stacktrace-on-cli-error

commit ed17bc171046a15f03c890687db8eb9652513bd9
Author: Sven-Hendrik Haase <svenstaro@gmail.com>

Add test to showcase that printing a traceback works

medium

输出格式如下

commit
Author:
Commit:

<full commit message> </full>
$ git log --pretty=medium -2

commit 6efea346dd54fa9adb65fc044d72e5ec8bdf4018
Merge: 1add1f8 ed17bc1
Author: David Lord <davidism@gmail.com>
Date: Thu Mar 16 13:43:01 2017 -0700

Merge pull request #2209 from svenstaro/print-stacktrace-on-cli-error

Print a stacktrace on CLI error (closes #2208)

commit ed17bc171046a15f03c890687db8eb9652513bd9
Author: Sven-Hendrik Haase <svenstaro@gmail.com>
Date: Thu Mar 16 20:56:12 2017 +0100

Add test to showcase that printing a traceback works

other

还有 fullemailfullfullerrawgit 自带的格式输出,这里就不介绍了

自定义输出

pretty 强大之处在于它可以自定义 log 的输出,就像下面这样

$ git log --pretty=format:'%H - %ae' -2

6efea346dd54fa9adb65fc044d72e5ec8bdf4018 - davidism@gmail.com
ed17bc171046a15f03c890687db8eb9652513bd9 - svenstaro@gmail.com

格式如下,更多的看官方文档。。

  • %H: commit hash
  • %h: abbreviated commit hash
  • %T: tree hash
  • %t: abbreviated tree hash
  • %P: parent hashes
  • %p: abbreviated parent hashes
  • %an: author name
  • %aN: author name (respecting .mailmap, see git-shortlog(1) or git-blame(1))
  • %ae: author email
  • %aE: author email (respecting .mailmap, see git-shortlog(1) or git-blame(1))
  • %ad: author date (format respects –date= option)
  • %aD: author date, RFC2822 style

stackoverflow 上抄了两个很好看的 log format,当然,你并不需要输入那么长,可以定义别名

git log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold green)(%ar)%C(reset) %C(white)%s%C(reset) %C(dim white)- %an%C(reset)%C(bold yellow)%d%C(reset)' --all
git log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n''          %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all

参考资料:

pretty git branch graphs

git log docs

《progit》