Git系列讲解(四):提交记录之git log与git blame的使用

2023-10-17 18:50

本文主要是介绍Git系列讲解(四):提交记录之git log与git blame的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

git log命令提供了很多子选项,善于使用这些选项可以加快工程师调查问题的效率,下面以ccache的项目举例讲解。

目录

  • 1. git log
  • 2. pretty模式 [ git log --pretty= ]
    • 2.1 git log --pretty=email
    • 2.2 git log --pretty=full
    • 2.3 git log --pretty=fuller
    • 2.4 git log --pretty=medium
    • 2.5 git log --pretty=oneline
    • 2.6 git log --pretty=raw
    • 2.7 git log --pretty=short
    • 2.8 git log --pretty=format:
  • 3. 单行输出
    • 3.1 git log --oneline
    • 3.2 git log --oneline <file\>
  • 4. 输出每笔提交内容变更量统计
    • 4.1 git log --stat
    • 4.2 git log --stat <file\>
  • 5. 输出所有提交的变更内容
    • 5.1 git log -p
    • 5.2 git log -p <file\>
  • 6. 显示某笔提交的变更情况 [ git show --stat commitID ]
  • 7. 显示某笔提交的变更内容 [ git show commitID ]
  • 8. 指定日期格式 [ git log --date= ]
  • 9. 依据某条件筛选显示
  • 10. 输出文件每行的提交记录
    • 10.1 git blame <file\>
    • 10.2 git blame -L <开始行数>,<结束行数> <file\>

1. git log

完整的commit id,作者,提交日期,注释

在这里插入图片描述


2. pretty模式 [ git log --pretty= ]

这个pretty可以理解为对git log默认输出的美化,包含几个子选项,使用者可依据自己的需求选择其一使用。

参数说明
email以邮件的形式输出日志,方便写邮件的时候用
full与默认格式相比,少了日期,多了提交者
fuller与默认格式相比,多了作者日期,提交者,提交日期
medium与默认格式一样
oneline每笔提交单行显示,和git log --oneline相比,commit id是全的
raw与默认格式相比,少了日期,多了提交者,tree,parent,gpg签名
short只有作者和提交注释,且注释是一行显示
format:自定义显示输出格式

2.1 git log --pretty=email

在这里插入图片描述

2.2 git log --pretty=full

在这里插入图片描述

2.3 git log --pretty=fuller

在这里插入图片描述

2.4 git log --pretty=medium

在这里插入图片描述

2.5 git log --pretty=oneline

在这里插入图片描述

2.6 git log --pretty=raw

在这里插入图片描述

2.7 git log --pretty=short

在这里插入图片描述

2.8 git log --pretty=format:

在这里插入图片描述

format的选项太多了,这里就不细讲了,下面是从手册中摘抄的说明,读者有时间可以根据自己的需要进行学习。

·   %H: commit hash
·   %h: abbreviated commit hash
·   %T: tree hash
·   %t: abbreviated tree hash
·   %P: parent hashes
·   %p: abbreviated parent hashes
·   %an: author name
·   %aN: author name (respecting .mailmap, see git-shortlog(1) or git-blame(1))
·   %ae: author email
·   %aE: author email (respecting .mailmap, see git-shortlog(1) or git-blame(1))
·   %ad: author date (format respects --date= option)
·   %aD: author date, RFC2822 style
·   %ar: author date, relative
·   %at: author date, UNIX timestamp
·   %ai: author date, ISO 8601-like format
·   %aI: author date, strict ISO 8601 format
·   %cn: committer name
·   %cN: committer name (respecting .mailmap, see git-shortlog(1) or git-blame(1))
·   %ce: committer email
·   %cE: committer email (respecting .mailmap, see git-shortlog(1) or git-blame(1))
·   %cd: committer date (format respects --date= option)
·   %cD: committer date, RFC2822 style
·   %cr: committer date, relative
·   %ct: committer date, UNIX timestamp
·   %ci: committer date, ISO 8601-like format
·   %cI: committer date, strict ISO 8601 format
·   %d: ref names, like the --decorate option of git-log(1)
·   %D: ref names without the " (", ")" wrapping.
·   %e: encoding
·   %s: subject
·   %f: sanitized subject line, suitable for a filename
·   %b: body
·   %B: raw body (unwrapped subject and body)
·   %N: commit notes
·   %GG: raw verification message from GPG for a signed commit
·   %G?: show "G" for a good (valid) signature, "B" for a bad signature, "U" for a good signature with unknown validity, "X" for a good signature that has expired, "Y" for a goodsignature made by an expired key, "R" for a good signature made by a revoked key, "E" if the signature cannot be checked (e.g. missing key) and "N" for no signature
·   %GS: show the name of the signer for a signed commit
·   %GK: show the key used to sign a signed commit
·   %gD: reflog selector, e.g., refs/stash@{1} or refs/stash@{2 minutes ago}; the format follows the rules described for the -g option. The portion before the @ is the refname as given onthe command line (so git log -g refs/heads/master would yield refs/heads/master@{0}).
·   %gd: shortened reflog selector; same as %gD, but the refname portion is shortened for human readability (so refs/heads/master becomes just master).
·   %gn: reflog identity name
·   %gN: reflog identity name (respecting .mailmap, see git-shortlog(1) or git-blame(1))
·   %ge: reflog identity email
·   %gE: reflog identity email (respecting .mailmap, see git-shortlog(1) or git-blame(1))
·   %gs: reflog subject
·   %Cred: switch color to red
·   %Cgreen: switch color to green
·   %Cblue: switch color to blue
·   %Creset: reset color
·   %C(...): color specification, as described under Values in the "CONFIGURATION FILE" section of git-config(1). By default, colors are shown only when enabled for log output (bycolor.diff, color.ui, or --color, and respecting the auto settings of the former if we are going to a terminal).  %C(auto,...)  is accepted as a historical synonym for the default(e.g., %C(auto,red)). Specifying %C(always,...) will show the colors even when color is not otherwise enabled (though consider just using `--color=always to enable color for the wholeoutput, including this format and anything else git might color).  auto alone (i.e.  %C(auto)) will turn on auto coloring on the next placeholders until the color is switched again.
·   %m: left (<), right (>) or boundary (-) mark
·   %n: newline
·   %%: a raw %
·   %x00: print a byte from a hex code
·   %w([<w>[,<i1>[,<i2>]]]): switch line wrapping, like the -w option of git-shortlog(1).
·   %<(<N>[,trunc|ltrunc|mtrunc]): make the next placeholder take at least N columns, padding spaces on the right if necessary. Optionally truncate at the beginning (ltrunc), the middle(mtrunc) or the end (trunc) if the output is longer than N columns. Note that truncating only works correctly with N >= 2.
·   %<|(<N>): make the next placeholder take at least until Nth columns, padding spaces on the right if necessary
·   %>(<N>), %>|(<N>): similar to %<(<N>), %<|(<N>) respectively, but padding spaces on the left
·   %>>(<N>), %>>|(<N>): similar to %>(<N>), %>|(<N>) respectively, except that if the next placeholder takes more spaces than given and there are spaces on its left, use those spaces
·   %><(<N>), %><|(<N>): similar to %<(<N>), %<|(<N>) respectively, but padding both sides (i.e. the text is centered)
·   %(trailers[:options]): display the trailers of the body as interpreted by git-interpret-trailers(1). The trailers string may be followed by a colon and zero or more comma-separatedoptions. If the only option is given, omit non-trailer lines from the trailer block. If the unfold option is given, behave as if interpret-trailer’s --unfold option was given. E.g.,%(trailers:only,unfold) to do both.

3. 单行输出

3.1 git log --oneline

git log --pretty=oneline差不多,都是每笔提交一行输出(commitID comment),不同的是git log --oneline的commit id是缩写的。

当第一行注释和第二行注释之间有空行,就只会显示第一行注释,比如下图的最新提交(80a228c3),对照git log默认输出的就只显示了第一行注释。
在这里插入图片描述

3.2 git log --oneline <file>

以单行模式输出某文件的所有提交记录。


4. 输出每笔提交内容变更量统计

4.1 git log --stat

在这里插入图片描述

4.2 git log --stat <file>

输出某文件的所有提交记录及其变更量统计。


5. 输出所有提交的变更内容

5.1 git log -p

输出每笔提交的记录以及其变更内容
在这里插入图片描述

5.2 git log -p <file>

输出某文件的所有提交记录及其变更内容。


6. 显示某笔提交的变更情况 [ git show --stat commitID ]

在这里插入图片描述


7. 显示某笔提交的变更内容 [ git show commitID ]

在这里插入图片描述


8. 指定日期格式 [ git log --date= ]

命令后接下面选项:

选项举例
defaultThu Aug 25 21:15:01 2022 +0200
iso86012022-08-25 21:15:01 +0200
localFri Aug 26 03:15:01 2022
raw1661454901 +0200
relative2 个月前
rfc2822Thu, 25 Aug 2022 21:15:01 +0200
short2022-08-25

9. 依据某条件筛选显示

选项说明
-n显示前n笔提交
--skip=n不显示前n笔提交
--author=<字符串>筛选出与字符串匹配的作者的提交,比如字符串是"Joel",那么会显示作者是"Joel abc"和"Joel def"等。
--committer=<字符串>筛选出与字符串匹配的提交者的提交
--since=<日期>, --after=<日期>显示指定时间之后的提交
--until=<日期>, --before=<日期>显示指定时间之前的提交
--grep=<字符串>显示提交说明中包含指定字符串的提交,见下面案例
-S <字符串>显示添加或删除内容匹配指定字符串的提交,见下面案例
-i忽略大小写进行匹配,可以搭配其他选项一起使用

案例:
现有一个git仓库,master分支包含了笔提交,管理两个文件(1.txt,2.txt)。
在这里插入图片描述

git log --grep="modify 1.txt"

查找commit message中包含"modify 1.txt"的提交。
在这里插入图片描述

git log -S "testvar = 1"

我们知道每笔提交都包含文件内容的变更(增加和删除),-S的作用就是从这些变更内容中找到要查找的内容(testvar = 1),并将其git日志输出来。注意要和--grep区分开来,--grep搜索的是提交信息,而-S是搜索提交中的变更内容(git管理的文件,代码等)

在这里插入图片描述

我们把上面命令稍作修改,变成 git log -S "testvar = 1" --pretty="%H" | xargs git show,这样就可以看到-S筛选出来的三笔提交,其变更内容都是包含了指定内容(testvar = 1)

在这里插入图片描述


10. 输出文件每行的提交记录

10.1 git blame <file>

输出指定文件中每行内容的最近一笔提交信息。
在这里插入图片描述

10.2 git blame -L <开始行数>,<结束行数> <file>

输出某文件从第几行到第几行的最近一笔提交记录
在这里插入图片描述

git blame -L 5,5 <file>              代表第5行的提交记录
git blame <file> | tail -n <行数>     搭配tail命令还可以显示最后多少行的提交记录

这篇关于Git系列讲解(四):提交记录之git log与git blame的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/227288

相关文章

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

一文详解Git中分支本地和远程删除的方法

《一文详解Git中分支本地和远程删除的方法》在使用Git进行版本控制的过程中,我们会创建多个分支来进行不同功能的开发,这就容易涉及到如何正确地删除本地分支和远程分支,下面我们就来看看相关的实现方法吧... 目录技术背景实现步骤删除本地分支删除远程www.chinasem.cn分支同步删除信息到其他机器示例步骤

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

ModelMapper基本使用和常见场景示例详解

《ModelMapper基本使用和常见场景示例详解》ModelMapper是Java对象映射库,支持自动映射、自定义规则、集合转换及高级配置(如匹配策略、转换器),可集成SpringBoot,减少样板... 目录1. 添加依赖2. 基本用法示例:简单对象映射3. 自定义映射规则4. 集合映射5. 高级配置匹

Spring 框架之Springfox使用详解

《Spring框架之Springfox使用详解》Springfox是Spring框架的API文档工具,集成Swagger规范,自动生成文档并支持多语言/版本,模块化设计便于扩展,但存在版本兼容性、性... 目录核心功能工作原理模块化设计使用示例注意事项优缺点优点缺点总结适用场景建议总结Springfox 是

在Spring Boot中集成RabbitMQ的实战记录

《在SpringBoot中集成RabbitMQ的实战记录》本文介绍SpringBoot集成RabbitMQ的步骤,涵盖配置连接、消息发送与接收,并对比两种定义Exchange与队列的方式:手动声明(... 目录前言准备工作1. 安装 RabbitMQ2. 消息发送者(Producer)配置1. 创建 Spr

嵌入式数据库SQLite 3配置使用讲解

《嵌入式数据库SQLite3配置使用讲解》本文强调嵌入式项目中SQLite3数据库的重要性,因其零配置、轻量级、跨平台及事务处理特性,可保障数据溯源与责任明确,详细讲解安装配置、基础语法及SQLit... 目录0、惨痛教训1、SQLite3环境配置(1)、下载安装SQLite库(2)、解压下载的文件(3)、

使用Python绘制3D堆叠条形图全解析

《使用Python绘制3D堆叠条形图全解析》在数据可视化的工具箱里,3D图表总能带来眼前一亮的效果,本文就来和大家聊聊如何使用Python实现绘制3D堆叠条形图,感兴趣的小伙伴可以了解下... 目录为什么选择 3D 堆叠条形图代码实现:从数据到 3D 世界的搭建核心代码逐行解析细节优化应用场景:3D 堆叠图

Springboot如何正确使用AOP问题

《Springboot如何正确使用AOP问题》:本文主要介绍Springboot如何正确使用AOP问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录​一、AOP概念二、切点表达式​execution表达式案例三、AOP通知四、springboot中使用AOP导出