git log 命令用法实例 (1)

使用 git log 命令查看提交记录时,默认打印commit hash值、做者、提交日期、和提交信息。若是想要查看更多内容,能够提供不一样的参数来指定查看的信息。具体实例说明以下。git

查看提交记录具体的改动

执行 git log 命令会打印提交信息,默认不会列出具体的改动内容。能够使用 git log -p 命令来显示具体的改动内容。查看 man git-add 对 -p 选项说明以下:express

-p, -u, --patch
Generate patch (see section on generating patches).code

即,git log -p 命令默认以patch的形式来显示改动内容,会显示修改前、修改后的对比。ci

git log -p 后面还能够提供 commit hash 值来从指定的 commit 开始查看,但不是只查看这个 commit 的改动。例如,git log -p 595bd27 命令是从 595bd27 这个 commit 开始显示代码修改,继续往下翻页,能够看到后面的 commit 改动。hash

若是只想查看指定 commit 的改动,能够使用 git show 命令。例如,git show 595bd27 只显示出 595bd27 这个 commit 自身的改动,翻看到最后就结束打印,不会继续往下显示后面 commit 的改动。it

查看提交记录改动的文件名

使用 git log --name-status 命令来查看提交记录改动的文件名,但不会打印具体的改动,方便查看改动了哪些文件。 查看 man git-log 对 --name-status 的说明以下:io

--name-status
Show only names and status of changed files.class

只查看前面几条提交信息

使用 git log 命令查看使用提交记录,会显示所有的提交信息。若是只想查看前面几条提交信息,能够执行 git log -<number> 命令,number 参数值是数字,指定要查看多少条信息,例如 一、二、3 等。查看 man git-log 对 -<number> 选项说明以下:file

-<number>, -n <number>, --max-count=<number>
Limit the number of commits to output.grep

例如,git log -3 命令会只列出前面三条提交信息。注意,这个命令并非只列出第三个提交信息。
能够把 -<number> 选项和其余选项结合使用。例如 git log -p -3 命令只查看前面三条提交信息的具体改动。

查找特定的commit信息

使用 git log --grep=<pattern> 命令在commit信息中查找指定的内容。查看 man git-log 对 --grep 的说明以下:

--grep=<pattern>
Limit the commits output to ones with log message that matches the specified pattern (regular expression). With more than one --grep=<pattern>, commits whose message matches any of the given patterns are chosen (but see --all-match).

这里说的commit信息指的是在执行 git commit 命令时填写的信息,不包含文件改动的内容。
例如,为了方便标识提交的修改对应哪一个模块,要求在commit信息里面写上模块名,假设应用UI代码的模块名是APPLICATION_UI,就能够使用 git log --grep=APPLICATION_UI 来过滤出全部 APPLICATION_UI 模块的提交历史。

相关文章
相关标签/搜索