標籤:分頁 哪些 更新 複製 路徑名 引用 清單 err 常用選項
1、查看提交曆史
在提交了若干更新,又或者複製了某個項目之後,你也許想回顧下提交曆史。完成這個任務最簡單而又有效工具是 git log
命令。
$ git log
commit ca82a6dff817ec66f44342007202690a93763949Author: Scott Chacon <[email protected]>Date: Mon Mar 17 21:52:11 2008 -0700 changed the version numbercommit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7Author: Scott Chacon <[email protected]>Date: Sat Mar 15 16:40:33 2008 -0700 removed unnecessary testcommit a11bef06a3f659402fe7563abf99ad00de2209e6Author: Scott Chacon <[email protected]>Date: Sat Mar 15 10:31:28 2008 -0700 first commit
預設不用任何參數的話,git log
會按提交時間列出所有的更新,最近的更新排在最上面。正如你所看到的,這個命令會列出每個提交的 SHA-1 校正和、作者的名字和電子郵件地址、提交時間以及提交說明。
2、查看特定的提交曆史2.1 定製輸出格式
git log
的常用選項列出了我們目前涉及到的和沒涉及到的選項,以及它們是如何影響 log 命令的輸出的。
選項 | 說明------------------|------------------------------------------- -p | 按補丁格式顯示每個更新之間的差異 --stat | 顯示每次更新的檔案修改統計資訊 --shortstat | 只顯示 --stat 中最後的行數修改添加移除統計 --name-only | 僅在提交資訊後顯示已修改的檔案清單 --name-status | 顯示新增、修改、刪除的檔案清單 --abbrev-commit | 僅顯示 SHA-1 的前幾個字元,而非所有的 40 個字元 --relative-date | 使用較短的相對時間顯示(比如,“2 weeks ago”) --graph | 顯示 ASCII 圖形表示的分支合并曆史 --pretty | 使用其他格式顯示曆史提交資訊
1)一個常用的選項是 -p
,用來顯示每次提交的內容差異。該選項除了顯示基本資料之外,還附帶了每次 commit 的變化。
$ git log -p -2
commit ca82a6dff817ec66f44342007202690a93763949Author: Scott Chacon <[email protected]>Date: Mon Mar 17 21:52:11 2008 -0700 changed the version numberdiff --git a/Rakefile b/Rakefileindex 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 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7Author: Scott Chacon <[email protected]>Date: Sat Mar 15 16:40:33 2008 -0700 removed unnecessary testdiff --git a/lib/simplegit.rb b/lib/simplegit.rbindex 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
2)你想看到每次提交的簡略的統計資訊,你可以使用 --stat
選項。在每次提交的下面列出所有被修改過的檔案、有多少檔案被修改了以及被修改過的檔案的哪些行被移除或是添加了。在每次提交的最後還有一個總結。
$ git log --stat
commit ca82a6dff817ec66f44342007202690a93763949Author: 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 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7Author: 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 a11bef06a3f659402fe7563abf99ad00de2209e6Author: 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(+)
3)另外一個常用的選項是 --pretty
。這個選項可以指定使用不同於預設格式的方式展示提交曆史。這個選項有一些內建的子選項供你使用。比如用 oneline 將每個提交放在一行顯示,查看的提交數很大時非常有用。另外還有 short,full 和 fuller 可以用,展示的資訊或多或少有些不同。
$ git log --pretty=oneline
ca82a6dff817ec66f44342007202690a93763949 changed the version number085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7 removed unnecessary testa11bef06a3f659402fe7563abf99ad00de2209e6 first commit
但最有意思的是 format
,可以定製要顯示的記錄格式。 這樣的輸出對後期提取分析格外有用
$ git log --pretty=format:"%h - %an, %ar : %s"
ca82a6d - Scott Chacon, 6 years ago : changed the version number085bb3b - Scott Chacon, 6 years ago : removed unnecessary testa11bef0 - Scott Chacon, 6 years ago : first commit
git log --pretty=format
常用的選項列出了常用的格式預留位置寫法及其代表的意義。
選項 | 說明------|------------------------------------- %H | 提交對象(commit)的完整雜湊字串 %h | 提交對象的簡短雜湊字串 %T | 樹對象(tree)的完整雜湊字串 %t | 樹對象的簡短雜湊字串 %P | 父物件(parent)的完整雜湊字串 %p | 父物件的簡短雜湊字串 %an | 作者(author)的名字 %ae | 作者的電子郵件地址 %ad | 作者修訂日期(可以用 --date= 選項定製格式) %ar | 作者修訂日期,按多久以前的方式顯示 %cn | 提交者(committer)的名字 %ce | 提交者的電子郵件地址 %cd | 提交日期 %cr | 提交日期,按多久以前的方式顯示 %s | 提交說明
當 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 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
2.2 限制輸出長度
除了定製輸出格式的選項之外,git log
還有許多非常實用的限制輸出長度的選項,也就是只輸出部分提交資訊。
選項 | 說明--------------------|----------------------------- -(n) | 僅顯示最近的 n 條提交 --since, --after | 僅顯示指定時間之後的提交。 --until, --before | 僅顯示指定時間之前的提交。 --author | 僅顯示指定作者相關的提交。 --committer | 僅顯示指定提交者相關的提交。 --grep | 僅顯示含指定關鍵字的提交 -S | 僅顯示添加或移除了某個關鍵字的提交
1)之前你已經看到過 -2
了,它只顯示最近的兩條提交, 實際上,這是 -<n>
寫法,其中的 n 可以是任何整數,表示僅顯示最近的若干條提交。不過實踐中我們是不太用這個選項的,Git 在輸出所有提交時會自動調用分頁程式,所以你一次只會看到一頁的內容。
2)另外還有按照時間作限制的選項,比如 --since
和 --until
也很有用。這個命令可以在多種格式下工作,比如說具體的某一天 "2008-01-15",或者是相對地多久以前 "2 years 1 day 3 minutes ago"。
$ git log --since=2.weeks
3)還可以給出若干搜尋條件,列出符合的提交。用 --author
選項顯示指定作者的提交,用 --grep
選項搜尋提交說明中的關鍵字。(請注意,如果要得到同時滿足這兩個選項搜尋條件的提交,就必須用 --all-match
選項。否則,滿足任意一個條件的提交都會被匹配出來)。
4)另一個非常有用的篩選選項是 -S
,可以列出那些添加或移除了某些字串的提交。比如說,你想找出添加或移除了某一個特定函數的引用的提交,你可以這樣使用。
$ git log -Sfunction_name
5)最後一個很實用的 git log
選項是路徑(path),如果只關心某些檔案或者目錄的曆史提交,可以在 git log
選項的最後指定它們的路徑。因為是放在最後位置上的選項,所以用兩個短劃線(--)隔開之前的選項和後面限定的路徑名。
來看一個實際的例子,如果要查看 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 useacd3b9e - Enhance hold_lock_file_for_{update,append}() APIf563754 - demonstrate breakage of detached checkout with symbolic link HEADd1a43f2 - reset --hard/read-tree --reset -u: remove unmerged new paths51a94af - Fix "checkout --track -b newbranch" on detached HEADb0ad11e - pull: allow "git pull origin $something:$current_branch" into an unborn branch
iOS - Git 查看提交曆史(分布式版本控制系統)