天天看點

git log指令全解析,打log還能這麼随心所欲!

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>...]      

這條指令有很多參數選項

一、不帶參數

  1. 如果不帶任何參數,它會列出所有曆史記錄,最近的排在最上方,顯示送出對象的哈希值,作者、送出日期、和送出說明
  2. 如果記錄過多,則按Page Up、Page Down、↓、↑來控制顯示
  3. 按q退出曆史記錄清單

二、顯示參數

  1. -p:按更新檔顯示每個更新間的差異,比下一條- -stat指令資訊更全
  2. --stat:顯示每次更新的修改檔案的統計資訊,每個送出都列出了修改過的檔案,以及其中添加和移除的行數,并在最後列出所有增減行數小計
  3. --shortstat:隻顯示--stat中最後的行數添加修改删除統計
  4. --name-only:盡在已修改的送出資訊後顯示檔案清單
  5. --name-status:顯示新增、修改和删除的檔案清單
  6. --abbrev-commit:僅顯示SHA-1的前幾個字元,而非所有的40個字元
  7. --relative-date:使用較短的相對時間顯示(例如:"two weeks ago")
  8. --graph:顯示ASCII圖形表示的分支合并曆史
  9. —pretty=:使用其他格式顯示曆史送出資訊,可選項有:oneline,short,medium,full,fuller,email,raw以及format:<string>,預設為medium,如:
    1. --pretty=oneline:一行顯示,隻顯示哈希值和送出說明(--online本身也可以作為單獨的屬性)
    2. --pretty=format:” ":控制顯示的記錄格式,如:
      1. %H  送出對象(commit)的完整哈希字串
      2. %h  送出對象的簡短哈希字串
      3. %T  樹對象(tree)的完整哈希字串
      4. %t  樹對象的簡短哈希字串
      5. %P  父對象(parent)的完整哈希字串
      6. %p  父對象的簡短哈希字串
      7. %an 作者(author)的名字
      8. %ae 作者的電子郵件位址
      9. %ad 作者修訂日期(可以用 -date= 選項定制格式)
      10. %ar 作者修訂日期,按多久以前的方式顯示
      11. %cn 送出者(committer)的名字
        1. 作者和送出者的差別不知道是啥?
        2. 作者與送出者的關系:作者是程式的修改者,送出者是代碼送出人(自己的修改不送出是怎麼能讓别人拉下來再送出的?)
        3. 其實作者指的是實際作出修改的人,送出者指的是最後将此工作成果送出到倉庫的人。是以,當你為某個項目釋出更新檔,然後某個核心成員将你的更新檔并入項目時,你就是作者,而那個核心成員就是送出者(soga)
      12. %ce 送出者的電子郵件位址
      13. %cd 送出日期(可以用 -date= 選項定制格式)
      14. %cr 送出日期,按多久以前的方式顯示
      15. %s  送出說明
    3. 帶顔色的--pretty=format:” ",這個另外寫出來分析
      1. 以這句為例:%Cred%h%Creset -%C(yellow)%d%Cblue %s %Cgreen(%cd) %C(bold blue)<%an>
      2. 它的效果是:   
        git log指令全解析,打log還能這麼随心所欲!
      3. 先斷句:[%Cred%h][%Creset   -][%C(yellow)%d ][%Cblue%s][%Cgreen(%cd)][%C(bold blue)<%an>]
      4. 然後就是很明顯能得到的規律了
        1. 一個顔色+一個内容
        2. 顔色以%C開頭,後邊接幾種顔色,還可以設定字型,如果要設定字型的話,要一塊加個括号
          1. 能設定的顔色值包括:reset(預設的灰色),normal, black, red, green, yellow, blue, magenta, cyan, white.
          2. 字型屬性則有bold, dim, ul, blink, reverse.  
        3. 内容可以是占位元字元,也可以是直接顯示的普通字元
  10. --date= (relative|local|default|iso|rfc|short|raw):定制後邊如果出現%ad或%cd時的日期格式
    1. 有幾個預設選項
      1. --date=relative:shows dates relative to the current time, e.g. "2 hours ago".
      2. --date=local:shows timestamps in user’s local timezone.
      3. --date=iso (or --date=iso8601):shows timestamps in ISO 8601 format.
      4. --date=rfc (or --date=rfc2822):shows timestamps in RFC 2822 format,often found in E-mail messages.
      5. --date=short:shows only date but not time, in YYYY-MM-DD format.這個挺好用
      6. --date=raw:shows the date in the internal raw git format %s %z format.
      7. --date=default:shows timestamps in the original timezone (either committer’s or author’s).
    2. 也可以自定義格式(需要git版本2.6.0以上),比如--date=format:'%Y-%m-%d %H:%M:%S' 會格式化成:2016-01-13 11:32:13,其他的格式化占位符如下:
      1. %a:Abbreviated weekday name
      2. %A:Full weekday name
      3. %b:Abbreviated month name
      4. %B:Full month name
      5. %c:Date and time representation appropriate for locale
      6. %d:Day of month as decimal number (01 – 31)
      7. %H: Hour in 24-hour format (00 – 23)
      8. %I:Hour in 12-hour format (01 – 12)
      9. %j:Day of year as decimal number (001 – 366)
      10. %m:Month as decimal number (01 – 12)
      11. %M:Minute as decimal number (00 – 59)
      12. %p:Current locale's A.M./P.M. indicator for 12-hour clock
      13. %S:Second as decimal number (00 – 59)
      14. %U:Week of year as decimal number, with Sunday as first day of week (00 – 53)
      15. %w:Weekday as decimal number (0 – 6; Sunday is 0)
      16. %W:Week of year as decimal number, with Monday as first day of week (00 – 53)
      17. %x:Date representation for current locale
      18. %X:Time representation for current locale
      19. %y:Year without century, as decimal number (00 – 99)
      20. %Y:Year with century, as decimal number
      21. %z, %Z:Either the time-zone name or time zone abbreviation, depending on registry settings; no characters if time zone is unknown
      22. %%:Percent sign

三、篩選參數:

  1. 按數量
    1. -n:顯示前n條log
  2. 按日期
    1. --after=
      1. 比如git log --after="2014-7-1”,顯示2014年7月1号之後的commit(包含7月1号)
      2. 後邊的日期還可以用相對時間表示,比如"1 week ago"和”yesterday",比如git log --after="yesterday"
      3. 這裡的格式可以是什麼?
    2. --before=
      1. 同上
      2. 另外這兩條指令可以同時使用表示時間段,比如git log --after="2014-7-1" --before="2014-7-4"
      3. 另外--since --until和 --after --before是一個意思,都可以用
  3. 按作者
    1. --author=
      1. 比如git log --author=“John",顯示John貢獻的commit
      2. 注意:作者名不需要精确比對,隻需要包含就行了
      3. 而且:可以使用正規表達式,比如git log --author="John\|Mary”,搜尋Marry和John貢獻的commit
      4. 而且:這個--author不僅包含名還包含email, 是以你可以用這個搜尋email
  4. 按commit描述
    1. --grep=
      1. 比如:git log --grep="JRA-224"
      2. 而且:可以傳入-i用來忽略大小寫
      3. 注意:如果想同時使用--grep和--author,必須在附加一個--all-match參數
  5. 按檔案
    1. - -(空格)或[沒有]
      1. 有時你可能隻對某個檔案的修改感興趣, 你隻想檢視跟某個檔案相關的曆史資訊, 你隻需要插入你感興趣檔案的路徑[對,是路徑,是以經常是不太好用]就可以了
      2. 比如:git log -- foo.py bar.py ,隻傳回和foo.py或bar.py相關的commit
      3. 這裡的--是告訴Git後面的參數是檔案路徑而不是branch的名字. 如果後面的檔案路徑不會和某個branch産生混淆, 你可以省略- -,比如git log foo.py 
      4. 另外,後邊的路徑還支援正則,比如:git log  *install.md 是,指定項目路徑下的所有以install.md結尾的檔案的送出曆史
      5. 另外,檔案名應該放到參數的最後位置,通常在前面加上--并用空格隔開表示是檔案
      6. 另外,git log file/ 檢視file檔案夾下所有檔案的送出記錄
  6. 按分支
    1. - -
      1. --branchName branchName為任意一個分支名字,檢視某個分支上的送出記錄
      2. 需要放到參數中的最後位置處
      3. 如果分支名與檔案名相同,系統會提示錯 誤,可通過--選項來指定給定的參數是分支名還是檔案名
        1. 比如:在目前分支中有一個名為v1的檔案,同時還存在一個名為v1的分支
        2. git log v1 -- 此時的v1代表的是分支名字(--後邊是空的)
        3. git log -- v1 此時的v1代表的是名為v1的檔案
        4. git log v1 -- v1 代表v1分支下的v1檔案
  7. 按内容
    1. -S"<string>"、-G"<string>"
      1. 有時你想搜尋和新增或删除某行代碼相關的commit. 可以使用這條指令
      2. 假設你想知道Hello, World!這句話是什麼時候加入到項目裡去的,可以用:git log -S"Hello,World!"
      3. 另外:如果你想使用正規表達式去比對而不是字元串, 那麼你可以使用-G代替-S.
      4. 這是一個非常有用的debug工具, 使用他你可以定位所有跟某行代碼相關的commit. 甚至可以檢視某行是什麼時候被copy的, 什麼時候移到另外一個檔案中去的
      5. 注:-S後沒有"=",與查詢内容之間也沒有空格符
  8. 按範圍
    1. git log <since>..<until>
      1. 這個指令可以檢視某個範圍的commit
      2. 這個指令非常有用當你使用branch做為range參數的時候. 能很友善的顯示2個branch之間的不同
      3. 比如:git log master..feature,master..feature這個range包含了在feature有而在master沒有的所有commit,同樣,如果是feature..master包含所有master有但是feature沒有的commit
      4. 另外,如果是三個點,表示或的意思:git log master...test 查詢master或test分支中的送出記錄
  9. 過濾掉merge commit
    1. --no-merges
      1. 預設情況下git log會輸出merge commit.  你可以通過--no-merges标記來過濾掉merge commit,git log --no-merges
      2. 另外,如果你隻對merge commit感興趣可以使用—merges,git log --merges
  10. 按标簽tag
    1. git log v1.0
      1. 直接這樣是查詢标簽之前的commit
      2. 加兩個點git log v1.0.. 查詢從v1.0以後的送出曆史記錄(不包含v1.0)
  11. 按commit
    1. git log commit :查詢commit之前的記錄,包含commit
    2. git log commit1 commit2:查詢commit1與commit2之間的記錄,包括commit1和commit2
    3. git log commit1..commit2:同上,但是不包括commit1
      1. 其中,commit可以是送出哈希值的簡寫模式,也可以使用HEAD代替
        1. HEAD代表最後一次送出,HEAD^為最後一個送出的父送出,等同于HEAD~1
        2. HEAD~2代表倒數第二次送出

最後幹貨,你會喜歡的~

下面第一條的效果是這樣:

git log指令全解析,打log還能這麼随心所欲!
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