git log命令_如何使用示例列出Git Log命令的提交历史记录?

git log命令

git log命令

Git source code versioning tool provides a lot of features. One of the most important and useful features is log or history. We can use git log command in order to list, filter, view commit history in different ways. In this tutorial we will examine git log command usage in detail with examples.

Git源代码版本控制工具提供了许多功能。 log或历史记录是最重要和最有用的功能之一。 我们可以使用git log命令以不同方式列出,过滤和查看提交历史记录。 在本教程中,我们将通过示例详细检查git log命令的用法。

列表提交历史 (List Commit History)

We will start with git  log command without any parameter. This will list all commit history in a interactive terminal where we can see and navigate.

我们将从不带任何参数的git log命令开始。 这将在交互式终端中列出所有提交历史记录,我们可以在其中查看和导航。

$ git log
List Commit History
List Commit History
列表提交历史

We can see from output that following information about the commit provided.

从输出中我们可以看到有关提供的提交的以下信息。

  • `Commit` number which is a unique hash identifies the commit

    提交编号是唯一的哈希值,用于标识提交
  • `Author` the developer who commit. Also email information is provided

    `Author`提交的开发人员。 还提供电子邮件信息
  • `Date` specifies when the commit occurred

    “日期”指定提交的时间
  • The last line provides note and information about the commit.

    最后一行提供有关提交的注释和信息。

列出每行一次提交 (List One Commit Per Line)

If we need to only list unique part of the commit id with the note provided by author we can use --oneline option which will just print single line about each commit.

如果只需要使用作者提供的注释列出提交ID的唯一部分,则可以使用--oneline选项,该选项将在每次提交时仅打印一行。

$ git log --oneline
List One Commit Per Line
List One Commit Per Line
列出每行一次提交

打印统计(Print Statistics)

We may need to print information about the commit in details. We will use --stat option.

我们可能需要详细打印有关提交的信息。 我们将使用--stat选项。

$ git log --stat
Print Statistics 
Print Statistics
打印统计

We can see from output that extra information like changed file, changed file count, number of lines added , number of lines deleted.

从输出中我们可以看到其他信息,例如更改的文件,更改的文件数,添加的行数,删除的行数。

打印补丁或差异信息 (Print Patch or Diff Information)

If we are interested with the code diff information we need to use -p option. -p option can be used to print path or diff of the files for the commits.

如果我们对代码差异信息感兴趣,则需要使用-p选项。 -p选项可用于打印提交的文件路径或差异。

$ git log -p
Print Patch or Diff Information
Print Patch or Diff Information
打印补丁或差异信息

We see from screenshot that added and removed code is shown clearly. Added code color is green and removed code is red. Also added code lines start with +plus and removed code lines starts with - minus.

从屏幕截图中我们可以清楚地看到添加和删除的代码。 添加的代码颜色为绿色,删除的代码为红色。 添加的代码行也以+加号开头,而删除的代码行以-减号开头。

显示/打印特定提交的详细信息 (Show/Print Specific Commit In Detail)

If we need to look specific commit we need to use git show command. We will also provide the commit id or number we can to print.

如果我们需要查看特定的提交,则需要使用git show命令。 我们还将提供可打印的提交ID或编号。

$ git show b1efd742499b00eef970feeef84dc64f301db61f
Print Specific Commit In Detail
Print Specific Commit In Detail
详细打印特定提交

We can see that specific commit provides diff information in detail.

我们可以看到特定的提交详细提供了差异信息。

LEARN MORE  Git - Ignore Files To Prevent Commit
了解更多Git-忽略文件以防止提交

显示/打印特定的提交统计信息(Show/Print Specific Commit Stats)

If we cant to just print specific commit stat and information we can provide --stat option to the git show command.

如果我们不能只打印特定的提交统计信息和信息,可以向git show命令提供--stat选项。

$ git show --stat b1efd742499b00eef970feeef84dc64f301db61f
Show/Print Specific Commit Stats
Show/Print Specific Commit Stats
显示/打印特定的提交统计信息

作者分组提交(Group Commits By Author)

If we want to inspect the commits according to the author name we need to group commits by author. We can use shortlog command in order to list commits notes grouped by author name.

如果要根据作者姓名检查提交,则需要按作者对提交进行分组。 我们可以使用shortlog命令来列出按作者姓名分组的提交注释。

$ git shortlog
Group Commits By Author
Group Commits By Author
作者分组提交

显示作者提交编号(Show Author Commit Numbers)

If we are interested with the authors commit numbers we need to provide -s options to the shortlog command.This will provides commit numbers for each authors.

如果我们对作者的提交编号感兴趣,我们需要为shortlog命令提供-s选项,这shortlog每个作者提供提交编号。

$ git shortlog -s
Show Author Commit Numbers
Show Author Commit Numbers
显示作者提交编号

按提交编号对作者进行排序(Sort Authors By Commit Numbers)

We can improve previous example and sort authors by their commit numbers. We will add -n too the previous example where final command will be like below.

我们可以改进前面的示例,并按作者编号对作者进行排序。 我们还将在前面的示例中添加-n ,其中最终命令将如下所示。

$ git shortlog -n -s
Sort Authors By Commit Numbers
Sort Authors By Commit Numbers
按提交编号对作者进行排序

漂亮的印刷品(Pretty Print)

We can also customize the log output according to our needs. We can use --pretty option and som parameters to print different log output. In this example we will use %cn for author name %h hash value of commit and %cd for commit time.

我们还可以根据需要自定义日志输出。 我们可以使用--pretty选项和som参数来打印不同的日志输出。 在此示例中,我们将%cn用作作者名%h的提交哈希值,将%cd用作提交时间。

$ git log --pretty="%cn committed %h on %cd"
Pretty Print
Pretty Print
漂亮的印刷品

按作者筛选(Filter By Author)

In some cases we may need to filter commits according to the author name. We will use --author and provide the author name to filter and show only given author. In this example we will filter author named dmiller.

在某些情况下,我们可能需要根据作者姓名过滤提交。 我们将使用--author并提供作者名称以过滤并仅显示给定的作者。 在此示例中,我们将过滤名为dmiller作者。

$ git log --author="dmiller"
Filter By Author
Filter By Author
按作者筛选

按号码筛选(Filter By Number)

If we want to list and print specified number of commit we need to use - with the number we want to print. In this example we will print last 5 commit.

如果我们要列出并打印指定数量的提交,则需要使用-与我们要打印的数量。 在此示例中,我们将打印最后5次提交。

$ git log -5 --oneline
Filter By Number
Filter By Number
按号码筛选

按日期筛选(Filter By Date)

We can also filter according  to date. We will provide the date we want to start listing. We will use --after option and provide the date. Date will be MM-DD-YYYY format. In this example we will list commits those created after 1 December 2018 .

我们还可以根据日期进行过滤。 我们将提供开始上市的日期。 我们将使用--after选项并提供日期。 日期将采用MM-DD-YYYY格式。 在本示例中,我们将列出在2018年12月1日之后创建的提交。

$ git log --after="12-1-2018"
Filter By Date
Filter By Date
按日期筛选

We can also use --before where commits created before specified date will be printed.

我们也可以使用--before ,在指定日期之前创建的提交将被打印。

LEARN MORE  How To Clone Git Branch Command Tutorial with Examples?
了解更多信息如何使用示例克隆Git分支命令教程?

按消息过滤 (Filter By Message)

We can print or list logs by filtering according to the message. We will use --grep option and provide the filter term. We will filter for message http in this example.

我们可以根据消息进行过滤来打印或列出日志。 我们将使用--grep选项并提供过滤条件。 在此示例中,我们将过滤消息http

$ git log --grep="http"
Filter By Message
Filter By Message
依讯息筛选

按文件过滤(Filter By File)

If we are looking for specific file change during commit we can filter for file. We will use -- and provide file names which is expected to be in commit change. In this example we will look file ip.c which is expected to be committed.

如果我们在提交期间正在寻找特定的文件更改,则可以过滤文件。 我们将使用--并提供预期在提交更改中的文件名。 在此示例中,我们将查找预期已提交的文件ip.c

$ git log -- ip.c

按内容过滤 (Filter By Content)

Also we can filter commits according to the commit content. This will be very useful if we want to search and filter for specific change. We will use -S option and provide filter term. In this example we will filter for raw_scan . Keep in mind that this may take some time because it will search in all commits which is not indexed for fast search.

我们也可以根据提交内容过滤提交。 如果我们要搜索和过滤特定的更改,这将非常有用。 我们将使用-S选项并提供过滤条件。 在此示例中,我们将过滤raw_scan 。 请记住,这可能需要一些时间,因为它将搜索所有未索引以快速搜索的提交。

$ git log -S"raw_scan"
Filter By Content
Filter By Content
按内容过滤

按提交ID /哈希范围过滤(Filter By Commit Id/Hash Range)

Commits have their own hash ids. If we want to list range of commits we can provide the start and end commit id where commits between them will be listed.

提交有自己的哈希ID。 如果我们要列出提交的范围,我们可以提供开始和结束提交ID,并在其中列出它们之间的提交。

$ git log b642dc129c4d349a849fb0e..1ba01193725f4c
Filter By Commit Id/Hash Range
Filter By Commit Id/Hash Range
按提交ID /哈希范围过滤

仅列出合并(List Only Merges)

By default merge commits are printed and listed. But if the default behaivour is change with config and we want to list and print merge commits we can use --merge option to list merge commits too.

默认情况下,合并提交被打印并列出。 但是,如果默认行为是使用config更改的,并且我们希望列出并打印合并提交,则也可以使用--merge选项列出合​​并提交。

$ git log --merge

列出没有合并 (List No Merges)

By default merges commits are printed and listed with git log command. If we do not want to list or print then for all operations we can use --no-merges option which will do not show merge commits.

默认情况下,使用git log命令打印并列出合并提交。 如果我们不想列出或打印,那么对于所有操作,我们都可以使用--no-merges选项,该选项不会显示合并提交。

$ git log --no-merge
 

翻译自: https://www.poftut.com/how-to-list-commit-history-with-git-log-command-with-examples/

git log命令

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值