1、git log 命令的基本用法

git log 可以让我们查看提交历史,git log 默认会输出 commit hash,author,date,commit message。如下所示:

$ git log
commit a8d08c1b60c0cbaa0be56378aa891726c45f0a49
Author: Tim <xxx@mail.com>
Date:   Fri Oct 28 18:58:39 2022 +0800

    modify some code in xxx section

2、git log 命令的参数介绍

git log 命令有众多的参数,选择合理的参数,可以方便查看过往的仓库提交历史记录。主要的参数有:--oneline--stat--author--after--before,还有-p-n。下文将给大家详细介绍一下。

--oneline 参数:简化默认的输出

--oneline 参数用于简化 git log 的默认的输出,仅仅输出 commit hash 前7个字符串和 commit message。如下所示:

$ git log --oneline
658ac24 modify some code in xxx section
9d05208 modify some code in xxx section 
cb53d27 modify some code in xxx section

--stat 参数:增加增删的统计数据

--stat 参数是在 git log 的基础上输出文件增删改的统计数据,方便我们查看仓库的改动情况。如下所示:

$ git log --stat
commit a8d08c1b60c0cbaa0be56378aa891726c45f0a49 
Author: Tim <xxx@mail.com>
Date:   Fri Oct 28 18:58:39 2022 +0800

    modify some code in xxx section

 lib/common/Localized.dart         |   2 +-
 lib/pages/calendar_plan_page.dart | 470 ++++++++++++++++++++---------------
 2 files changed, 248 insertions(+), 224 deletions(-)

--author 参数:过滤作者

在团队合作开发过程中,如果只想查看某个小伙伴提交的代码,怎么办呢?此时可以加上 --author 参数用来过滤 commit,限定输出给定的用户,如下所示:

$ git log --author="Tim"
commit 84b86471ea12bd1f5994d878a318dcdead1137e9
Author: Tim <xxx@mail.com>
Date:   Wed Dec 19 17:29:25 2022 -0800

    modify some code in xxx section

--after和--before 参数:限定指定日期范围的日志

如果我们想看某段时间内的提交历史,可以使用 --after--before 参数,十分好用,如下所示。不过,需要注意日期的格式,是10-1-2022这样的形式,国外风格的日期表示形式。

$ git log --after '10-1-2022'
commit a8d08c1b60c0cbaa0be56378aa891726c45f0a49 (HEAD -> dev, origin/dev, origin/HEAD)
Author: Tim <xxx@mail.com>
Date:   Fri Oct 28 18:58:39 2022 +0800

    modify some code in xxx section
...

-p 参数:输出每次提交的内容差异

-p 参数很常用,它能控制输出每个 commit 具体修改的内容,输出的形式以 diff 的形式给出。如下所示:

$ git log -p
commit a8d08c1b60c0cbaa0be56378aa891726c45f0a49
Author: Tim <xxx@mail.com>
Date:   Fri Oct 28 18:58:39 2022 +0800

    modify some code in xxx section

diff --git a/lib/common/Localized.dart b/lib/common/Localized.dart
index c870ab8..1992ccf 100644
--- a/lib/common/Localized.dart
+++ b/lib/common/Localized.dart

-n 参数:限定log输出的条数

如果想看最近的 n 条提交日志,怎么办?很简单。直接在 log 命令之后,加 -n 参数即可,n 表示用户要输出的数量.

$ git log -n 3 
commit a8d08c1b60c0cbaa0be56378aa891726c45f0a49
Author: Tim <xxx@mail.com>
Date:   Fri Oct 28 18:58:39 2022 +0800

    modify some code in xxx section
...

3、小结

git log 命令在日常工作中很常用,主要的参数有:--oneline--stat--author--after--before,还有-p-n。希望大家能够掌握,从而在工作中提升效率。

标签: none

[网站公告]-[2024年兼职介绍]


添加新评论