1. 程式人生 > >git命令——git log

git命令——git log

功能

在提交了若干更新,又或者克隆了某個專案之後,你也許想回顧下提交歷史。 完成這個任務最簡單而又有效的方法是 使用git log 命令。

引數

不帶任何引數

$ git log
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon <[email protected]>
Date: Mon Mar 17 21:52:11 2008 -0700
    changed the version number

commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: Scott Chacon 
<[email protected]> Date: Sat Mar 15 16:40:33 2008 -0700 removed unnecessary test commit a11bef06a3f659402fe7563abf99ad00de2209e6 Author: Scott Chacon <[email protected]> Date: Sat Mar 15 10:31:28 2008 -0700 first commit
View Code

預設不用任何引數的話,git log 會按提交時間列出所有的更新,最近的更新排在最上面。git log會列出每個提交的 SHA-1 校驗和、作者的名字和電子郵件地址、提交時間以及提交說明。

-p

-p用來顯示每次提交的內容差異。 你也可以加上 -2 來僅顯示最近兩次提交:

-p除了顯示基本資訊之外,還附帶了每次 commit 的變化。 當進行程式碼審查,或者快速瀏覽某個搭檔提交的 commit 所帶來的變化的時候,這個引數就非常有用了。

$ git log -p -2
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon <[email protected]>
Date: Mon Mar 17 21:52:11 2008 -0700

    changed the version number
    
diff --git a/Rakefile b/Rakefile index a874b73..8f94139 100644 --- a/Rakefile +++ b/Rakefile @@ -5,7 +5,7 @@ require 'rake/gempackagetask' spec = Gem::Specification.new do |s| s.platform = Gem::Platform::RUBY s.name = "simplegit" - s.version = "0.1.0" + s.version = "0.1.1" s.author = "Scott Chacon" s.email = "[email protected]" s.summary = "A simple gem for using Git in Ruby code." commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7 Author: Scott Chacon <[email protected]> Date: Sat Mar 15 16:40:33 2008 -0700 removed unnecessary test diff --git a/lib/simplegit.rb b/lib/simplegit.rb index a0a60ae..47c6340 100644 --- a/lib/simplegit.rb +++ b/lib/simplegit.rb @@ -18,8 +18,3 @@ class SimpleGit end end - -if $0 == __FILE__ - git = SimpleGit.new - puts git.show -end \ No newline at end of file
View Code

--stat

-p可以也可以顯示每次commit變化,問題就是顯示資訊太詳細了,檔案修改了什麼都會顯示出來。如果修改了很多檔案,-p會輸出大量內容。--stat可以顯示每次提交的簡略的統計資訊。--stat 選項在每次提交的下面列出所有被修改過的檔案、有多少檔案被修改了以及被修改過的檔案的哪些行被移除或是添加了。 在每次提交的最後還有一個總結。

$ git log --stat
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon <[email protected]>
Date: Mon Mar 17 21:52:11 2008 -0700

    changed the version number
    
 Rakefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: Scott Chacon <[email protected]>
Date: Sat Mar 15 16:40:33 2008 -0700

    removed unnecessary test
    
 lib/simplegit.rb | 5 -----
 1 file changed, 5 deletions(-)
 
commit a11bef06a3f659402fe7563abf99ad00de2209e6
Author: Scott Chacon <[email protected]>
Date: Sat Mar 15 10:31:28 2008 -0700

    first commit
    
README | 6 ++++++
Rakefile | 23 +++++++++++++++++++++++
lib/simplegit.rb | 25 +++++++++++++++++++++++++
3 files changed, 54 insertions(+)
View Code

--pretty

--pretty可以指定使用不同於預設格式的方式展示提交歷史。 這個選項有一些內建的子選項供你使用。

--pretty=oneline 將每個提交放在一行顯示,檢視的提交數很大時非常有用。

View Code

--pretty=short,full 和 fuller 展示的資訊或多或少有些不同。

--pretty=format可以定製要顯示的記錄格式。這樣的輸出對後期提取分析格外有用 。

$ git log --pretty=format:"%h - %an, %ar : %s"
ca82a6d - Scott Chacon, 6 years ago : changed the version number
085bb3b - Scott Chacon, 6 years ago : removed unnecessary test
a11bef0 - Scott Chacon, 6 years ago : first commit
View Code

--pretty=format 常用的選項 列出了常用的格式佔位符寫法及其代表的意義。

作者:指的是實際作出修改的人

提交者:指的是最後將此工作成果提交到倉庫的人。

舉例:當你為某個專案釋出補丁,然後某個核心成員將你的補丁併入專案時,你就是作者,而那個核心成員就是提交者。

 --graph

顯示 ASCII 圖形表示的分支合併歷史。這個選項常與--pretty=format 或 --pretty=oneline 連用。

$ git log --pretty=format:"%h %s" --graph
* 2d3acf9 ignore errors from SIGCHLD on trap
* 5e3ee11 Merge branch 'master' of git://github.com/dustin/grit
|\
| * 420eac9 Added a method for getting the current branch.
* | 30e367c timeout code and tests
* | 5a09431 add timeout protection to grit
* | e1193f8 support for heads with slashes in them
|/
* d6016bc require time for xmlschema
* 11d191e Merge branch 'defunkt' into local
View Code

限制輸出選項

舉例:檢視 Git 倉庫中,2008 年 10 月期間,Junio Hamano 提交的但未合併的測試檔案,可以用下面的查詢命令:

$ git log --pretty="%h - %s" --author=gitster --since="2008-10-01" \
    --before="2008-11-01" --no-merges -- t/
5610e3b - Fix testcase failure when extended attributes are in use
acd3b9e - Enhance hold_lock_file_for_{update,append}() API
f563754 - demonstrate breakage of detached checkout with symbolic link
HEAD
d1a43f2 - reset --hard/read-tree --reset -u: remove unmerged new paths
51a94af - Fix "checkout --track -b newbranch" on detached HEAD
b0ad11e - pull: allow "git pull origin $something:$current_branch" into an
unborn branch
View Code