• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • git log

    git log命令用于显示提交日志信息。该命令采用适用于git rev-list命令的选项来控制显示的内容以及如何以及适用于git diff-*命令的选项,以控制如何更改每个提交引入的内容。

    语法

    git log [<options>] [<revision range>] [[\--] <path>…]
    


    选项

    --author

    只寻找某个特定作者的提交。要过滤你的提交历史,只寻找某个特定作者的提交,你可以使用--author选项。例如,比方说我们要找 Git 源码中 Linus 提交的部分。我们可以执行类似git log --author=Linus的命令。这个查找是大小写敏感的,并且也会检索电子邮箱地址。我在此例中使用-[number]选项,以限制结果为最近[number]次的提交。

    git log --author=Linus --oneline -5
    81b50f3 Move 'builtin-*' into a 'builtin/' subdirectory
    3bb7256 make "index-pack" a built-in
    377d027 make "git pack-redundant" a built-in
    b532581 make "git unpack-file" a built-in
    112dd51 make "mktag" a built-in
    


    --since --before

    如果你要指定一个你感兴趣的日期范围以过滤你的提交,可以执行几个选项——我用--since--before,但是你也可以用--until--after。例如,如果我要看 Git 项目中三周前且在四月十八日之后的所有提交,我可以执行这个(我还用了--no-merges选项以隐藏合并提交):

    git log --oneline --before={3.weeks.ago} --after={2010-04-18} --no-merges
    5469e2d Git 1.7.1-rc2
    d43427d Documentation/remote-helpers: Fix typos and improve language
    272a36b Fixup: Second argument may be any arbitrary string
    b6c8d2d Documentation/remote-helpers: Add invocation section
    5ce4f4e Documentation/urls: Rewrite to accomodate transport::address
    00b84e9 Documentation/remote-helpers: Rewrite description
    03aa87e Documentation: Describe other situations where -z affects git diff
    77bc694 rebase-interactive: silence warning when no commits rewritten
    636db2c t3301: add tests to use --format="%N"
    


    --grep

    根据提交注释过滤提交记录。你或许还想根据提交注释中的某个特定短语查找提交记录。可以用--grep选项。比如说我知道有个提交是有关使用 P4EDITOR 环境变量,又想回忆起那个改动是啥样子的。我可以用--grep选项找到该提交。

    git log --grep=P4EDITOR --no-merges
    commit 82cea9ffb1c4677155e3e2996d76542502611370
    Author: Shawn Bohrer
    Date:   Wed Mar 12 19:03:24 2008 -0500
    
        git-p4: Use P4EDITOR environment variable when set
    
        Perforce allows you to set the P4EDITOR environment variable to your
        preferred editor for use in perforce.  Since we are displaying a
        perforce changelog to the user we should use it when it is defined.
    
        Signed-off-by: Shawn Bohrer <shawn.bohrer@gmail.com>
        Signed-off-by: Simon Hausmann <simon@lst.de>
    

    Git 会对所有的--grep--author参数作逻辑或。如果你用--grep--author时,想看的是某人写作的并且有某个特殊的注释内容的提交记录,你需要加上--all-match选项。在这些例子中,我会用上--format选项,这样我们就可以看到每个提交的作者是谁了。

    如果我查找注释内容含有“p4 depo”的提交,我得到了三个提交:

    git log --grep="p4 depo" --format="%h %an %s"
    ee4fd1a Junio C Hamano Merge branch 'master' of git://repo.or.cz/git/fastimport
    da4a660 Benjamin Sergeant git-p4 fails when cloning a p4 depo.
    1cd5738 Simon Hausmann Make incremental imports easier to use by storing the p4 d
    

    如果我加上--author=Hausmann参数,与进一步过滤上述结果到 Simon 的唯一提交相反,它会告诉我所有 Simon 的提交,或者注释中有“p4 demo”的提交:

    git log --grep="p4 depo" --format="%h %an %s" --author="Hausmann"
    cdc7e38 Simon Hausmann Make it possible to abort the submission of a change to Pe
    f5f7e4a Simon Hausmann Clean up the git-p4 documentation
    30b5940 Simon Hausmann git-p4: Fix import of changesets with file deletions
    4c750c0 Simon Hausmann git-p4: git-p4 submit cleanups.
    0e36f2d Simon Hausmann git-p4: Removed git-p4 submit --direct.
    edae1e2 Simon Hausmann git-p4: Clean up git-p4 submit's log message handling.
    4b61b5c Simon Hausmann git-p4: Remove --log-substitutions feature.
    36ee4ee Simon Hausmann git-p4: Ensure the working directory and the index are cle
    e96e400 Simon Hausmann git-p4: Fix submit user-interface.
    38f9f5e Simon Hausmann git-p4: Fix direct import from perforce after fetching cha
    2094714 Simon Hausmann git-p4: When skipping a patch as part of "git-p4 submit" m
    1ca3d71 Simon Hausmann git-p4: Added support for automatically importing newly ap
    ...
    

    如果加上--all-match,结果就是我想要的了:

    git log --grep="p4 depo" --format="%h %an %s" --author="Hausmann" --all-match
    1cd5738 Simon Hausmann Make incremental imports easier to use by storing the p4 d
    


    -S

    依据所引入的差值过滤。如果你写的提交注释都极度糟糕怎么办?或者,如果你要找某个函数是何时引入的,某些变量是在哪里开始被使用的?你可以告诉 Git 在每个提交之间的差值中查找特定字符串。例如,如果我们想要找出哪个提交修改出了类似函数名“userformat_find_requirements”,我们可以执行(注意在“-S”与你要找的东东之间没有“=”):

    git log -Suserformat_find_requirements
    commit 5b16360330822527eac1fa84131d185ff784c9fb
    Author: Johannes Gilger
    Date:   Tue Apr 13 22:31:12 2010 +0200
    
        pretty: Initialize notes if %N is used
    
        When using git log --pretty='%N' without an explicit --show-notes, git
        would segfault. This patches fixes this behaviour by loading the needed
        notes datastructures if --pretty is used and the format contains %N.
        When --pretty='%N' is used together with --no-notes, %N won't be
        expanded.
    
        This is an extension to a proposed patch by Jeff King.
    
        Signed-off-by: Johannes Gilger
        Signed-off-by: Junio C Hamano
    


    -p

    显示每个提交引入的补丁。每个提交都是项目的一个快照。由于每个提交都记录它所基于的快照,Git 能够经常对它们求差值,并以补丁形式向你展示。这意味着,对任意提交,你都可以获取该提交给项目引入补丁。你可以用git show[SHA]加上某个特定的提交 SHA 获取,或者执行git log -p,它会告诉 Git 输出每个提交之后的补丁。这是个总结某一分支或者两个提交之间都发生了神马的好途径。

    git log -p --no-merges -2
    commit 594f90bdee4faf063ad07a4a6f503fdead3ef606
    Author: Scott Chacon 
    Date:   Fri Jun 4 15:46:55 2010 +0200
    
        reverted to old class name
    
    diff --git a/ruby.rb b/ruby.rb
    index bb86f00..192151c 100644
    --- a/ruby.rb
    +++ b/ruby.rb
    @@ -1,7 +1,7 @@
    -class HiWorld
    +class HelloWorld
       def self.hello
         puts "Hello World from Ruby"
       end
     end
    
    -HiWorld.hello
    +HelloWorld.hello
    
    commit 3cbb6aae5c0cbd711c098e113ae436801371c95e
    Author: Scott Chacon 
    Date:   Fri Jun 4 12:58:53 2010 +0200
    
        fixed readme title differently
    
    diff --git a/README b/README
    index d053cc8..9103e27 100644
    --- a/README
    +++ b/README
    @@ -1,4 +1,4 @@
    -Hello World Examples
    +Many Hello World Examples
     ======================
    
     This project has examples of hello world in
    

    这是个总结改动,以及合并或发布之前重审一系列提交的好方式。


    --stat

    显示每个提交引入的改动的差值统计。如果-p选项对你来说太详细了,你可以用--stat总结这些改动。这是不用-p,而用--stat选项时,同一份日志的输出。

    git log --stat --no-merges -2
    commit 594f90bdee4faf063ad07a4a6f503fdead3ef606
    Author: Scott Chacon 
    Date:   Fri Jun 4 15:46:55 2010 +0200
    
        reverted to old class name
    
     ruby.rb |    4 ++--
     1 files changed, 2 insertions(+), 2 deletions(-)
    
    commit 3cbb6aae5c0cbd711c098e113ae436801371c95e
    Author: Scott Chacon 
    Date:   Fri Jun 4 12:58:53 2010 +0200
    
        fixed readme title differently
    
     README |    2 +-
     1 files changed, 1 insertions(+), 1 deletions(-)
    

    同样的基本信息,但更紧凑——它仍然让你看到相对改动,和改动了哪些文件。


    示例

    显示整个提交历史记录,但跳过合并。

    git log --no-merges
    
    commit c5f8a258babf5eec54edc794ff980d8340396592
    Author: maxsu 
    Date:   Wed Jul 12 22:07:59 2017 +0800
    
        commit a new file: newfile.txt
    ... ...
    


    显示自 v2.6.12 版以来所有提交更改 include/scsi 或 drivers/scsi 子目录中的任何文件的所有提交。

    git log master include/scsi drivers/scsi
    


    显示最近两周的更改文件gitk--是必要的,以避免与名为 gitk 的分支混淆。

    git log --since="2 weeks ago" -- gitk
    


    显示 test 分支中尚未在 release 分支中的提交,以及每个提交修改的路径列表。

    git log --name-status release..test
    


    显示更改 builtin/rev-list.c 的提交,包括在文件被赋予其现有名称之前发生的提交。

    git log --follow builtin/rev-list.c
    


    显示在任何本地分支中的所有提交,但不包括任何远程跟踪分支机构的起始点(origin 不具有)。

    git log --branches --not --remotes=origin
    


    显示本地主服务器中的所有提交,但不显示任何远程存储库主分支。

    git log master --not --remotes=*/master
    


    显示历史,包括变化差异,但仅从“主分支”的角度来看,忽略来自合并分支的提交,并显示合并引入的变化的完全差异。只有当遵守在一个整合分支上合并所有主题分支的严格策略时,这才有意义。

    git log -p -m --first-parent
    


    显示文件 main.c 中的函数 main()随着时间的推移而演变。

    git log -L '/int main/',/^}/:main.c
    


    将显示最近三次的提交。

    git log -3
    


    根据提交 ID 查询日志。

    # 查询 ID (如:6bab70a08afdbf3f7faffaff9f5252a2e4e2d552)之前的记录,包含commit
    git log commit_id 
    
    # 查询 commit1 与 commit2 之间的记录,包括commit1和commit2
    git log commit1_id commit2_id 
    
    # 同上,但是不包括 commit1
    git log commit1_id..commit2_id 
    

    其中,commit_id 可以是提交哈希值的简写模式,也可以使用 HEAD 代替。HEAD 代表最后一次提交,HEAD^为最后一个提交的父提交,等同于 HEAD~1,HEAD~2 代表倒数第二次提交--pretty按指定格式显示日志信息,可选项有:oneline,short,medium,full,fuller,email,raw以及format:,默认为 medium,可以通过修改配置文件来指定默认的方式。

    git log (--pretty=)oneline
    


    th>选项
    常见的 format 选项
    说明
    %H提交对象(commit)的完整哈希字串
    %h提交对象的简短哈希字串
    %T树对象(tree)的完整哈希字串
    %t树对象的简短哈希字串
    %P父对象(parent)的完整哈希字串
    %p父对象的简短哈希字串
    %an作者(author)的名字
    %ae作者的电子邮件地址
    %ad作者修订日期(可以用-date=选项定制格式)
    %ar作者修订日期,按多久以前的方式显示
    %cn提交者(committer)的名字
    %ce提交者的电子邮件地址
    %cd提交日期
    %cr提交日期,按多久以前的方式显示
    %s提交说明
    注:作者是指最后一次修改文件的人;而提交者是指提交该文件的人。


    git log --pretty=format:"%an %ae %ad %cn %ce %cd %cr %s" --graph
    


    • --mergs:查看所有合并过的提交历史记录。
    • --no-merges:查看所有未被合并过的提交信息。
    • --author=someonet:查询指定作者的提交记录。
    git log --author=maxsu
    


    • --since--affter:仅显示指定时间之后的提交(不包含当前日期)。
    • --until--before:仅显示指定时间之前的提交(包含当前日期)。
    git log --before={3,weeks,ago} --after={2018-04-18}
    

    上篇:git remote

    下篇:git describe