คำสั่ง git log แสดง Record ของ Commits ใน Git Repository โดย Default git log แสดง Commit Hash, Commit Message, และ Commit Metadata อื่นๆ คุณสามารถ Filter Output ของ git log ด้วย Options ต่างๆ
Version Control Systems มี Core Purpose หนึ่ง: Record ว่า Codebase Evolve ตามเวลาอย่างไร ให้ดูว่า Projects Progress อย่างไร รู้ว่าใคร Contribute Project และ Identify ว่ามี Changes อะไรใน Code และเมื่อไหร่
แต่ดู History ที่ Git Repository สร้างจริงยังไง? นี่คือที่ที่ git log Command ช่วย
Tutorial นี้จะอธิบายพร้อม Examples: Basics ของ git log Command และวิธีใช้ Inspect Git Repository
คำสั่ง git log
คำสั่ง git log แสดง List ของ Commits ทั้งหมดที่ทำไป Repository เห็น Hash ของแต่ละ Git Commit, Message ที่ Associated กับแต่ละ Commit, และ Metadata เพิ่มเติม Command นี้ Useful สำหรับ Display History ของ Repository
ในขณะที่ git status Command Focus ที่ Current Working Directory git log ให้ดู History ของ Repository
ตัวอย่าง git log Command
ลอง Example ว่า git log ทำงานยังไง เราทำงาน Repository ชื่อ "demo-repository" ตอนนี้อยากดู List ของ Commits ที่ Push ไป Repository ใช้คำสั่ง:
git log คืน List ของ Commits ทั้งหมดใน Repository ตัวอย่าง Single Commit ที่คืนค่า:
commit 43433c674e3d6c86a889fad222dae179785893cf (HEAD -> master, origin/master, origin/HEAD) Author: James Gallagher <37276661+jamesgallagher432@users.noreply.github.com> Date: Tue Apr 7 13:09:58 2020 +0100 Update index.html
มาวิเคราะห์ Output ทีละขั้นตอน แต่ละ Entry ที่ git log คืนค่ามี:
- Secure Hash Algorithm (SHA) Hash สำหรับ Commit ใช้ Unique Identify แต่ละ Commit ใน Repository ใน Example คือ "43433c674e3d6c86a889fad222dae179785893cf"
- Author ของ Commit ใน Example คือ James Gallagher Email ของ Author ก็ระบุด้วย
- Date ที่ Commit Push ใน Example คือ Tuesday, April 7th
- Message ที่ Associated กับ Commit Commit Message ที่คืนคือ "Update index.html"
Data ทั้งหมดนี้ให้ข้อมูลที่มีประโยชน์เกี่ยวกับ Commits ใน Repository ของเรา
Filtering Output ของ git log
โดย Default git log คืน List ของ Commits ทั้งหมดใน Repository ด้วยโครงสร้างนี้ ตามคุณนึก像ได้ อาจอ่านยากเร็วๆ หากมี Commits เยอะ
git log มาพร้อม Flags สำหรับ Filter Logs นี่คือ Flags หลัก:
Filter by Most Recent
หากอยาก Return จำนวน Commits เฉพาะ ใช้ Flag -n ตัวอย่าง Flag ใน Action:
คำสั่งนี้คืน List ของ 3 Commits ล่าสุดที่ทำไป Repository
Filter by Author หรือ Committer
Filter Commits ที่ git log คืนโดย Person ที่เขียนหรือ Commit Changes สมมติอยากดู Commits ที่ "John Smith" Push ใช้ Commands:
git log --author="John Smith" git log --committer="John smith"
Flag --author จำกัด Results ให้ Commits ที่ Changes ทำโดย John Smith Flag --committer จำกัด Results ให้ Commits ที่ Committed โดยคนนั้นจริงๆ
จะพบว่าใน Most Cases Author และ Committer เป็นคนเดียวกัน ใน Projects ใหญ่ Author ของ Commit อาจไม่ใช่คน Push ไป Repository นั่นคือเหตุผลมีสอง Flags นี้
Filter by Date
นอกจากนี้ Filter Results ของ git log โดย Date ได้ ใช้ Flags --before และ --after Flags ทั้งสองรับ Date Formats หลากหลาย แต่สอง Format ที่ใช้บ่อยคือ Relative References และ Full Dates
สมมติอยาก Retrieve Commits หลัง 2019-3-2 ใช้คำสั่ง:
git log --after="2019-3-2"
เราระบุ Date Filter Commits ทำนองเดียวกัน หากอยาก Commits ก่อนเมื่อวาน ใช้คำสั่ง:
git log --before="yesterday"
เราระบุ Relative Value ("yesterday")
ตอนนี้ สมมติอยาก Commits ที่ Published หลัง 2019-3-2 และก่อน 2019-3-19 ใช้คำสั่ง:
git log --after="2019-3-2" --before="2019-3-19"
Filter by File
ตอนใช้ git log Command อาจอยากดู Commits ที่ Affect File เฉพาะเท่านั้น ระบุ File ที่อยากดู Changes ได้
สมมติอยากดู Changes ใน File "main.py" ใช้คำสั่ง:
Statement — สั่ง git log ว่า Arguments ที่ระบุคือ File Paths ไใช่ Branch Names
ใน Command เราระบุ File เดียวที่อยาก Filter Response ของ git log แต่ถ้าอยากระบุ Multiple Files ก็ได้
Filter by Content
Search Commits ที่ Remove หรือ Add Particular Line ของ Code ได้

"Career Karma เข้ามาในชีวิตฉันเมื่อฉันต้องการมากที่สุด และช่วยจับคู่กับบูทแคมป์ได้เร็ว สองเดือนหลังจบ ฉันพบงานฝันที่ตรงกับคุณค่าและเป้าหมายในชีวิต!"
Venus, วิศวกรซอฟต์แวร์ที่ Rockbot
ค้นหาบูทแคมป์ของคุณ
สมมติอยาก Search Commits ทั้งหมดที่ Add Term "# Introduction" ไป Code ใช้คำสั่ง:
git log -S"# Introduction"
Flag -S ใช้ระบุ Commit Change ที่อยากหาใน List ของ Commits สังเกตว่าไม่มี Space หรือ Equals Sign ระหว่าง -S Flag และ Quotation Marks ที่ใช้ระบุ Content ของ Commit ที่หา
Filter by Range
ใช้ Parameters since และ until ส่ง Range ของ Commits ไป git log Syntax สำหรับ Parameters นี้:
สมมติอยาก Commits ตั้งแต่ Commit b72beb5 Push จนถึง Commit b53b22d Push ใช้คำสั่ง:
คำสั่งนี้คืน List ของ Commits ทั้งหมดระหว่าง b72beb5 และ b53b22d Commits
Filter by Message
Flag --grep Filter Commits ที่ git log คืนโดย Commit Message ที่ Associated กับ Commit เฉพาะ
ตัวอย่าง สมมติอยาก Return Commits ทั้งหมดที่ชื่อขึ้นต้นด้วย "feat:" ใช้ Code:
คำสั่งนี้คืน List ของ Commits ทั้งหมดที่ Messages เริ่มด้วย "feat:"
Formatting Output ของ git log
Section ก่อนเราทำงานกับ Full Output จาก git log Command Output นี้มี SHA ของ Commit, Commit Author, Date ที่ Commit Push, และ Commit Message
แม้ข้อมูลทั้งหมด Useful แต่บ่อยครั้งต้องการแค่ Specific Data เกี่ยวกับ Commit โชคดีที่ git log มาพร้อม Flags สำหรับ Format Output
Return Brief Logs
โดย Default git log Statement คืน Full Log Entry สำหรับแต่ละ Commit ดึง List ของ Commit IDs และ Associated Commit Messages ได้ด้วย Flag --oneline
Syntax สำหรับ Flag --oneline:
รันใน Repository จากก่อนหน้า คำสั่งคืน:
43433c6 (HEAD -> master, origin/master, origin/HEAD) Update index.html a7d8dc2 docs: Update README.md b53b22d feat: Update website 3b16026 feat: Launch new homepage b72beb5 first commit
เห็น Commit IDs และบรรทัดแรกของ Messages ที่ Associated กับ Commit อ่านง่ายกว่า Metadata ทั้งหมดที่ git log คืนค่า
แต่ละ Entry อยู่บน Single Line เทคนิค git log one line นี้ Handy เพราะแสดง Commits โดยไม่แสดงข้อมูลมากเกินไป
Decorating Output
Flag --decorate ให้เห็น References ทั้งหมด (Branches และ Tags) ที่ Point ไป Commit เฉพาะ Syntax สำหรับ Flag นี้:
คำสั่งคืน:
43433c6 (HEAD -> master, origin/master, origin/HEAD) Update index.html a7d8dc2 docs: Update README.md b53b22d feat: Update website 3b16026 (tag: v1) feat: Launch new homepage b72beb5 first commit
Commit ที่สี่ใน List ตอนนี้มี Tag Name ระบุ เพราะ Flag --decorate เปิดเผย References ที่ Associated กับแต่ละ Commit ใน Commit History
Returning a Diff
Flag --stat ให้ Display จำนวน Lines ของ Code ที่ Added และ Deleted จาก Repository ในแต่ละ Commit ตัวอย่าง git log --stat Command ใน Action:
Author: James GallagherDate: Mon Apr 6 09:11:46 2020 +0100 feat: Update website index.html | 1 + index.js | 0 2 files changed, 1 insertion(+)
Plus Signs (+) แสดง Insertions และถ้ามี Minus Signs (-) แสดง Deletions Data นี้ให้เรียนรู้ Overall Changes ใน Repository
หากอยากดู Exact Changes ใน Repository ใช้ Flag -p คืน Diff ครอบคลุมมากขึ้นแสดง Changes ใน Commit
นี่คือ One Entry ที่คืนจาก git log -p Command รันบน Example Repository:
Author: James Gallagher <37276661+jamesgallagher432@users.noreply.github.com> Date: Tue Apr 7 13:09:58 2020 +0100 Update index.html diff --git a/index.html b/index.html index f45673f..2d2701d 100644 --- a/index.html +++ b/index.html @@ -1,2 +1,3 @@ -This is a file. -Changes have been made. + +This is a website.
+
Output นี้แสดง Description ของ Commit และ Detailed Breakdown ของแต่ละ Change ใน Commit เห็นเมื่อ People Added/Removed Content จาก Files ใน Repository
แม้ Data นี้ Useful แต่อาจอ่านยากเร็วหาก Commits เยอะ อาจใช้ Flags อื่นๆ ที่อธิบายในบทความ เพื่อให้ Information ที่คืนจาก Commit นี้ Comprehensive และ อ่านง่าย
อ่านเพิ่มเติม Git Diffs ใน git diff Command Guide
คำสั่ง git shortlog
คำสั่ง git shortlog ให้ Summary ของ git log Output ของ git shortlog Group โดย Author หมายถึงดูง่ายว่าใครทำ Changes อะไรใน Repository
ลองรัน git shortlog บน Repository จากก่อนหน้า:
Command คืน:
James Gallagher (5): first commit feat: Launch new homepage feat: Update website docs: Update README.md Update index.html
James คือคนเดียวที่ Contribute Repository นี้ และทำ 5 Commits แต่ถ้ามี Contributors อื่นๆ Contributions จะ List ที่นี่ พร้อม Total Commits ที่ Push ไป Repository
สรุป
คำสั่ง git log แสดงว่า Changes อะไรทำไป Repository, โดยใคร, และเมื่อไหร่ คุณ Filter Output ของ git log ให้แสดงแค่ Information ที่ต้องรู้
git log Command มาพร้อมสองประเภท Flags บาง Flags ช่วย Format Output ของ Log อีกบาง Flags ช่วย Filter Commits ที่คืนโดย Command
Tutorial นี้อธิบายพร้อม Examples ว่าใช้ git log และ Flags พื้นฐานยังไง git log Command เป็น Tool สำคัญใน Arsenal ตอนทำงานกับ Git เมื่อรู้ใช้ Command นี้อย่างมีประสิทธิภาพ คุณจะ Master Inspecting Git Repositories!
อ่านเพิ่มเติม Git ใน How to Learn Git Guide