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
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
打印统计(Print Statistics)
We may need to print information about the commit in details. We will use --stat
option.
我们可能需要详细打印有关提交的信息。 我们将使用--stat
选项。
$ git log --stat
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
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
We can see that specific commit provides diff information in detail.
我们可以看到特定的提交详细提供了差异信息。
显示/打印特定的提交统计信息(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
作者分组提交(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
显示作者提交编号(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
按提交编号对作者进行排序(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
漂亮的印刷品(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"
按作者筛选(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 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 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"
We can also use --before
where commits created before specified date will be printed.
我们也可以使用--before
,在指定日期之前创建的提交将被打印。
按消息过滤 (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 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"
按提交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
仅列出合并(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命令