git log命令全解析

 轉自:https://www.cnblogs.com/bellkosmos/p/5923439.html

git log命令非常強大而好用,在複雜系統的版本管理中扮演着重要的角色,但默認的git log命令顯示出的東西實在太醜,不好好打扮一下根本沒法見人,打扮好了用alias命令拍個照片,就正式出道了!
 
下面先詳細而系統地介紹git log的所有配置知識(用我一向簡潔清晰的表述方式),熟悉了這些東西,你就可以自由配置自己美麗的git log了~
最後上個乾貨,直接給一個我打扮好的alias配置,懶人直接跳到最後吧 !
 
(轉載請註明:博客園-閣剛廣志,地址:http://www.cnblogs.com/bellkosmos/p/5923439.html )
 
git log用於查詢版本的歷史,命令形式如下:
git log [<options>] [<since>..<until>] [[--] <path>...]
 

這條命令有很多參數選項
一、不帶參數
如果不帶任何參數,它會列出所有歷史記錄,最近的排在最上方,顯示提交對象的哈希值,作者、提交日期、和提交說明
如果記錄過多,則按Page Up、Page Down、↓、↑來控制顯示
按q退出歷史記錄列表
 
二、顯示參數
-p:按補丁顯示每個更新間的差異,比下一條- -stat命令信息更全
--stat:顯示每次更新的修改文件的統計信息,每個提交都列出了修改過的文件,以及其中添加和移除的行數,並在最後列出所有增減行數小計
--shortstat:只顯示--stat中最後的行數添加修改刪除統計
--name-only:盡在已修改的提交信息後顯示文件清單
--name-status:顯示新增、修改和刪除的文件清單
--abbrev-commit:僅顯示SHA-1的前幾個字符,而非所有的40個字符
--relative-date:使用較短的相對時間顯示(例如:"two weeks ago")
--graph:顯示ASCII圖形表示的分支合併歷史
—pretty=:使用其他格式顯示歷史提交信息,可選項有:oneline,short,medium,full,fuller,email,raw以及format:<string>,默認爲medium,如:
--pretty=oneline:一行顯示,只顯示哈希值和提交說明(--online本身也可以作爲單獨的屬性)
--pretty=format:” ":控制顯示的記錄格式,如:
%H  提交對象(commit)的完整哈希字串
%h  提交對象的簡短哈希字串
%T  樹對象(tree)的完整哈希字串
%t  樹對象的簡短哈希字串
%P  父對象(parent)的完整哈希字串
%p  父對象的簡短哈希字串
%an 作者(author)的名字
%ae 作者的電子郵件地址
%ad 作者修訂日期(可以用 -date= 選項定製格式)
%ar 作者修訂日期,按多久以前的方式顯示
%cn 提交者(committer)的名字
作者和提交者的區別不知道是啥?
作者與提交者的關係:作者是程序的修改者,提交者是代碼提交人(自己的修改不提交是怎麼能讓別人拉下來再提交的?)
其實作者指的是實際作出修改的人,提交者指的是最後將此工作成果提交到倉庫的人。所以,當你爲某個項目發佈補丁,然後某個核心成員將你的補丁併入項目時,你就是作者,而那個核心成員就是提交者(soga)
%ce 提交者的電子郵件地址
%cd 提交日期(可以用 -date= 選項定製格式)
%cr 提交日期,按多久以前的方式顯示
%s  提交說明
帶顏色的--pretty=format:” ",這個另外寫出來分析
以這句爲例:%Cred%h%Creset -%C(yellow)%d%Cblue %s %Cgreen(%cd) %C(bold blue)<%an>
它的效果是:   
 

先斷句:[%Cred%h][%Creset   -][%C(yellow)%d ][%Cblue%s][%Cgreen(%cd)][%C(bold blue)<%an>]
然後就是很明顯能得到的規律了
一個顏色+一個內容
顏色以%C開頭,後邊接幾種顏色,還可以設置字體,如果要設置字體的話,要一塊加個括號
能設置的顏色值包括:reset(默認的灰色),normal, black, red, green, yellow, blue, magenta, cyan, white.
字體屬性則有bold, dim, ul, blink, reverse.  
內容可以是佔位元字符,也可以是直接顯示的普通字符
--date= (relative|local|default|iso|rfc|short|raw):定製後邊如果出現%ad或%cd時的日期格式
有幾個默認選項
--date=relative:shows dates relative to the current time, e.g. "2 hours ago".
--date=local:shows timestamps in user’s local timezone.
--date=iso (or --date=iso8601):shows timestamps in ISO 8601 format.
--date=rfc (or --date=rfc2822):shows timestamps in RFC 2822 format,often found in E-mail messages.
--date=short:shows only date but not time, in YYYY-MM-DD format.這個挺好用
--date=raw:shows the date in the internal raw git format %s %z format.
--date=default:shows timestamps in the original timezone (either committer’s or author’s).
也可以自定義格式(需要git版本2.6.0以上),比如--date=format:'%Y-%m-%d %H:%M:%S' 會格式化成:2016-01-13 11:32:13,其他的格式化佔位符如下:
%a:Abbreviated weekday name
%A:Full weekday name
%b:Abbreviated month name
%B:Full month name
%c:Date and time representation appropriate for locale
%d:Day of month as decimal number (01 – 31)
%H: Hour in 24-hour format (00 – 23)
%I:Hour in 12-hour format (01 – 12)
%j:Day of year as decimal number (001 – 366)
%m:Month as decimal number (01 – 12)
%M:Minute as decimal number (00 – 59)
%p:Current locale's A.M./P.M. indicator for 12-hour clock
%S:Second as decimal number (00 – 59)
%U:Week of year as decimal number, with Sunday as first day of week (00 – 53)
%w:Weekday as decimal number (0 – 6; Sunday is 0)
%W:Week of year as decimal number, with Monday as first day of week (00 – 53)
%x:Date representation for current locale
%X:Time representation for current locale
%y:Year without century, as decimal number (00 – 99)
%Y:Year with century, as decimal number
%z, %Z:Either the time-zone name or time zone abbreviation, depending on registry settings; no characters if time zone is unknown
%%:Percent sign
 
三、篩選參數:
按數量
-n:顯示前n條log
按日期
--after=
比如git log --after="2014-7-1”,顯示2014年7月1號之後的commit(包含7月1號)
後邊的日期還可以用相對時間表示,比如"1 week ago"和”yesterday",比如git log --after="yesterday"
這裏的格式可以是什麼?
--before=
同上
另外這兩條命令可以同時使用表示時間段,比如git log --after="2014-7-1" --before="2014-7-4"
另外--since --until和 --after --before是一個意思,都可以用
按作者
--author=
比如git log --author=“John",顯示John貢獻的commit
注意:作者名不需要精確匹配,只需要包含就行了
而且:可以使用正則表達式,比如git log --author="John\|Mary”,搜索Marry和John貢獻的commit
而且:這個--author不僅包含名還包含email, 所以你可以用這個搜索email
按commit描述
--grep=
比如:git log --grep="JRA-224"
而且:可以傳入-i用來忽略大小寫
注意:如果想同時使用--grep和--author,必須在附加一個--all-match參數
按文件
- -(空格)或[沒有]
有時你可能只對某個文件的修改感興趣, 你只想查看跟某個文件相關的歷史信息, 你只需要插入你感興趣文件的路徑[對,是路徑,所以經常是不太好用]就可以了
比如:git log -- foo.py bar.py ,只返回和foo.py或bar.py相關的commit
這裏的--是告訴Git後面的參數是文件路徑而不是branch的名字. 如果後面的文件路徑不會和某個branch產生混淆, 你可以省略- -,比如git log foo.py 
另外,後邊的路徑還支持正則,比如:git log  *install.md 是,指定項目路徑下的所有以install.md結尾的文件的提交歷史
另外,文件名應該放到參數的最後位置,通常在前面加上--並用空格隔開表示是文件
另外,git log file/ 查看file文件夾下所有文件的提交記錄
按分支
- -
--branchName branchName爲任意一個分支名字,查看某個分支上的提交記錄
需要放到參數中的最後位置處
如果分支名與文件名相同,系統會提示錯 誤,可通過--選項來指定給定的參數是分支名還是文件名
比如:在當前分支中有一個名爲v1的文件,同時還存在一個名爲v1的分支
git log v1 -- 此時的v1代表的是分支名字(--後邊是空的)
git log -- v1 此時的v1代表的是名爲v1的文件
git log v1 -- v1 代表v1分支下的v1文件
按內容
-S"<string>"、-G"<string>"
有時你想搜索和新增或刪除某行代碼相關的commit. 可以使用這條命令
假設你想知道Hello, World!這句話是什麼時候加入到項目裏去的,可以用:git log -S"Hello,World!"
另外:如果你想使用正則表達式去匹配而不是字符串, 那麼你可以使用-G代替-S.
這是一個非常有用的debug工具, 使用他你可以定位所有跟某行代碼相關的commit. 甚至可以查看某行是什麼時候被copy的, 什麼時候移到另外一個文件中去的
注:-S後沒有"=",與查詢內容之間也沒有空格符
按範圍
git log <since>..<until>
這個命令可以查看某個範圍的commit
這個命令非常有用當你使用branch做爲range參數的時候. 能很方便的顯示2個branch之間的不同
比如:git log master..feature,master..feature這個range包含了在feature有而在master沒有的所有commit,同樣,如果是feature..master包含所有master有但是feature沒有的commit
另外,如果是三個點,表示或的意思:git log master...test 查詢master或test分支中的提交記錄
過濾掉merge commit
--no-merges
默認情況下git log會輸出merge commit.  你可以通過--no-merges標記來過濾掉merge commit,git log --no-merges
另外,如果你只對merge commit感興趣可以使用—merges,git log --merges
按標籤tag
git log v1.0
直接這樣是查詢標籤之前的commit
加兩個點git log v1.0.. 查詢從v1.0以後的提交歷史記錄(不包含v1.0)
按commit
git log commit :查詢commit之前的記錄,包含commit
git log commit1 commit2:查詢commit1與commit2之間的記錄,包括commit1和commit2
git log commit1..commit2:同上,但是不包括commit1
其中,commit可以是提交哈希值的簡寫模式,也可以使用HEAD代替
HEAD代表最後一次提交,HEAD^爲最後一個提交的父提交,等同於HEAD~1
HEAD~2代表倒數第二次提交

 



git config --global alias.lm  "log --no-merges --color --date=format:'%Y-%m-%d %H:%M:%S' --author='你的名字!自己修改!' --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Cblue %s %Cgreen(%cd) %C(bold blue)<%an>%Creset' --abbrev-commit"


git config --global alias.lms  "log --no-merges --color --stat --date=format:'%Y-%m-%d %H:%M:%S' --author='你的名字!自己修改!' --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Cblue %s %Cgreen(%cd) %C(bold blue)<%an>%Creset' --abbrev-commit"


git config --global alias.ls "log --no-merges --color --graph --date=format:'%Y-%m-%d %H:%M:%S' --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Cblue %s %Cgreen(%cd) %C(bold blue)<%an>%Creset' --abbrev-commit"


git config --global alias.lss "log --no-merges --color --stat --graph --date=format:'%Y-%m-%d %H:%M:%S' --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Cblue %s %Cgreen(%cd) %C(bold blue)<%an>%Creset' --abbrev-commit"

 

 

 

發佈了272 篇原創文章 · 獲贊 37 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章