面向工资编程,面向面试学习!

查看提交历史

当我们在项目里提交了若干更新,或者克隆了某个项目之后,我们该怎么看提交历史呢? git log 命令可以让我们查看提交commit history。为了练习 git log 的用法,我们先克隆 flask 项目,然后进入该项目,并在项目中运行 git log:

git clone git@github.com:pallets/flask.git
cd flask
git log
commit 38eb5d3b49d628785a470e2e773fc5ac82e3c8e4 (HEAD -> master, origin/master, origin/HEAD)
Author: Adrian 
Date:   Mon Nov 25 00:38:50 2019 +0100

    Remove comment about extension backwards compat

    0.7 was a long time ago; there's no reason for extension to supports such old versions.

commit 25a5d3031167566cef0fcf38b54afc6b7dcf6e86
Merge: d49cfb35 a3415fc6
Author: David Lord 
Date:   Tue Nov 19 09:37:21 2019 -0800

    Merge pull request #3427 from greyli/update-old-links

    Update old pocoo links everywhere

git log默认会输出commit hash, author, date, commit message,并且按照时间倒序将最新的更新显示在上面。

-p

输出每个commit具体修改的内容,输出的形式以diff的形式给出,-2 表示仅显示最近两次提交:

git log -p -2
commit 38eb5d3b49d628785a470e2e773fc5ac82e3c8e4 (HEAD -> master, origin/master, origin/HEAD)
Author: Adrian 
Date:   Mon Nov 25 00:38:50 2019 +0100

    Remove comment about extension backwards compat

    0.7 was a long time ago; there's no reason for extension to supports such old versions.

diff --git a/src/flask/app.py b/src/flask/app.py
index 21fd2b6d..c2208f14 100644
--- a/src/flask/app.py
+++ b/src/flask/app.py
@@ -549,12 +549,7 @@ class Flask(_PackageBoundObject):

         #: a place where extensions can store application specific state.  For
         #: example this is where an extension could store database engines and
-        #: similar things.  For backwards compatibility extensions should register
-        #: themselves like this::
-        #:
-        #:      if not hasattr(app, 'extensions'):
-        #:          app.extensions = {}
-        #:      app.extensions['extensionname'] = SomeObject()
+        #: similar things.
         #:
         #: The key must match the name of the extension module. For example in
         #: case of a "Flask-Foo" extension in `flask_foo`, the key would be

commit 25a5d3031167566cef0fcf38b54afc6b7dcf6e86
Merge: d49cfb35 a3415fc6
Author: David Lord 
Date:   Tue Nov 19 09:37:21 2019 -0800

    Merge pull request #3427 from greyli/update-old-links

    Update old pocoo links everywhere

--pretty=oneline

--pretty选项可以指定使用某种内建格式展示提交历史,用 oneline 表示将每个提交放在一行显示,仅仅输出 commit hash 和 commit message:

git log --pretty=oneline -5
38eb5d3b49d628785a470e2e773fc5ac82e3c8e4 (HEAD -> master, origin/master, origin/HEAD) Remove comment about extension backwards compat
25a5d3031167566cef0fcf38b54afc6b7dcf6e86 Merge pull request #3427 from greyli/update-old-links
a3415fc6dcf3f355a2b815686d6de12fa7383335 Use https for pallets URL
ef434ea9985b756f13bed283dfa55828829e5e1f Replace old pocoo links everywhere
d49cfb35d4bb45e94d8c7c1920e7be99e4e10dde Merge pull request #3435 from pallets/send-file-text

--pretty=format,可以定制要显示的记录格式,这样的输出便于后期编程提取分析:

git log --pretty=format:"%h - %an, %ar : %s" -3
38eb5d3b - Adrian, 2 weeks ago : Remove comment about extension backwards compat
25a5d303 - David Lord, 3 weeks ago : Merge pull request #3427 from greyli/update-old-links
a3415fc6 - Grey Li, 4 weeks ago : Use https for pallets URL

常用的格式占位符写法及其代表的意义:

选项 说明
%H 提交对象(commit)的完整哈希字串
%h 提交对象的简短哈希字串
%T 树对象(tree)的完整哈希字串
%t 树对象的简短哈希字串
%P 父对象(parent)的完整哈希字串
%p 父对象的简短哈希字串
%an 作者(author)的名字
%ae 作者的电子邮件地址
%ad 作者修订日期(可以用 -date= 选项定制格式)
%ar 作者修订日期,按多久以前的方式显示
%cn 提交者(committer)的名字
%ce 提交者的电子邮件地址
%cd 提交日期
%cr 提交日期,按多久以前的方式显示
%s 提交说明

--graph

显示 ASCII 图形表示的分支合并历史:

git log --graph -5
* commit 38eb5d3b49d628785a470e2e773fc5ac82e3c8e4 (HEAD -> master, origin/master, origin/HEAD)
| Author: Adrian 
| Date:   Mon Nov 25 00:38:50 2019 +0100
|
|     Remove comment about extension backwards compat
|
|     0.7 was a long time ago; there's no reason for extension to supports such old versions.
|
*   commit 25a5d3031167566cef0fcf38b54afc6b7dcf6e86
|\  Merge: d49cfb35 a3415fc6
| | Author: David Lord 
| | Date:   Tue Nov 19 09:37:21 2019 -0800
| |
| |     Merge pull request #3427 from greyli/update-old-links
| |
| |     Update old pocoo links everywhere
| |
| * commit a3415fc6dcf3f355a2b815686d6de12fa7383335
| | Author: Grey Li 
| | Date:   Fri Nov 15 12:34:13 2019 +0800
| |
| |     Use https for pallets URL
| |
| * commit ef434ea9985b756f13bed283dfa55828829e5e1f
|/  Author: Grey Li 
|   Date:   Fri Nov 15 12:27:44 2019 +0800
|
|       Replace old pocoo links everywhere
|
|       pocco.org -> palletsprojects.com
|
*   commit d49cfb35d4bb45e94d8c7c1920e7be99e4e10dde
|\  Merge: 2659f0a5 980168d0
| | Author: David Lord 
| | Date:   Tue Nov 19 09:08:07 2019 -0800
| |
| |     Merge pull request #3435 from pallets/send-file-text
| |
| |     send_file doesn't allow StringIO

git log 命令常用的选项及其释义:

选项 说明
-p 按补丁格式显示每个更新之间的差异。
--stat 显示每次更新的文件修改统计信息。
--shortstat 只显示 --stat 中最后的行数修改添加移除统计。
--name-only 仅在提交信息后显示已修改的文件清单。
--name-status 显示新增、修改、删除的文件清单。
--abbrev-commit 仅显示 SHA-1 的前几个字符,而非所有的 40 个字符。
--relative-date 使用较短的相对时间显示(比如,“2 weeks ago”)。
--graph 显示 ASCII 图形表示的分支合并历史。
--pretty 使用其他格式显示历史提交信息。可用的选项包括 oneline,short,full,fuller 和 format(后跟指定格式)。

限定log输出

  • 加 -n 参数指定输出的数量,n 是一个整数,表示你要输出的数量;
  • --since, --after 仅显示指定时间之后的提交;
  • --until, --before 仅显示指定时间之前的提交。;
git log -2 --since="2018-01-01" --before="2019-12-30"
commit 38eb5d3b49d628785a470e2e773fc5ac82e3c8e4 (HEAD -> master, origin/master, origin/HEAD)
Author: Adrian 
Date:   Mon Nov 25 00:38:50 2019 +0100

    Remove comment about extension backwards compat

    0.7 was a long time ago; there's no reason for extension to supports such old versions.

commit 25a5d3031167566cef0fcf38b54afc6b7dcf6e86
Merge: d49cfb35 a3415fc6
Author: David Lord 
Date:   Tue Nov 19 09:37:21 2019 -0800

    Merge pull request #3427 from greyli/update-old-links

    Update old pocoo links everywhere

限制 git log 输出的选项:

选项 说明
-(n) 仅显示最近的 n 条提交
--since, --after 仅显示指定时间之后的提交。
--until, --before 仅显示指定时间之前的提交。
--author 仅显示指定作者相关的提交。
--grep 仅显示含指定关键字的提交。
-S 仅显示添加或移除了某个关键字的提交。
--committer 仅显示指定提交者相关的提交。