【Git 学习笔记_04】gitk 命令与 git log 其他参数的使用

1.7 用 gitk 查看提交历史

# make sure you have gitk installed
$ which gitk
/usr/bin/gitk
# Sync the commit ID
$ git checkout master && git reset --hard 13dcad
# bring up the gitk interface, --all to see everything
$ gitk --all &

实测结果:
在这里插入图片描述


1.8 在历史版本中查找 commit 版本

# look at the entire history and search every commit that has "Performance" in its commit message:
$ git log --grep "Performance" --oneline --all

上述命令查找 Git 库内所有分支、所有提交注释中,包含 Performance 字样的 commit 版本,并单行显示。注意,这里的 --grep 参数是 区分 大小写的。

此外,也可以在 gitk 命令中查找指定关键字,gitk 会自动高亮匹配:

在这里插入图片描述

1.9 在历史版本的代码中搜索信息

有时候,仅仅在提交注释中进行检索并不能满足实际工作需要,比如要定位哪些提交版本改动了某个方法或变量时,就需要借助 git log 的其他参数实现了。

练习:以 JGit 项目为例,找出满足以下条件的所有提交版本信息:

  1. SHA-1 指定为 b14a939
  2. 变更的一行中包含 "isOutdated" 方法
  3. 单行显示结果

执行命令如下:

$ git checkout master && git reset --hard b14a939
$ git log -G "isOutdated" --oneline
c9e4a7855 Add isOutdated method to DirCache
797ebba30 Add support for getting the system wide configuration
4c14b7623 Make lib.Repository abstract and lib.FileRepository its implementation
c9c57d34d Rename Repository 'config' as 'repoConfig'
5c780b387 Fix unit tests using MockSystemReader with user configuation
cc905e7d4 Make Repository.getConfig aware of changed config

参数 -G 实现的是在 文件变更的增量补丁 中查找。只要提交的版本中,某一行代码新增或删除过包含关键词 isOutdated 的情况,就会被检索到。

与之类似的另一个参数是 -S,其唯一区别在于,-S 检索的是指定关键词在某版本中的 出现次数 的变化情况。对比 -S 的执行结果:

$ git checkout master && git reset --hard b14a939
$ git log -S "isOutdated" --oneline
c9e4a7855 Add isOutdated method to DirCache
797ebba30 Add support for getting the system wide configuration
4c14b7623 Make lib.Repository abstract and lib.FileRepository its implementation
5c780b387 Fix unit tests using MockSystemReader with user configuation
cc905e7d4 Make Repository.getConfig aware of changed config

可以看到 commit ID 为 c9c57d34d 仅在 -G 时出现,在 -S 中未曾出现。借助命令 git show 查看该版本详情( grep -C4 会展示目标代码行前后各 4 行的情况):

$ git show c9c57d3 | grep -C4 "isOutdated"
@@ -417,14 +417,14 @@ public FileBasedConfig getConfig() {
                                throw new RuntimeException(e);
                        }
                }
-               if (config.isOutdated()) {
+               if (repoConfig.isOutdated()) {
                                try {
-                                       loadConfig();
+                                       loadRepoConfig();
                                } catch (IOException e) {

原来是对象的重命名导致的变更,导致这一行出现了关键词,但出现次数并不受影响。

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安冬的码畜日常

您的鼓励是我持续优质内容的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值