## git log 過(guò)濾你的提交歷史記錄
通過(guò)查看分支中另一分支看不到的提交記錄,我們已經(jīng)看到如何用?`git log`?來(lái)比較分支。 (如果你不記得了,它看起來(lái)是這樣的:`git log branchA ^branchB`)。 而且,你也可以用`git log`?去尋找特定的提交。 在此,我們會(huì)看到一些更廣為使用的?`git log`?選項(xiàng),不過(guò)哪有很多。 完整的清單可以看看官方文檔。
### git log –author?只尋找某個(gè)特定作者的提交
要過(guò)濾你的提交歷史,只尋找某個(gè)特定作者的提交,你可以使用?`--author`?選項(xiàng)。 例如,比方說(shuō)我們要找 Git 源碼中 Linus 提交的部分。 我們可以執(zhí)行類似?`git log --author=Linus`?的命令。 這個(gè)查找是大小寫(xiě)敏感的,并且也會(huì)檢索電子郵箱地址。 我在此例中使用?`-[number]`?選項(xiàng),以限制結(jié)果為最近 [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?根據(jù)日期過(guò)濾提交記錄
如果你要指定一個(gè)你感興趣的日期范圍以過(guò)濾你的提交,可以執(zhí)行幾個(gè)選項(xiàng) —— 我用?`--since`?和?`--before`,但是你也可以用?`--until`?和?`--after`。 例如,如果我要看 Git 項(xiàng)目中三周前且在四月十八日之后的所有提交,我可以執(zhí)行這個(gè)(我還用了?`--no-merges`?選項(xiàng)以隱藏合并提交):
~~~
$ 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?根據(jù)提交注釋過(guò)濾提交記錄
你或許還想根據(jù)提交注釋中的某個(gè)特定短語(yǔ)查找提交記錄??梢杂?`--grep`?選項(xiàng)。 比如說(shuō)我知道有個(gè)提交是有關(guān)使用 P4EDITOR 環(huán)境變量,又想回憶起那個(gè)改動(dòng)是啥樣子的 —— 我可以用`--grep`?選項(xiàng)找到該提交。
~~~
$ 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 會(huì)對(duì)所有的?`--grep`?和?`--author`?參數(shù)作邏輯或。 如果你用?`--grep`?和?`--author`?時(shí),想看的是某人寫(xiě)作的并且有某個(gè)特殊的注釋內(nèi)容的提交記錄, 你需要加上?`--all-match`?選項(xiàng)。 在這些例子中,我會(huì)用上?`--format`?選項(xiàng),這樣我們就可以看到每個(gè)提交的作者是誰(shuí)了。
如果我查找注釋內(nèi)容含有 “p4 depo”的提交,我得到了三個(gè)提交:
~~~
$ 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`?參數(shù),與進(jìn)一步過(guò)濾上述結(jié)果到 Simon 的唯一提交相反, 它會(huì)告訴我所有 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
...
~~~
不過(guò),如果加上?`--all-match`,結(jié)果就是我想要的了:
~~~
$ 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?依據(jù)所引入的差值過(guò)濾
如果你寫(xiě)的提交注釋都極度糟糕怎么辦?或者,如果你要找某個(gè)函數(shù)是何時(shí)引入的,某些變量是在哪里開(kāi)始被使用的? 你可以告訴 Git 在每個(gè)提交之間的差值中查找特定字符串。 例如,如果我們想要找出哪個(gè)提交修改出了類似函數(shù)名“userformat_find_requirements”, 我們可以執(zhí)行(注意在“-S”與你要找的東東之間沒(méi)有“=”):
~~~
$ 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?顯示每個(gè)提交引入的補(bǔ)丁
每個(gè)提交都是項(xiàng)目的一個(gè)快照。由于每個(gè)提交都記錄它所基于的快照,Git 能夠經(jīng)常對(duì)它們求差值,并以補(bǔ)丁形式向你展示。 這意味著,對(duì)任意提交,你都可以獲取該提交給項(xiàng)目引入補(bǔ)丁。 你可以用?`git show [SHA]`?加上某個(gè)特定的提交 SHA 獲取,或者執(zhí)行?`git log -p`, 它會(huì)告訴 Git 輸出每個(gè)提交之后的補(bǔ)丁。這是個(gè)總結(jié)某一分支或者兩個(gè)提交之間都發(fā)生了神馬的好途徑。
~~~
$ 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
~~~
這是個(gè)總結(jié)改動(dòng),以及合并或發(fā)布之前重審一系列提交的好方式。
### git log –stat?顯示每個(gè)提交引入的改動(dòng)的差值統(tǒng)計(jì)
如果?`-p`?選項(xiàng)對(duì)你來(lái)說(shuō)太詳細(xì)了,你可以用?`--stat`?總結(jié)這些改動(dòng)。 這是不用?`-p`,而用?`--stat`?選項(xiàng)時(shí),同一份日志的輸出。
~~~
$ 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(-)
~~~
同樣的基本信息,但更緊湊 —— 它仍然讓你看到相對(duì)改動(dòng),和改動(dòng)了哪些文件。
