Git 檢視提交歷史

Git 檢視提交歷史

Git 提交歷史一般常用兩個命令:
  • git log - 檢視歷史提交記錄。
  • git blame <file> - 以列表形式檢視指定檔案的歷史修改記錄。
  • git log

    在使用 Git 提交了若干更新之後,又或者克隆了某個專案,想回顧下提交歷史,我們可以使用 git log 命令檢視。

    針對我們前一章節的操作,使用 git log 命令列出歷史提交記錄如下:

    $ git log
    commit d5e9fc2c811e0ca2b2d28506ef7dc14171a207d9 (HEAD -> master)
    Merge: c68142b 7774248
    Author: itread01 <[email protected]>
    Date:   Fri May 3 15:55:58 2019 +0800
    
        Merge branch 'change_site'
    
    commit c68142b562c260c3071754623b08e2657b4c6d5b
    Author: itread01 <[email protected]>
    Date:   Fri May 3 15:52:12 2019 +0800
    
        修改程式碼
    
    commit 777424832e714cf65d3be79b50a4717aea51ab69 (change_site)
    Author: itread01 <[email protected]>
    Date:   Fri May 3 15:49:26 2019 +0800
    
        changed the itread01.php
    
    commit c1501a244676ff55e7cccac1ecac0e18cbf6cb00
    Author: itread01 <[email protected]>
    Date:   Fri May 3 15:35:32 2019 +0800
    

    我們可以用 --oneline 選項來檢視歷史記錄的簡潔的版本。

    $ git log --oneline
    $ git log --oneline
    d5e9fc2 (HEAD -> master) Merge branch 'change_site'
    c68142b 修改程式碼
    7774248 (change_site) changed the itread01.php
    c1501a2 removed test.txt、add itread01.php
    3e92c19 add test.txt
    3b58100 第一次版本提交
    

    這告訴我們的是,此專案的開發歷史。

    我們還可以用 --graph 選項,檢視歷史中什麼時候出現了分支、合併。以下為相同的命令,開啟了拓撲圖選項:

    *   d5e9fc2 (HEAD -> master) Merge branch 'change_site'
    |\  
    | * 7774248 (change_site) changed the itread01.php
    * | c68142b 修改程式碼
    |/  
    * c1501a2 removed test.txt、add itread01.php
    * 3e92c19 add test.txt
    * 3b58100 第一次版本提交
    

    現在我們可以更清楚明瞭地看到何時工作分叉、又何時歸併。

    你也可以用 --reverse 引數來逆向顯示所有日誌。

    $ git log --reverse --oneline
    3b58100 第一次版本提交
    3e92c19 add test.txt
    c1501a2 removed test.txt、add itread01.php
    7774248 (change_site) changed the itread01.php
    c68142b 修改程式碼
    d5e9fc2 (HEAD -> master) Merge branch 'change_site'
    

    如果只想查詢指定使用者的提交日誌可以使用命令:git log --author , 例如,比方說我們要找 Git 原始碼中 Linus 提交的部分:

    $ git log --author=Linus --oneline -5
    81b50f3 Move 'builtin-*' into a 'builtin/' subdirectory
    3bb7256 make "index-pack" a built-in
    377d027 make "git pack-redundant" a built-in
    b532581 make "git unpack-file" a built-in
    112dd51 make "mktag" a built-in
    

    如果你要指定日期,可以執行幾個選項:--since 和 --before,但是你也可以用 --until 和 --after。

    例如,如果我要看 Git 專案中三週前且在四月十八日之後的所有提交,我可以執行這個(我還用了 --no-merges 選項以隱藏合併提交):

    $ git log --oneline --before={3.weeks.ago} --after={2010-04-18} --no-merges
    5469e2d Git 1.7.1-rc2
    d43427d Documentation/remote-helpers: Fix typos and improve language
    272a36b Fixup: Second argument may be any arbitrary string
    b6c8d2d Documentation/remote-helpers: Add invocation section
    5ce4f4e Documentation/urls: Rewrite to accomodate transport::address
    00b84e9 Documentation/remote-helpers: Rewrite description
    03aa87e Documentation: Describe other situations where -z affects git diff
    77bc694 rebase-interactive: silence warning when no commits rewritten
    636db2c t3301: add tests to use --format="%N"
    

    更多 git log 命令可檢視:http://git-scm.com/docs/git-log

    git blame

    如果要檢視指定檔案的修改記錄可以使用 git blame 命令,格式如下:
    git blame <file>

    git blame 命令是以列表形式顯示修改記錄,如下例項:

    $ git blame README 
    ^d2097aa (tianqixin 2020-08-25 14:59:25 +0800 1) # itread01 Git 測試
    db9315b0 (itread01    2020-08-25 16:00:23 +0800 2) # 入門教學