ย้อนดูบรรทัดที่ถูกลบ ด้วย Reverse Git Blame

Teerayut Hiruntaraporn
2 min readMar 26, 2022

--

git blame เป็นคำสั่งใน git ที่ช่วยในการดูว่า แต่ละบรรทัดถูกเพิ่มเข้ามาเมื่อไหร่ โดยใคร เช่น

$git blame test.txt 
^57fafa5 (User1 2022-03-26 18:23:07 +0700 1) test1
^57fafa5 (User1 2022-03-26 18:23:07 +0700 2) test2
^57fafa5 (User1 2022-03-26 18:23:07 +0700 3) test3
0348bd9e (User1 2022-03-26 18:23:37 +0700 4) test4
0348bd9e (User1 2022-03-26 18:23:37 +0700 5) test7
0348bd9e (User1 2022-03-26 18:23:37 +0700 6) test8
0348bd9e (User1 2022-03-26 18:23:37 +0700 7) test9
92405550 (User1 2022-03-26 18:24:33 +0700 8) test10
92405550 (User1 2022-03-26 18:24:33 +0700 9) test11
92405550 (User1 2022-03-26 18:24:33 +0700 10) test12
92405550 (User1 2022-03-26 18:24:33 +0700 11) test13
92405550 (User1 2022-03-26 18:24:33 +0700 12) test14
$

อย่างตัวอย่างนี้ เราก็จะเห็นว่ามีการปรับข้อมูล อยู่ 3 commit แต่ละช่วงเพิ่มอะไรมาบ้าง

อย่างไรก็ตามก็ยังมีปัญหาว่า แล้ว ถ้าเราอยากทราบว่าในไฟล์นั้นมีบรรทัดที่ถูกลบไป แล้วมันลบไปตอนไหน เราจะทำอย่างไร

อย่างแรกสุดคือ การใช้ git log -c

❯ git log -c -S’test5' test.txt 
commit 86211f205c6fd13840e90af039c2814e04d6d664
Author: User1 <User1@mail.net>
Date: Sat Mar 26 18:24:04 2022 +0700
remove somediff — git a/test.txt b/test.txt
index 6f33d7d..fc35d96 100644
— — a/test.txt
+++ b/test.txt
@@ -2,8 +2,6 @@ test1
test2
test3
test4
-test5
-test6
test7
test8
test9
commit 0348bd9ef65c89b53b69803454dfe9606e1c7fed
Author: User1 <User1@mail.net>
Date: Sat Mar 26 18:23:37 2022 +0700
updatediff — git a/test.txt b/test.txt
index ff8f2b6..6f33d7d 100644
— — a/test.txt
+++ b/test.txt
@@ -1,5 +1,10 @@
test1
test2
test3
-tset4
+test4
+test5
+test6
+test7
+test8
+test9

โดย -S จะเป็นการค้นหาคำที่เป็นเนื้อหาใน commit ส่วน -c มาจาก — dif-merges=combined ซึ่งจะเป็นการแสดง ค่าความแตกต่างใน commit นั้นๆ ออกมา

จากคำสั่งนี้ก็สามารถที่จะเห็นภาพว่า โดยตัดไปตอนไหน ทีนี้ ถ้าอยากเห็นภาพเป็นภายในไฟล์เลย เพราะบางครั้งการแสดง diff มันก็ไม่ได้ชัดเจนว่าใช่ตำแหน่งที่เราต้องการหรือไม่

ตรงนี้เราสามารถใช้ git blame แบบ reverse ช่วยได้ครับ โดยสั่งว่า

git blame — reverse <commit ที่ยังมีไฟล์อยู่> <filename>

เช่น

❯ git blame --reverse 0348bd9ef65c89b53b69803454dfe9606e1c7fed..HEAD  test.txt
92405550 (User1 2022-03-26 18:24:33 +0700 1) test1
92405550 (User1 2022-03-26 18:24:33 +0700 2) test2
92405550 (User1 2022-03-26 18:24:33 +0700 3) test3
92405550 (User1 2022-03-26 18:24:33 +0700 4) test4
^0348bd9 (User1 2022-03-26 18:23:37 +0700 5) test5
^0348bd9 (User1 2022-03-26 18:23:37 +0700 6) test6
92405550 (User1 2022-03-26 18:24:33 +0700 7) test7
92405550 (User1 2022-03-26 18:24:33 +0700 8) test8
92405550 (User1 2022-03-26 18:24:33 +0700 9) test9
86211f20 (User1 2022-03-26 18:24:04 +0700 10)

โดย commit ที่มี ^ ก็เป็น ตัวเริ่ม

ทีนี้ในรายการจะเปลี่ยนรายละเอียดเล็กน้อย จากเดิม คือ บรรทัดนั้นถูกปรับปรุง เมื่อไหร่ commit อะไร

จะกลายเป็น บรรทัดนั้นหน้าตามเป็นแบบนี้ จนถึง commit สุดท้าย เมื่อไหร่

อย่างกรณีของ บรรทัด test5 นั่นคือ commit ก่อนที่จะโดนลบนั่นเอง

ซึ่งเราก็จะสามารถไปนั่งดูบริบทย้อนกลับได้ ว่า เราลบไปทำไมนะ ได้แล้ว

--

--

Teerayut Hiruntaraporn
Teerayut Hiruntaraporn

No responses yet