Git blame 查看代码是谁写的

栏目: 编程工具 · 发布时间: 4年前

内容简介:得到的结果,每列分别为

git blame 命令可以查看每行代码的提交详情,包括提交人、时间等信息,成熟的编辑器如 Sublime、Jetbrains 等都集成了这个功能,使用起来非常简单,今天我们来关注下这个命令本身,毕竟在服务器上我们可不能安装这些编辑器。

$ git blame <filename>

Git blame 查看代码是谁写的

得到的结果,每列分别为

提交 sha1   用户名 提交时间                    行数    代码
...
39a22489    (wxnacy 2018-07-26 19:01:55 +0800  6)      def filter(source: dict, *args, **kwargs):
...

这是最简单的语法,我们查看文件的每一行代码的最后提交详情,不过这通常不是我们的目的,一般只需要看某一行或某个范围行,这时可以使用 -L 参数。

$ git blame <filename> -L <start>[,<end>]

查看第 10 行

$ git blame <filename> -L 10,10

查看第 10 行及以后

$ git blame <filename> -L 10

查看第 10 到 20 行

$ git blame <filename> -L 10,20

end 也可以加上正负符号,比如

查看 10 行以后 5 行的数据

$ git blame <filename> -L 10,+5

查看 10 行以前 5 行的数据

$ git blame <filename> -L 10,-5

startend 又不止于数字,它还可以是个正则表达式,如果 start 为正则表达式,则会匹配到 end 行,如果 end 为正则表达式,则从 start 行开始匹配,到匹配行截止,如果 start 或者 end 其中之一有值,则从匹配行开始显示全部内容。

查看正则匹配到 20 行

$ git blame <filename> -L /filter/,20

Git blame 查看代码是谁写的

查看 3 行到正则匹配行

$ git blame <filename> -L 3,/filter/

Git blame 查看代码是谁写的

-L 参数的功能还不止这些,有个更智能的方式是直接通过函数名来查看一个区域块的代码

$ git blame <filename> -L:<func_name>

这是很方便的能力,不过只能识别文件最外层的方法名和类名

$ git blame <filename> -L:filter

Git blame 查看代码是谁写的

完整参数列表

usage: git blame [<options>] [<rev-opts>] [<rev>] [--] <file>

    <rev-opts> are documented in git-rev-list(1)

    --incremental         Show blame entries as we find them, incrementally
    -b                    Show blank SHA-1 for boundary commits (Default: off)
    --root                Do not treat root commits as boundaries (Default: off)
    --show-stats          Show work cost statistics
    --progress            Force progress reporting
    --score-debug         Show output score for blame entries
    -f, --show-name       Show original filename (Default: auto)
    -n, --show-number     Show original linenumber (Default: off)
    -p, --porcelain       Show in a format designed for machine consumption
    --line-porcelain      Show porcelain format with per-line commit information
    -c                    Use the same output mode as git-annotate (Default: off)
    -t                    提交时间显示为时间戳,默认关闭
    -l                    显示完整的 commit sha1,默认关闭
    -s                    Suppress author name and timestamp (Default: off)
    -e, --show-email      显示提交用户的邮箱,默认关闭
    -w                    Ignore whitespace differences
    --indent-heuristic    Use an experimental heuristic to improve diffs
    --minimal             Spend extra cycles to find better match
    -S <file>             Use revisions from <file> instead of calling git-rev-list
    --contents <file>     Use <file>'s contents as the final image
    -C[<score>]           Find line copies within and across files
    -M[<score>]           Find line movements within and across files
    -L <n,m>              Process only line range n,m, counting from 1
    --abbrev[=<n>]        use <n> digits to display SHA-1s

最近更新

Vim 执行 Shell 命令(3)

Python 在 Mac 系统中从剪切板中获取图片

Python 使用 memory_profiler 分析程序内存占用情况

Linux time 命令统计脚本耗时

Python 判断字符串是否为大写及 is 方法延伸

awk 正则表达式中使用参数

SQLAlchemy 解决其他连接修改数据后,查询不到的问题

Python 如何给屏幕打印信息加上颜色

Redis get 报错 a key holding the wrong kind of value

Vim 执行 Shell 命令(2)

最近热读

Go 判断数组中是否包含某个 item

办理北京工作居住证的一些细节

Go 语法错误:Non-declaration statement outside function body

Python f-strings 3.6 版本新增加的字符串格式化功能

Python APScheduler 定时任务

扫码关注公众号,或搜索公众号“温欣爸比” 及时获取我的最新文章 Git blame 查看代码是谁写的


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

精通Java并发编程(第2版)

精通Java并发编程(第2版)

[西] 哈维尔·费尔南德斯·冈萨雷斯 / 唐富年 / 人民邮电出版社 / 2018-10 / 89.00元

Java 提供了一套非常强大的并发API,可以轻松实现任何类型的并发应用程序。本书讲述Java 并发API 最重要的元素,包括执行器框架、Phaser 类、Fork/Join 框架、流API、并发数据结构、同步机制,并展示如何在实际开发中使用它们。此外,本书还介绍了设计并发应用程序的方法论、设计模式、实现良好并发应用程序的提示和技巧、测试并发应用程序的工具和方法,以及如何使用面向Java 虚拟机的......一起来看看 《精通Java并发编程(第2版)》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具