## git log 過濾你的提交歷史記錄
通過查看分支中另一分支看不到的提交記錄,我們已經看到如何用?`git log`?來比較分支。 (如果你不記得了,它看起來是這樣的:`git log branchA ^branchB`)。 而且,你也可以用`git log`?去尋找特定的提交。 在此,我們會看到一些更廣為使用的?`git log`?選項,不過哪有很多。 完整的清單可以看看官方文檔。
### git log –author?只尋找某個特定作者的提交
要過濾你的提交歷史,只尋找某個特定作者的提交,你可以使用?`--author`?選項。 例如,比方說我們要找 Git 源碼中 Linus 提交的部分。 我們可以執行類似?`git log --author=Linus`?的命令。 這個查找是大小寫敏感的,并且也會檢索電子郵箱地址。 我在此例中使用?`-[number]`?選項,以限制結果為最近 [number] 次的提交。
~~~
$ git log --author=Linus --oneline -5
81b50f3 Move 'builtin-*' into a 'builtin/' subdirectory
3bb7256 make "index-pack" a built-in
377d027 make "git pack-redundant" a built-in
b532581 make "git unpack-file" a built-in
112dd51 make "mktag" a built-in
~~~
### git log –since –before?根據日期過濾提交記錄
如果你要指定一個你感興趣的日期范圍以過濾你的提交,可以執行幾個選項 —— 我用?`--since`?和?`--before`,但是你也可以用?`--until`?和?`--after`。 例如,如果我要看 Git 項目中三周前且在四月十八日之后的所有提交,我可以執行這個(我還用了?`--no-merges`?選項以隱藏合并提交):
~~~
$ git log --oneline --before={3.weeks.ago} --after={2010-04-18} --no-merges
5469e2d Git 1.7.1-rc2
d43427d Documentation/remote-helpers: Fix typos and improve language
272a36b Fixup: Second argument may be any arbitrary string
b6c8d2d Documentation/remote-helpers: Add invocation section
5ce4f4e Documentation/urls: Rewrite to accomodate transport::address
00b84e9 Documentation/remote-helpers: Rewrite description
03aa87e Documentation: Describe other situations where -z affects git diff
77bc694 rebase-interactive: silence warning when no commits rewritten
636db2c t3301: add tests to use --format="%N"
~~~
### git log –grep?根據提交注釋過濾提交記錄
你或許還想根據提交注釋中的某個特定短語查找提交記錄。可以用?`--grep`?選項。 比如說我知道有個提交是有關使用 P4EDITOR 環境變量,又想回憶起那個改動是啥樣子的 —— 我可以用`--grep`?選項找到該提交。
~~~
$ git log --grep=P4EDITOR --no-merges
commit 82cea9ffb1c4677155e3e2996d76542502611370
Author: Shawn Bohrer
Date: Wed Mar 12 19:03:24 2008 -0500
git-p4: Use P4EDITOR environment variable when set
Perforce allows you to set the P4EDITOR environment variable to your
preferred editor for use in perforce. Since we are displaying a
perforce changelog to the user we should use it when it is defined.
Signed-off-by: Shawn Bohrer
Signed-off-by: Simon Hausmann
~~~
Git 會對所有的?`--grep`?和?`--author`?參數作邏輯或。 如果你用?`--grep`?和?`--author`?時,想看的是某人寫作的并且有某個特殊的注釋內容的提交記錄, 你需要加上?`--all-match`?選項。 在這些例子中,我會用上?`--format`?選項,這樣我們就可以看到每個提交的作者是誰了。
如果我查找注釋內容含有 “p4 depo”的提交,我得到了三個提交:
~~~
$ git log --grep="p4 depo" --format="%h %an %s"
ee4fd1a Junio C Hamano Merge branch 'master' of git://repo.or.cz/git/fastimport
da4a660 Benjamin Sergeant git-p4 fails when cloning a p4 depo.
1cd5738 Simon Hausmann Make incremental imports easier to use by storing the p4 d
~~~
如果我加上?`--author=Hausmann`?參數,與進一步過濾上述結果到 Simon 的唯一提交相反, 它會告訴我所有 Simon 的提交,或者注釋中有“p4 demo”的提交:
~~~
$ git log --grep="p4 depo" --format="%h %an %s" --author="Hausmann"
cdc7e38 Simon Hausmann Make it possible to abort the submission of a change to Pe
f5f7e4a Simon Hausmann Clean up the git-p4 documentation
30b5940 Simon Hausmann git-p4: Fix import of changesets with file deletions
4c750c0 Simon Hausmann git-p4: git-p4 submit cleanups.
0e36f2d Simon Hausmann git-p4: Removed git-p4 submit --direct.
edae1e2 Simon Hausmann git-p4: Clean up git-p4 submit's log message handling.
4b61b5c Simon Hausmann git-p4: Remove --log-substitutions feature.
36ee4ee Simon Hausmann git-p4: Ensure the working directory and the index are cle
e96e400 Simon Hausmann git-p4: Fix submit user-interface.
38f9f5e Simon Hausmann git-p4: Fix direct import from perforce after fetching cha
2094714 Simon Hausmann git-p4: When skipping a patch as part of "git-p4 submit" m
1ca3d71 Simon Hausmann git-p4: Added support for automatically importing newly ap
...
~~~
不過,如果加上?`--all-match`,結果就是我想要的了:
~~~
$ git log --grep="p4 depo" --format="%h %an %s" --author="Hausmann" --all-match
1cd5738 Simon Hausmann Make incremental imports easier to use by storing the p4 d
~~~
### git log -S?依據所引入的差值過濾
如果你寫的提交注釋都極度糟糕怎么辦?或者,如果你要找某個函數是何時引入的,某些變量是在哪里開始被使用的? 你可以告訴 Git 在每個提交之間的差值中查找特定字符串。 例如,如果我們想要找出哪個提交修改出了類似函數名“userformat_find_requirements”, 我們可以執行(注意在“-S”與你要找的東東之間沒有“=”):
~~~
$ git log -Suserformat_find_requirements
commit 5b16360330822527eac1fa84131d185ff784c9fb
Author: Johannes Gilger
Date: Tue Apr 13 22:31:12 2010 +0200
pretty: Initialize notes if %N is used
When using git log --pretty='%N' without an explicit --show-notes, git
would segfault. This patches fixes this behaviour by loading the needed
notes datastructures if --pretty is used and the format contains %N.
When --pretty='%N' is used together with --no-notes, %N won't be
expanded.
This is an extension to a proposed patch by Jeff King.
Signed-off-by: Johannes Gilger
Signed-off-by: Junio C Hamano
~~~
### git log -p?顯示每個提交引入的補丁
每個提交都是項目的一個快照。由于每個提交都記錄它所基于的快照,Git 能夠經常對它們求差值,并以補丁形式向你展示。 這意味著,對任意提交,你都可以獲取該提交給項目引入補丁。 你可以用?`git show [SHA]`?加上某個特定的提交 SHA 獲取,或者執行?`git log -p`, 它會告訴 Git 輸出每個提交之后的補丁。這是個總結某一分支或者兩個提交之間都發生了神馬的好途徑。
~~~
$ git log -p --no-merges -2
commit 594f90bdee4faf063ad07a4a6f503fdead3ef606
Author: Scott Chacon
Date: Fri Jun 4 15:46:55 2010 +0200
reverted to old class name
diff --git a/ruby.rb b/ruby.rb
index bb86f00..192151c 100644
--- a/ruby.rb
+++ b/ruby.rb
@@ -1,7 +1,7 @@
-class HiWorld
+class HelloWorld
def self.hello
puts "Hello World from Ruby"
end
end
-HiWorld.hello
+HelloWorld.hello
commit 3cbb6aae5c0cbd711c098e113ae436801371c95e
Author: Scott Chacon
Date: Fri Jun 4 12:58:53 2010 +0200
fixed readme title differently
diff --git a/README b/README
index d053cc8..9103e27 100644
--- a/README
+++ b/README
@@ -1,4 +1,4 @@
-Hello World Examples
+Many Hello World Examples
======================
This project has examples of hello world in
~~~
這是個總結改動,以及合并或發布之前重審一系列提交的好方式。
### git log –stat?顯示每個提交引入的改動的差值統計
如果?`-p`?選項對你來說太詳細了,你可以用?`--stat`?總結這些改動。 這是不用?`-p`,而用?`--stat`?選項時,同一份日志的輸出。
~~~
$ git log --stat --no-merges -2
commit 594f90bdee4faf063ad07a4a6f503fdead3ef606
Author: Scott Chacon
Date: Fri Jun 4 15:46:55 2010 +0200
reverted to old class name
ruby.rb | 4 ++--
1 files changed, 2 insertions(+), 2 deletions(-)
commit 3cbb6aae5c0cbd711c098e113ae436801371c95e
Author: Scott Chacon
Date: Fri Jun 4 12:58:53 2010 +0200
fixed readme title differently
README | 2 +-
1 files changed, 1 insertions(+), 1 deletions(-)
~~~
同樣的基本信息,但更緊湊 —— 它仍然讓你看到相對改動,和改動了哪些文件。