Git的日志工具在版本控制中有很重要的作用, 如:

  • 在团队协作中, 各成员需要实时了解当前项目的 开发进度
  • 在分支查看及合并上, 通过 --graph 选项查看分支结构来帮助合并
    而我所说的日志工具, 实则就是通过 git log 命令查看 提交历史 .
1
2
3
4
5
6
7
8
9
10
11
12
$ git log
commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: Char Jin <charjindev@gmail.com>
Date: Sat Mar 15 16:40:33 2019 -0800

removed unnecessary test

commit a11bef06a3f659402fe7563abf99ad00de2209e6
Author: Char Jin <charjindev@gmail.com>
Date: Sat Mar 15 10:31:28 2019 -0800

first commit

这是最基本的日志显示形式, 其中包含所有的提交记录(按时间降序), 且一并列出 SHA-1校验和、作者姓名、邮箱、提交日期以及提交信息. 但这些数据并不适应于所有场合的显示, 所以需要定制化地显示 log 信息.

  1. 通过 -p 选项显示每次提交所引入的差异, -2 输出最近的2次提交.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    $ git log -p -2
    commit ca82a6dff817ec66f44342007202690a93763949
    Author: Char Jin <charjindev@gmail.com>
    Date: Mon Mar 17 21:52:11 2019 -0800

    changed the version number

    diff --git a/Rakefile b/Rakefile
    index a874b73..8f94139 100644
    --- a/Rakefile
    +++ b/Rakefile
    @@ -5,7 +5,7 @@ require 'rake/gempackagetask'
    spec = Gem::Specification.new do |s|
    s.platform = Gem::Platform::RUBY
    s.name = "simplegit"
    - s.version = "0.1.0"
    + s.version = "0.1.1"
    s.author = "Char Jin"
    s.email = "charjindev@gmail.com"

以上 + - 号所在行分别表示该文件与上次提交时相比, 增加的新内容与删去的旧内容. 而修改一行实际上就表现为 删去了该行又添加了新行

  1. -stat 选项查看每次提交的统计信息
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    $ git log --stat
    commit ca82a6dff817ec66f44342007202690a93763949
    Author: Char Jin <charjindev@gmail.com>
    Date: Mon Mar 17 21:52:11 2019 -0800

    changed the version number

    Rakefile | 2 +-
    1 file changed, 1 insertion(+), 1 deletion(-)

    commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
    Author: Char Jin <charjindev@gmail.com>
    Date: Sat Mar 15 16:40:33 2019 -0800

    removed unnecessary test

    lib/simplegit.rb | 5 -----
    1 file changed, 5 deletions(-)

    commit a11bef06a3f659402fe7563abf99ad00de2209e6
    Author: Char Jin <charjindev@gmail.com>
    Date: Sat Mar 15 10:31:28 2019 -0800

    first commit

    README | 6 ++++++
    Rakefile | 23 +++++++++++++++++++++++
    lib/simplegit.rb | 25 +++++++++++++++++++++++++
    3 files changed, 54 insertions(+)

实际上 stat 就是 statistics 的缩写, 通过该选项日志会统计每一次提交 文件改动的数量以及有多少新增行和删除行 , 并且能看到在日志的最后输出了总计信息.

  1. 以上的两种输出选项是很直观地显示了数据的改动情况, 但是它并不适合所有场景, 很多时候我们只想知道近期有几次新的提交, 是谁提交的, 提交的内容是什么等等. 那实际上我需要的是一个简明的提交信息, 此时 --pretty 就非常有用了. 它用来更改日志的默认输出格式, 语法如下
    1
    2
    $ git log --pretty=oneline
    ca82a6dff817ec66f44342007202690a93763949 changed the version number 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7 removed unnecessary test a11bef06a3f659402fe7563abf99ad00de2209e6 first commit

以上命令中的 oneline 是Git的预置格式, 其含义如字面所述(即一行), 与之类似的还有 short、full、fuller 等等, 具体的格式效果怎样呈现, 读者可以自行尝试.

  1. 虽然Git预置的格式选项很好用, 但是其定制化程度还是不高, 所以Git提供了另一种方式让使用者自定义log的输出格式
    1
    2
    3
    4
    $ git log --pretty=format:"%h - %an, %ar : %s" 
    ca82a6d - Char Jin, 6 years ago : changed the version number
    085bb3b - Char Jin, 6 years ago : removed unnecessary test
    a11bef0 - Char Jin, 6 years ago : first commit
    格式选项如下:
Option Description 中文释义
%H Commit hash 提交对象的散列值
%h Abbreviated commit hash 提交对象的简短散列值
%T Tree hash 树对象的散列值
%t Abbreviated tree hash 树对象的简短散列值
%P Parent hashes 父对象的散列值
%p Abbreviated parent hashes 父对象的简短散列值
%an Author name 作者的名字
%ae Author email 作者的电子邮件地址
%ad Author date (format respects the –date=option) 创作日期(可以用 –date= 选项定制格式)
%ar Author date, relative 相对于当前日期的创作日期
%cn Committer name 提交者的名字
%ce Committer email 提交者的电子邮件地址
%cd Committer date 提交日期
%cr Committer date, relative 相对于当前日期的提交日期
%s Subject 提交信息的主题