更新時間:2021-09-27 09:29:23 來源:動力節點 瀏覽1374次
在您創建了多個提交之后,或者如果您克隆了一個具有現有提交歷史的存儲庫,您可能想要回顧一下發生了什么。執行此操作的最基本和最強大的工具是git log命令。
這些示例使用了一個名為“simplegit”的非常簡單的項目。要獲取項目,請運行:
$ git clone https://github.com/schacon/simplegit-progit
當你git log在這個項目中運行時,你應該得到如下所示的輸出:
$ git log
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon <[email protected]>
Date: Mon Mar 17 21:52:11 2008 -0700
Change version number
commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: Scott Chacon <[email protected]>
Date: Sat Mar 15 16:40:33 2008 -0700
Remove unnecessary test
commit a11bef06a3f659402fe7563abf99ad00de2209e6
Author: Scott Chacon <[email protected]>
Date: Sat Mar 15 10:31:28 2008 -0700
Initial commit
默認情況下,不帶參數,git log按時間倒序列出在該存儲庫中進行的提交;也就是說,最近的提交首先出現。如您所見,此Git命令列出了每個提交及其 SHA-1 校驗和、作者姓名和電子郵件、寫入日期和提交消息。
該git log命令提供了大量不同的選項,可以準確地向您顯示您要查找的內容。在這里,我們將向您展示一些最受歡迎的。
更有用的選項之一是-por --patch,它顯示了每次提交中引入的差異(補丁輸出)。您還可以限制顯示的日志條目數,例如使用-2僅顯示最后兩個條目。
$ git log -p -2
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon <[email protected]>
Date: Mon Mar 17 21:52:11 2008 -0700
Change 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
Remove 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
此選項顯示相同的信息,但在每個條目之后直接顯示差異。這對于代碼審查或快速瀏覽協作者添加的一系列提交期間發生的事情非常有用。您還可以將一系列匯總選項與git log. 例如,如果您想查看每次提交的一些縮寫統計信息,您可以使用以下--stat選項:
$ git log --stat
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon <[email protected]>
Date: Mon Mar 17 21:52:11 2008 -0700
Change 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
Remove 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
Initial commit
README | 6 ++++++
Rakefile | 23 +++++++++++++++++++++++
lib/simplegit.rb | 25 +++++++++++++++++++++++++
3 files changed, 54 insertions(+)
如您所見,該--stat選項會在每個提交條目下方打印修改文件的列表、更改的文件數量以及這些文件中添加和刪除的行數。
另一個非常有用的選項是--pretty. 此選項將日志輸出更改為默認格式以外的格式。一些預構建的選項值可供您使用。oneline此選項的值將每個提交打印在一行上,這在您查看大量提交時非常有用。此外,short,full,和fuller值顯示在大致相同的格式,但分別與更少或更多的信息,輸出:
$ git log --pretty=oneline
ca82a6dff817ec66f44342007202690a93763949 Change version number
085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7 Remove unnecessary test
a11bef06a3f659402fe7563abf99ad00de2209e6 Initial commit
最有趣的選項值是format,它允許您指定自己的日志輸出格式。這在您為機器解析生成輸出時特別有用——因為您明確指定格式,您知道它不會隨著 Git 的更新而改變:
$ git log --pretty=format:"%h - %an, %ar : %s"
ca82a6d - Scott Chacon, 6 years ago : Change version number
085bb3b - Scott Chacon, 6 years ago : Remove unnecessary test
a11bef0 - Scott Chacon, 6 years ago : Initial commit
在oneline和format期權值與另一個特別有用log叫選項--graph。此選項添加了一個漂亮的小 ASCII 圖形,顯示您的分支和合并歷史記錄:
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 Add 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
除了輸出格式選項之外,git log還有許多有用的限制選項;也就是說,讓您只顯示提交子集的選項。您已經看到了一個這樣的選項 --2選項,它只顯示最后兩次提交。事實上,你可以做-<n>, wheren是任何整數來顯示最后一次n提交。實際上,您不太可能經常使用它,因為默認情況下 Git 將所有輸出都通過尋呼機進行管道傳輸,因此您一次只能看到一頁日志輸出。
但是,諸如--since和 之類的限時選項--until非常有用。例如,此命令獲取最近兩周的提交列表:
$ git log --since=2.weeks
此命令適用于多種格式 - 您可以指定特定日期,例如"2008-01-15",或相對日期,例如"2 years 1 day 3 minutes ago"。
您還可以過濾列表以匹配某些搜索條件的提交。該--author選項允許您過濾特定作者,該--grep選項允許您在提交消息中搜索關鍵字。
另一個真正有用的過濾器是-S選項(通俗地稱為 Git 的“pickaxe”選項),它接受一個字符串并僅顯示那些更改了該字符串出現次數的提交。例如,如果您想查找添加或刪除對特定函數的引用的最后一次提交,您可以調用:
$ git log -S function_name
git log作為過濾器傳遞的最后一個真正有用的選項是路徑。如果指定目錄或文件名,則可以將日志輸出限制為對這些文件進行更改的提交。這始終是最后一個選項,通常以雙破折號 ( --)開頭,以將路徑與選項分開:
$ git log -- path/to/file
以上就是關于“Git查看提交歷史”的介紹,如果大家想了解更多,可以關注動力節點Git教程,課程內容適合初學者學習,希望對大家有所幫助哦。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習