今日目標
• 學會查詢 Git 歷史記錄
• 掌握各種 log 查詢技巧
• 學習追蹤程式碼變更
• 找出 bug 的來源和責任歸屬
為什麼需要查詢歷史?
職場真實情況:
主管:「這個 bug 是誰寫的?什麼時候加入的?」
同事:「這個功能是誰負責的?」
客戶:「上週的版本還正常,現在怎麼壞了?」
如果不會查 Git 歷史 → 😰 不知道...
會查 Git 歷史 → 😎 馬上找到!
查詢歷史的用途:
• 🔍 找出 bug 來源:知道誰在何時引入問題
• 📊 分析程式碼變更:追蹤功能的演進
• 👥 了解開發歷程:誰做了什麼修改
• 🔄 版本回溯:找到特定版本的程式碼
• 📈 團隊績效分析:統計提交數量和貢獻度
操作步驟
步驟1:基本歷史查詢
查看提交記錄:
git log
git log --oneline
git log --oneline -5
git log --oneline -10
範例輸出:
a1b2c3d (HEAD -> main) feat: 新增使用者儀表板
e4f5g6h fix: 修復登入驗證問題
i7j8k9l docs: 更新 README 說明
m1n2o3p style: 改善首頁樣式
q4r5s6t feat: 新增團隊介紹頁面
步驟2:圖形化顯示分支歷史
顯示分支結構:
git log --oneline --graph
git log --oneline --graph --all
git log --graph --all --decorate --oneline
範例輸出:
git show a1b2c3d
git show --stat a1b2c3d
git show --no-patch a1b2c3d
步驟4:按條件搜尋提交
按作者搜尋:
git log --author="你的名字"
git log --author="Alice|Bob"
git log --author="$(git config user.name)"
按日期搜尋:
git log --since="today"
git log --since="1 week ago"
git log --since="2024-01-01"
git log --since="2024-01-01" --until="2024-01-31"
按提交訊息搜尋:
git log --grep="login"
git log --grep="fix"
git log --grep="feat"
git log --grep="LOGIN" -i
步驟5:查看檔案變更歷史
查看特定檔案的歷史:
git log -- index.html
git log --oneline -- index.html
git log -p -- index.html
git log --follow -- index.html
查看檔案的每一行是誰寫的:
git blame index.html
git blame -L 10,20 index.html
git blame -l index.html
步驟6:比較不同版本
比較兩個提交:
git diff a1b2c3d e4f5g6h
git diff --name-only a1b2c3d e4f5g6h
git diff --stat a1b2c3d e4f5g6h
比較分支:
git diff main feature/homepage
git diff main
git diff --staged
步驟7:搜尋程式碼內容
在提交歷史中搜尋程式碼:
git log -S "function login"
git log -G "login.*function"
git log -S "function login" -p
在目前程式碼中搜尋:
git grep "login"
git grep -n "login"
git grep -l "login"
git grep -i "LOGIN"
實用查詢技巧
技巧1:找出引入 bug 的提交
使用 git bisect(二分搜尋法):
git bisect start
git bisect bad
git bisect good v1.0.0
git bisect good
git bisect bad
git bisect reset
技巧2:查看檔案在特定版本的內容
git show a1b2c3d:index.html
git show v1.0.0:index.html
git show a1b2c3d:index.html > old_index.html
技巧3:統計提交資訊
git shortlog -sn
git shortlog -sn --since="1 month ago"
git shortlog -s
git shortlog
技巧4:查看提交之間的關係
git branch --contains a1b2c3d
git merge-base main feature/homepage
git log a1b2c3d..HEAD
進階查詢組合
組合1:查看最近一週自己的提交
git log --author="$(git config user.name)" --since="1 week ago" --oneline
組合2:查看某個功能的完整開發歷程
git log --grep="登入" --all --oneline --graph
組合3:產生變更日誌
git log v1.0.0..v1.1.0 --oneline --no-merges
git log v1.0.0..v1.1.0 --oneline --no-merges | grep "feat:"
git log v1.0.0..v1.1.0 --oneline --no-merges | grep "fix:"
組合4:找出最大的檔案修改
git log --pretty=format: --name-only | sort | uniq -c | sort -rg | head -10
git log --oneline -- index.html | wc -l
實戰案例
案例1:追蹤 Bug 來源
問題:網站突然無法登入
git log --grep="login" --since="1 week ago" --oneline
git log -p -- auth/login.js
git blame auth/login.js
git show a1b2c3d
案例2:準備版本發布說明
git log v1.0.0..HEAD --oneline
echo "新功能:"
git log v1.0.0..HEAD --oneline --grep="feat:"
echo "Bug 修復:"
git log v1.0.0..HEAD --oneline --grep="fix:"
echo "文件更新:"
git log v1.0.0..HEAD --oneline --grep="docs:"
案例3:程式碼審查準備
git log main..feature/new-function --oneline
git diff main...feature/new-function
git diff --name-only main...feature/new-function
常用 Git Log 參數總結
--oneline # 簡潔一行顯示
--graph # 圖形化顯示分支
--all # 顯示所有分支
--decorate # 顯示分支和標籤名稱
-n <數字> # 限制顯示數量
-5 # 顯示最近 5 個
--since="日期" # 從某日期開始
--until="日期" # 到某日期結束
--after="日期" # 等同 since
--before="日期" # 等同 until
--author="名字" # 按作者篩選
--grep="關鍵字" # 按提交訊息搜尋
-- <檔案名> # 只顯示該檔案的歷史
-p # 顯示 diff
--stat # 顯示統計資訊
--follow # 追蹤檔案重命名
-S "字串" # 搜尋程式碼變更
-G "正規表達式" # 正規表達式搜尋
今日重點回顧
• ✅ 掌握基本的 log 查詢指令
• ✅ 學會按條件搜尋提交記錄
• ✅ 能夠追蹤檔案變更歷史
• ✅ 會使用 blame 找出程式碼作者
• ✅ 學習進階查詢技巧組合
核心指令總結
git log --oneline # 簡潔查看歷史
git log --graph --all # 圖形化顯示分支
git log --author="名字" # 按作者查詢
git log --since="日期" # 按日期查詢
git log --grep="關鍵字" # 按訊息搜尋
git log -p -- <檔案> # 查看檔案歷史
git blame <檔案> # 查看每行作者
git show # 查看提交詳情
git diff # 比較版本