格式化log輸出
oneline
--oneline標記將每個commit壓縮成一行. 默認情況下顯示一個commit ID和commit描述的第一行. 輸出如下:
0e25143 Merge branch 'feature' ad8621a Fix a bug in the feature 16b36c6 Add a new feature 23ad9ad Add the initial code base
decorate
許多時候知道commit是和哪一個分支或tag關聯的是非常有用的. --decorate標記會讓git log顯示每個commit的引用(如:分支、tag等).
可以和其它的config選項結合使用. 例如, git log --oneline --decorate輸出如下:
0e25143 (HEAD, master) Merge branch 'feature' ad8621a (feature) Fix a bug in the feature 16b36c6 Add a new feature 23ad9ad (tag: v0.9) Add the initial code base
從中可以看出第一個commit在master分支. 第二個commit在一個feature分支, 第四個commit被tag為v0.9.
diffs
git log有許多選項是為了展示每個commit的不同之處的. 最常用的兩個是--stat和-p.
--stat顯示每個commit中每個文件的添加的行數和刪除的行數.這對我們了解一個commit大致有些什么修改非常有用. 下面的commit hello.py新增了67行, 刪除了38行:
commit f2a238924e89ca1d4947662928218a06d39068c3 Author: John <john@example.com> Date: Fri Jun 25 17:30:28 2014 -0500 Add a new feature hello.py | 105 ++++++++++++++++++++++++----------------- 1 file changed, 67 insertion(+), 38 deletions(-)
文件名后面的+-符號是這個文件相對的增加和刪除行數.
如果你想看每個commit具體修改了些什么可以使用git log -p, 結果如下:
commit 16b36c697eb2d24302f89aa22d9170dfe609855b Author: Mary <mary@example.com> Date: Fri Jun 25 17:31:57 2014 -0500 Fix a bug in the feature diff --git a/hello.py b/hello.py index 18ca709..c673b40 100644 --- a/hello.py +++ b/hello.py @@ -13,14 +13,14 @@ B -print("Hello, World!") +print("Hello, Git!")
shortlog
git shortlog是一個特殊版本的git log, 他的目的是為了創建一個發布的通知. 將commit按照作者分組, 顯示每個commit的第一行描述. 通過他你很容易看到誰做了些什么.
例如, 兩個開發者為一個項目貢獻了5個commit, git shortlog的輸出如下:
Mary (2): Fix a bug in the feature Fix a serious security hole in our framework John (3): Add the initial code base Add a new feature Merge branch 'feature'
默認情況下git shortlog按照作者名排序, 你可以使用-n按照每個作者的commit數量來排序.
graph
--graph標記會畫出一個ASCII圖展示commit歷史的分支結構. 通常和--oneline --decorate結合使用:
git log --graph --oneline --decorate
* 0e25143 (HEAD, master) Merge branch 'feature' |\ | * 16b36c6 Fix a bug in the new feature | * 23ad9ad Start a new feature * | ad8621a Fix a critical security issue |/ * 400e4b7 Fix typos in the documentation * 160e224 Add the initial code base
星號告訴你commit在哪一個branch上,上面的輸出告訴我們23ad9ad 和 16b36c6在一個主題分支, 其它的在master分支.
自定義輸出
可以使用--pretty=format:"<string>"來自定義輸出.
例如%cn代表commiter name, %h代表commit hash的縮寫, %cd代表commiter date.
git log --pretty=format:"%cn committed %h on %cd"
輸出如下:
John committed 400e4b7 on Fri Jun 24 12:30:04 2014 -0500 John committed 89ab2cf on Thu Jun 23 17:09:42 2014 -0500 Mary committed 180e223 on Wed Jun 22 17:21:19 2014 -0500 John committed f12ca28 on Wed Jun 22 13:50:31 2014 -0500
篩選commit歷史
按數量
可以使用-n來限制輸出的數量. 下面的例子只顯示最近3個commit.
git log -3
按日期
可以使用--after或--before來按照日期篩選. 下面的例子只顯示2014年7月1號之后的commit(包含7月1號).
git log --after="2014-7-1"
還可以使用一個相對的時間, 例如"1 week ago"和"yesterday""
git log --after="yesterday"
如果看某個時間段的commit可以同時使用--after和--before. 下面的例子顯示2014年7月1日到2014年7月4日之間的commit:
git log --after="2014-7-1" --before="2014-7-4"
注意--since --until和 --after --before是一個意思.
按作者
git log --author="John"
顯示John貢獻的commit. 作者名不需要精確匹配--只需要包含就行了.
還可以使用正則表達式. 下面的命令搜索Marry和John貢獻的commit.
git log --author="John\|Mary"
注意這個--author不僅包含名還包含email, 所以你可以用這個搜索email.
按commit描述
例如, 如果你的團隊會在每個commit描述里面加上相關的issue號, 你可以使用下面的命令查找跟某個issue相關的commit:
git log --grep="JRA-224"
還可以傳入-i用來忽略大小寫.
按文件
有時你可能只對某個文件的修改感興趣, 你只想查看跟某個文件相關的歷史信息, 你只需要插入你感興趣文件的路徑就可以了. 下面的例子只返回和foo.py或bar.py相關的commit:
git log -- foo.py bar.py
這里的--是告訴Git后面的參數是文件路徑而不是branch的名字. 如果后面的文件路徑不會和某個branch產生混淆, 你可以省略--.
按內容
有時你想搜索和新增或刪除某行代碼相關的commit. 可以使用 -S"<string>". 下面的例子假設你想知道Hello, World!這句話是什么時候加入到項目里去的, 你可以使用下面的命令:
git log -S"Hello,World!"
如果你想使用正則表達式去匹配而不是字符串, 那么你可以使用-G代替-S.
這是一個非常有用的debug工具, 使用他你可以定位所有跟某行代碼相關的commit. 甚至可以查看某行是什么時候被copy的, 什么時候移到另外一個文件中去的.
按范圍
可以查看某個范圍的commit:
git log <since>..<until>
這個命令非常有用當你使用branch做為range參數的時候. 能很方便的顯示2個branch之間的不同. 看看下面的命令:
git log master..feature
master..feature這個range包含了在feature有而在master沒有的所有commit. 如圖所示(圖中最下面的master..feature應該錯了, 是feature..master才對):
feature..master包含所有master有但是feature沒有的commit.
過濾merge commit
默認情況下git log會輸出merge commit. 你可以通過--no-merges標記來過濾掉merge commit:
git log --no-merges
如果你只對merge commit感興趣可以使用--merges:
git log --merges