git - 揪出来改你代码的人

侯佳奇的博客 / 2024-03-05 / 原文

先查找某个文件的历史修改记录,然后根据关键信息匹配对应的更改,根据具体情况显示更改的前N行,就能查到commit信息了!

比如我要查看是谁删掉了我requirements.txt文件的这行代码openpyxl==3.1.2

执行下面的命令就查到commit信息了

 ➜  e100_v2 git:(3.3/dev) git log -p -- requirements.txt | grep -B 15 '\-.*openpyxl==3.1.2'

commit 11f04311b919a175c87016aabfdbdcc634849cd7
Author: 侯佳奇 <houjiaqi@3dnest.cn>
Date:   Tue Aug 29 14:52:20 2023 +0800

    feat:done

diff --git a/requirements.txt b/requirements.txt
index fa6818af..321fb562 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -71,4 +71,5 @@ pycryptodome==3.15.0
 pillow==9.2.0
 python-dateutil==2.8.2
 SensorsAnalyticsSDK==1.10.3
-openpyxl==3.1.2

解释一下:

git log -- <filepath> 可以查看某个文件的修改记录 -p 参数会显示每次提交的内容

利用grep进行关键字搜索, -B <n> 参数显示结果行+结果行前n行的内容.

上面的命令结合grep做的操作. 那么git本身能不能对修改内容进行模糊匹配?

查了下有的, git log -G 可以做正则匹配

➜  e100_v2 git:(3.3/dev) git log -G'openpyxl==3.1.2' -p
commit 11f04311b919a175c87016aabfdbdcc634849cd7
Author: 侯佳奇 <houjiaqi@3dnest.cn>
Date:   Tue Aug 29 14:52:20 2023 +0800

    feat:done

diff --git a/requirements.txt b/requirements.txt
index fa6818af..321fb562 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -71,4 +71,5 @@ pycryptodome==3.15.0
 pillow==9.2.0
 python-dateutil==2.8.2
 SensorsAnalyticsSDK==1.10.3
-openpyxl==3.1.2
\ No newline at end of file
+openpyxl==3.1.2
+minio==7.1.16

commit b3aa26b4d338cf63b5f46f848e27f9ba0e5a7e65
Author: gaozengzeng <gaozengzeng@3dnest.cn>
Date:   Fri Aug 18 17:46:09 2023 +0800

    feat: 添加导出功能

diff --git a/requirements.txt b/requirements.txt
index 597d72af..fa6818af 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -70,4 +70,5 @@ zope.interface==5.4.0
 pycryptodome==3.15.0
 pillow==9.2.0
 python-dateutil==2.8.2
-SensorsAnalyticsSDK==1.10.3
\ No newline at end of file
+SensorsAnalyticsSDK==1.10.3
+openpyxl==3.1.2
\ No newline at end of file
(END)

这个查出了添加和删除时的两个commit, 有没有办法只显示添加的或者删除的?

...