如何查看 Git 日志?
Git 是最流行的版本控制系统,它将随着时间的推移对我们的项目所做的更改记录在一个称为存储库的特殊数据库中。我们可以查看我们的项目,看看谁在何时以及为什么做了什么改变,如果我们搞砸了,我们可以很容易地将我们的项目恢复到早期的状态。在本文中,我们将看到用于检查 git 日志的各种命令,即您可以说的存储库或项目的历史记录,这些命令的核心含义是什么,以及它们的用例是什么,哪个命令用于哪个场景。
关键术语:
- 日志:它是存储库中完成的所有提交的记录。
- 提交:提交是 git 存储库在某个时间点的快照。
- 提交 ID:它是一个 40 个字符的十六进制值,它是 git 每次提交到我们的存储库时生成的唯一标识符。
那么让我们开始这篇文章。用于使用 git 浏览历史的最著名和最常用的命令是 git logs。那么现在让我们深入了解一下 git log 是什么以及它是如何工作的。
Git 日志
它以多行显示在该存储库中进行的所有提交以及提交 ID、作者姓名、日期和提交消息。如果您在 git log 中有多个日志,那么要退出 git log 命令,请按 q。因此,现在如果您想将日志限制为 n 次提交。例如,如果您只想在 git 日志中查看 1 次提交,或者您想查看 2 次提交,或者它可以是任意数字,具体取决于您在 git 存储库中完成的提交总数。该命令将是git log -n其中 n 表示您希望查看日志的最多提交次数。

使用 git 日志

在 git log 中传递一个参数,使其持续到 n 次提交
更漂亮的日志:
要以非常装饰、整洁和干净的方式查看日志,我们使用以下命令。
git log --decorate --oneline --graph
由于这是一个相当大的命令,我们可以为这个命令分配一个别名
git config --global alias.name_of_alias "command_name"
所以现在让我们把这个命令分解成几个部分git config –globa l 这是用来让别名在 git 中被全局识别为命令alias.name_of_alias用于给别名命名,然后“ command_name”是其中的命令你正在创建一个别名,这个命令应该用双引号写。现在要在git alias_name中使用命令类型的别名,如果您想查看存储库中所有内容的历史记录,请将–all作为命令中的参数传入。

使用命令 git log –decorate –oneline –graph

为命令 git log –decorate –oneline –graph 制作别名

使用命令的别名

检查存储库中所有内容的历史记录
着色日志:
要以彩色方式查看日志或自定义日志输出格式,我们使用以下命令
git log --graph ---pretty=format: '%C(red)%h%Creset -%C(yellow)%d%Creset %s %C(green)(%cr)%C(yellow)<%an>%Creset'
所以现在让我们把这个命令分解成几个部分来了解它的核心含义。在这里您可以看到允许我们自定义日志输出格式的格式选项。这个命令中有很多参数,所以让我们以表格的方式理解它们,在这里我制作了一个表格,其中包含每个参数的详细信息。 Parameter Details%C colors the output that comes after it %h denotes the first some letters of the commit ID %H denotes the complete commit id %Creset resets color to the default terminal color %d ref names or simple names %s denotes the commit message %cr committer date %an author-name

使用上面的命令自定义我们自己的日志输出格式
我们也可以给这个命令起一个别名,以避免写这个大命令。用于创建别名的命令与我们在创建别名以装饰方式显示日志输出格式时使用的命令相同。使用的命令是:
git config --global alias.alias_name "Command_name"

使用 git alias_name
在线日志:
这将在一行中显示日志输出。但它只首先显示提交 ID 和提交消息的某些部分。

使用 git log –oneline 和 git log online -n
因此,现在如果您想将日志限制为 n 次提交。例如,如果您只想在 git 日志中查看 1 次提交,或者您想查看 2 次提交,或者它可以是任意数字,具体取决于您在 git 存储库中完成的提交总数。该命令将是git log –oneline -n 其中 n 表示您希望查看日志的最多提交次数。
日志搜索:
所以日志搜索实际上将我们文件中写入的文本与提交 id 匹配,以便我们知道文件中的数据何时是这个字符串say hello 然后哪些提交 id 与这个字符串匹配。所以日志搜索中有一些参数或选项可以让我们的搜索变得容易。因此,让我们一一探索。
- git log -S 字符串:这里,S 代表搜索, 字符串是一组字符,即一些文本,例如:- hello cloud this all statement is a 字符串。因此,此命令显示与字符串关联的最新提交 id。但是,如果您想查看与此特定字符串关联的所有提交 id,那么我们在此命令中使用不同的参数。
- git log -G 字符串:这里 G 代表一个组,它显示与字符串关联的所有提交 id。

使用 git log -s 字符串和 git log -g 字符串
git shortlog:基本上它以以下语法显示日志输出格式:
Committer1(提交数)
- 提交消息 1
- 提交消息 2
- 提交消息 n
其中 n 表示作者提交的最后一条提交消息号
Committer2(提交数)
- 提交消息 1
- 提交消息 2
- 提交消息 n
其中 n 表示作者提交的最后一条提交消息号

使用 git 短日志
在这里,您只能看到我的姓名、提交次数和这些提交的提交消息,因为在我的情况下,我只在我的存储库中工作,这就是为什么它会显示我的姓名、提交次数和与之关联的提交消息。但是在大型项目中,有很多开发人员在一个项目上工作,每当他们修改某些内容或在项目中进行任何更改时,他们都会提交这些更改,然后您可以看到很多提交者以及他们的提交数量和提交消息。在 git shortlog 中,命令中还有许多可用的参数或选项,让我们了解它们。 Parameter Details-s or –summary It displays the number of commits and the committer’s name -e or –email It displays the committer’s name, number of commits, commit messages along with the mail id -n or –numbered It sorts the output by the number of commits instead of alphabetically by committer’s name

使用 gitshortlog -s ,git shortlog -e , git shortlog -n
git grep
因此 git grep 的工作原理是,如果您想获取文件中特定字符串的位置,或者特定字符串是否与特定提交 id 相关联。在这种情况下, git grep 是一个非常有用的命令。在检查文件中是否存在字符串以及检查提交 id 中的特定字符串时,您必须在命令中传递特定字符串,并在双引号中提及该字符串。

使用 git grep
对于文件中的一系列行:该命令将与 git log 相同,只是我们必须传入一些新参数以检查文件中的一系列行。命令是:
git log -L starting_line_number,ending_line_number:file_name here -L denotes the line range for a file.

使用 git log -L starting_line_number,ending_line_number:file_name
过滤日志:
在这个概念中,我们将看到命令中用于过滤日志的各种参数。
- 有几天: git log –after 'mention the days ago'
- 例如: git log --after '4 days ago'
- 对于特定日期: git log –after year_number-month_number-date_number
- 作者姓名: git log –author=”Author_name”
我们也可以使用 此命令中的since子句而不是 after。

使用 git log –after 'number_of_days days ago'

使用 git log –after year_number-month_number-Date_number

使用 git log --author=”author-name”
内联更改:
如果我们的用例是查看内联更改的日志,我们使用参数 -p 或 –patch 来查看内联更改的日志。

使用 git log –patch
提交的文件:
如果您想查看提交正在提交的日志中的文件。我们使用命令
git log --stat

使用 git log –stat
提交的内容:
如果你想查看提交的内容,你可以使用一个简单的命令
git show commit-id

使用 git 显示提交 ID
提交者名称和提交后的时间
现在,如果您想查看提交者姓名和自他在线提交以来的时间。我们使用以下命令
git log --oneline --decorate --source --pretty=format:'"%Cblue %h %Cgreen %ar %Cblue %an %C(yellow) %d %Creset %s"' --all --graph

上述命令的输出