Git手册 - Git log

2021-05-05
git
约 1659 字 预计阅读 4 分钟

git log是每天最常用的功能, 可以查看过往的历史,提交代码的差异,时间,文件,作者,以及分支和commit的关系和前后顺序

1. 常用的选项

1
2
3
git log --oneline                               # 等同于--pretty=oneline
git log -n                                      # n是数量,查看最近的n次提交
git log -L start,end:filename                   # 查看文件的范围的提交历史

2. 重要

2.1. log的操作模式

git log的模式基本和vim相同,所以移动和搜索基本都是支持的

/可以搜索, 搜索结果页可以用n/N来进行跳转

vim的移动和翻页都支持

2.2. 一些特殊符号

  • ^ 排除

  • .. 在两者之间, 显示后者,并且排除掉前者

3. Diff

有时候我们需要查询文件的修改记录,log就可以简单的查看

  • git log --patch|-p 可以查看diff文件, 修改的详细信息
  • git log --stat 可以查看diff的统计信息摘要
 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
git log -p
# 输出如下
commit 699aaa85694f31ac14752c22070c762d32dbaf70
Author: coco <ergal@163.com>
Date:   Wed May 19 23:24:46 2021 +0800

    chore: update versionrc file

diff --git a/.versionrc b/.versionrc
index 7ab9b3c..21be9b7 100644
--- a/.versionrc
+++ b/.versionrc
@@ -4,9 +4,9 @@
             {"type": "feat", "section": "Features"},
             {"type": "fix", "section": "Bug Fixes"},
             {"type": "chore", "hidden": true},
-            {"type": "docs", "hidden": false},
+            {"type": "docs", "hidden": false , "section": "Document"},
             {"type": "style", "hidden": true},
-            {"type": "refactor", "hidden": false},
+            {"type": "refactor", "hidden": false, "section": "Refactor"},
             {"type": "perf", "hidden": false},
             {"type": "test", "hidden": true}
         ]

commit 096efa43f9eb80ba35fc27fb27d53f254173a388
Author: coco <ergal@163.com>
Date:   Wed May 19 23:17:20 2021 +0800

    feat: add article to courses

    changes
        * add course category articles
        * add course subcate articles

    Closes #25, #26

diff --git a/applications/articles/forms.py b/applications/articles/forms.py
index 6a31fc0..8c9e481 100644
--- a/applications/articles/forms.py
+++ b/applications/articles/forms.py
@@ -38,8 +38,9 @@ class ArticleForm(ModelForm):
     desc = forms.CharField(max_length=200, required=True, label=u"描述")
     content = forms.CharField(required=True, label=u"内容")
     keywords = forms.CharField(required=False, label=u"关键字")
-
+    business_id = forms.IntegerField(required=False)
+    business_item_id = forms.IntegerField(required=False)



git log --stat
# 输出如下
commit 699aaa85694f31ac14752c22070c762d32dbaf70
Author: coco <ergal@163.com>
Date:   Wed May 19 23:24:46 2021 +0800

    chore: update versionrc file

 .versionrc | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

commit 096efa43f9eb80ba35fc27fb27d53f254173a388
Author: coco <ergal@163.com>
Date:   Wed May 19 23:17:20 2021 +0800

    feat: add article to courses

    changes
        * add course category articles
        * add course subcate articles

    Closes #25, #26

 applications/articles/forms.py                     |   5 +-
 .../articles/migrations/0005_auto_20210519_1800.py |  23 ++
 applications/articles/models.py                    |   6 +-
 .../articles/templates/bg_admin/article_edit.html  |   4 +
 .../articles/templates/bg_admin/article_index.html |  12 +-
 .../articles/templates/bg_admin/article_new.html   |   4 +
 applications/articles/views.py                     |  60 ++-
 .../courses/templates/bg_admin/course_cate.html    | 433 +++++++++++----------
 .../templates/bg_admin/course_cate_sub_index.html  | 413 ++++++++++----------
 .../courses/templates/course/cate_courses.html     | 235 +++++------
 applications/courses/templatetags/courses_tags.py  |  57 ++-
 applications/courses/views.py                      |  13 +
 libs/utils.py                                      |  17 +
 rinpoche_ask_api/development.py                    |   9 +
 rinpoche_ask_api/production.py                     |  10 +
 rinpoche_ask_api/settings.py                       |   9 +
 rinpoche_ask_api/test.py                           |   9 +
 17 files changed, 764 insertions(+), 555 deletions(-)

commit dc95805cf40070ba76b1f6b22575a0a9c66786bd

查看单个文件的记录

1
git log -p --date=relative -- docker/.zshrc

具体信息可查看git log官方手册的DIFF FORMATTING章节

4. decorate

是否展示引用的名字,多分支开发的时候非常有用,这些提交来自哪个分支或者标签等引用信息, 默认是开启的(short)

git log --decorate[=short|full|auto|no]

例如:

1
2
3
4
7c3ad60 (HEAD -> develop, origin/master, origin/develop, azure/master, master) docs: update changelog manually
adeb53e docs: update changelog manually
9675519 (tag: v3.3.0) chore(release): 3.3.0
699aaa8 chore: update versionrc file

括号里就包含了ref信息,还有下面的选项可以用来过滤

--decorate-refs=<pattern>

--decorate-refs-exclude=<pattern>

5. graph

用ascii绘制分支图形,可以看到提交的分支模型

git log --graph

例如

1
2
3
4
5
6
7
8
*   0e25143 (HEAD, master) Merge branch 'feature'
|\  
| * 16b36c6 Fix a bug in the new feature
| * 23ad9ad Start a new feature
* | ad8621a Fix a critical security issue
|/  
* 400e4b7 Fix typos in the documentation
* 160e224 Add the initial code base

6. 输出格式Pretty Format

1
2
git log --pretty=[oneline|short|medium|full|fuller|reference|email|mboxrd|raw]
git log --pretty=format:占位符

6.1. format

格式化输出有很多占位符,比如

1
2
3
4
5
6
7
8
%H
commit hash

%h
abbreviated commit hash

%T
tree hash

示例:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
git log --pretty=format:%h

7c3ad60
adeb53e
9675519
699aaa8
096efa4
...


git log --pretty=format:"%C(magenta)%h%Creset -%C(red)%d%Creset %s %C(dim green)(%cr) [%an]" --abbrev-commit -30 --graph

7. 过滤和条件筛选

常用的过滤方式如下, 包含日期, 作者

7.1. 日期

–since=

–after=

Show commits more recent than a specific date.

–until=

–before=

Show commits older than a specific date.

7.2. 作者

–author=

–committer=

Limit the commits output to ones with author/committer header lines that match the specified pattern (regular expression).

7.3. 过滤message

–grep

7.4. 按文件内容过滤

–pickaxe-all 或者 -S 或者 -G

–pickaxe-regex-S转化为regex模式

但是注意-S<regex> --pickaxe-regex 不等同于 -G<regex>

1
git log -S"Bug" -p

7.5. 范围

使用..来排除前者

  • 可以使commit之间
  • 可以是branch之间
1
2
3
4
# 显示这两次提交之间的不同, 也就是3e66174这次提交之前(包含这个提交)和现在的不同之处
git log HEAD..3e66174
# 显示分支之间的不同
git log master..feature/calendar

7.6. 示例

1
2
3
4
git log --since="2 weeks ago" --until=yestoday
git log --before="2 days ago"
git log --until=yestoday
git log --author=coco

7.7. 日期格式

--date=[iso-strict|defaul|rfc|rfc2822|iso|local|relative|human|raw|short|unix|format:...]

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# 短格式
git log --date=short

commit 7c3ad6040c1b5dcf739ca6e0446fbc85c6337f6a (HEAD -> develop, origin/master, origin/develop, azure/master, master)
Author: coco <ergal@163.com>
Date:   2021-05-19

    docs: update changelog manually
# 相对时间
git log --date=relative
commit 7c3ad6040c1b5dcf739ca6e0446fbc85c6337f6a (HEAD -> develop, origin/master, origin/develop, azure/master, master)
Author: coco <ergal@163.com>
Date:   2 days ago

    docs: update changelog manually

具体查看git log 的help的Commit Formatting

8. 其它选项

  • --no-merges 排除merge的commit
  • --merges 只看合并commit(merge,rebase的不包含在内,除非rebase加上了merge参数)

9. 相关操作

9.1. shortlog

git shortlog

TAG: git scm
文章作者 : Cocding