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>
- 它的效果是:
git log指令全解析,打log還能這麼随心所欲! - 先斷句:[%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是一個意思,都可以用
- --after=
- 按作者
- --author=
- 比如git log --author=“John",顯示John貢獻的commit
- 注意:作者名不需要精确比對,隻需要包含就行了
- 而且:可以使用正規表達式,比如git log --author="John\|Mary”,搜尋Marry和John貢獻的commit
- 而且:這個--author不僅包含名還包含email, 是以你可以用這個搜尋email
- --author=
- 按commit描述
- --grep=
- 比如:git log --grep="JRA-224"
- 而且:可以傳入-i用來忽略大小寫
- 注意:如果想同時使用--grep和--author,必須在附加一個--all-match參數
- --grep=
- 按檔案
- - -(空格)或[沒有]
- 有時你可能隻對某個檔案的修改感興趣, 你隻想檢視跟某個檔案相關的曆史資訊, 你隻需要插入你感興趣檔案的路徑[對,是路徑,是以經常是不太好用]就可以了
- 比如: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後沒有"=",與查詢内容之間也沒有空格符
- -S"<string>"、-G"<string>"
- 按範圍
- 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分支中的送出記錄
- git log <since>..<until>
- 過濾掉merge commit
- --no-merges
- 預設情況下git log會輸出merge commit. 你可以通過--no-merges标記來過濾掉merge commit,git log --no-merges
- 另外,如果你隻對merge commit感興趣可以使用—merges,git log --merges
- --no-merges
- 按标簽tag
- git log v1.0
- 直接這樣是查詢标簽之前的commit
- 加兩個點git log v1.0.. 查詢從v1.0以後的送出曆史記錄(不包含v1.0)
- git log 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代表倒數第二次送出
- 其中,commit可以是送出哈希值的簡寫模式,也可以使用HEAD代替
最後幹貨,你會喜歡的~
下面第一條的效果是這樣:
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"
參考資料:
http://www.cnblogs.com/irocker/p/advanced-git-log.html
http://stackoverflow.com/questions/7853332/git-log-date-formats
https://git-scm.com/docs/git-log