Git - 檢視提交歷史


Git 能夠詳細記錄專案隨時間推移所做的更改歷史。透過在每次提交中記錄每個更改,Git 允許開發人員跟蹤、還原並瞭解其程式碼庫的演變過程。

提交歷史對於從事專案的個人開發人員和團隊來說都是關鍵資源,使他們能夠審查過去的更改、除錯問題並有效地協作。

提交歷史的重要性

Git 中的提交歷史本質上是快照的時間線,每個快照都代表專案開發中的一個點。透過檢視提交歷史,您可以:

  • 瞭解專案的演變:檢視程式碼庫如何隨時間推移發生變化以及誰進行了這些更改。

  • 追蹤錯誤:透過審查具體的提交來找出錯誤何時何地被引入。

  • 回滾到以前的版本:如果最近的更改導致問題,您可以回滾到程式碼的早期穩定版本。

  • 高效協作:團隊可以看到其他人正在做什麼,審查程式碼更改,並確保專案按計劃進行。

`git log` 命令

您可以使用 **git log** 命令檢查 Git 中的提交歷史,該命令提供了對所做更改的完整概述,從而更容易跟蹤開發進度。

**git log** 顯示提交列表,從最新的提交開始,以及提交雜湊、作者、日期和提交訊息等資訊。

要在 Git 中檢視提交歷史記錄,請執行以下命令:

$ git log
$ git log
commit 94744071e847ab78390eb1b8a5e013dbeee7ced2 (HEAD -> main, origin/main, origin/HEAD)
Author: Tutorialspoint <tutorialspoint@example.com>
Date:   Wed Aug 07 17:51:17 2024 +0530

   modified the margin and font size

commit 1bc52022c8fe3ee799ccf36bdc3e6ec0c99bb9f5
Author: Tutorialspoint <tutorialspoint@example.com>
Date:   Wed Aug 07 13:01:47 2024 +0530

   changed the width dimension

commit a98962782a8bc6ac91b793b634eb5786b3fb85fc
Author: tonystark <86562149+tonystark@users.noreply.github.com>
Date:   Sat Dec 10 08:35:10 2022 +0000

   Initial commit
:...skipping...

這個簡單的日誌檢視為您提供了一個線性的提交歷史,最新的更改顯示在頂部。

**git log** 命令顯示預設的提交歷史檢視,其中包括:

  • 提交雜湊:提交的唯一識別符號(SHA-1 雜湊)。

  • 作者:進行提交的人員的姓名和電子郵件地址。

  • 日期:提交的日期和時間。

  • 提交訊息:作者提供的簡短描述,解釋提交包含的內容。

Git log 命令顯示了幾種檢視倉庫歷史記錄的方法。

按檔案瀏覽提交歷史

有時,您可能希望檢視特定檔案的歷史記錄,而不是整個專案。Git 允許您使用 git log 命令和檔名來實現這一點。

git log <file>

如果您想檢視影響檔案的每個提交的差異,您可以新增 **-p** 標誌。

$ git log -p -2

在上例中,**-p** 選項顯示提交的詳細檢視,**-2** 顯示最後兩次提交。

$ git log -p -2
commit 45ef43e5c3664f9ff725ad7b41a80af6ab515 (HEAD -> main, origin/main, origin/HEAD)
Author: Tutorialspoint <tutorialspoint@example.com>
Date:   Wed Aug 07 17:51:17 2024 +0530

   changed top and right dimensions

diff --git a/main.html b/main.html
index f7f85dd..9b97bad 100644
--- a/main.html
+++ b/main.html
@@ -46,8 +46,8 @@
            display : inline-block;
            border  : 2px solid green;
            position: absolute;
-           right:45px;
            top: 36px;
+           right:42px; ^M
+           top:35px; ^M
:...skipping...
commit 45ef43e5c3664f9ff725ad7b41a80af6ab515 (Head -> main, origin/main, origin/HEAD)
Author: Tutorialspoint <tutorialspoint@example.com>
Date:   Wed Aug 07 18:56:17 2024 +0530

   changed top and right dimensions

diff --git a/main.html b/main.html
index f7f85dd..9b97bad 100644
--- a/main.html
+++ b/main.html
@@ -46,8 +46,8 @@

您可以使用 **git log --stat** 命令顯示每次提交所做更改的簡要概述,包括更改的檔案數、新增或刪除的行數以及檔名。

$ git log --stat
$ git log --stat
commit 45ef43e5c3664f9ff725ad7b41a80af6ab515 (HEAD -> main, origin/main, origin/HEAD)
Author: Tutorialspoint <tutorialspoint@example.com>
Date:   Wed Aug 07 17:51:17 2024 +0530

   changed top and right dimensions

main.html  |  4 ++--
1 file changed, 2 insertions(+), 2 deletion(-)

commit 94744071e847ab78390eb1b8a5e013dbeee7ced2
Author: Tutorialspoint <tutorialspoint@example.com>
Date:   Wed Aug 07 17:51:17 2024 +0530

   modified the margin and font size

main.html  |  4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

commit 1bc52022c8fe3ee799ccf36bdc3e6ec0c99bb9f5
Author: Tutorialspoint <tutorialspoint@example.com>
:...skipping...
commit 45ef43e5c3664f9ff725ad7b41a80af6ab515 (Head -> main, origin/main, origin/HEAD)
Author: Tutorialspoint <tutorialspoint@example.com>
Date:   Wed Aug 07 13:01:47 2024 +0530

   changed top and right dimensions

如果您想完整了解每次提交所做的所有更改,包括新增、刪除或修改的具體行。

$ git log --stat -p
$ git log --stat -p
commit 45ef43e5c3664f9ff725ad7b41a80af6ab515 (HEAD -> main, origin/main, origin/HEAD)
Author: Tutorialspoint <tutorialspoint@example.com>
Date:   Wed Aug 07 17:51:17 2024 +0530

   changed top and right dimensions
---
main.html  |  4 ++--
1 file changed, 2 insertions(+), 2 deletion(-)

diff --git a/main.html b/main.html
index f7f85dd..9b97bad 100644
--- a/main.html
+++ b/main.html
@@ -46,8 +46,8 @@
            display : inline-block;
            border  : 2px solid green;
            position: absolute;
:...skipping...
commit 45ef43e5c3664f9ff725ad7b41a80af6ab515 (Head -> main, origin/main, origin/HEAD)
Author: Tutorialspoint <tutorialspoint@example.com>
Date:   Wed Aug 07 18:56:17 2024 +0530

自定義 `git log` 輸出

雖然 **git log** 的預設輸出很有用,但您可以根據需要自定義它以顯示更多或更少的資訊。Git 提供了多個選項來格式化和篩選提交歷史記錄。

單行日誌

如果您希望更簡潔地檢視提交歷史,則每一提交顯示在一行中,您可以使用 **--oneline** 標誌。這對於快速概述歷史記錄特別有用。

$ git log --oneline

示例輸出

abc1234 Add user authentication feature
def7890 Update README with installation instructions

此檢視僅顯示簡寫的提交雜湊和提交訊息,便於快速掃描歷史記錄。

按日期或作者篩選

您可以按日期、作者或兩者的組合篩選提交歷史記錄。當您嘗試查詢特定提交或檢視特定時間段內所做的更改時,這非常有用。

按日期篩選:要顯示自特定日期以來進行的提交,請使用 **--since** 選項。

git log --since="2024-08-01"

按作者篩選:要檢視特定作者所做的提交,請使用 **--author** 選項。

git log --author="Tutorialspoint "

您可以組合這些選項以進一步縮小歷史記錄範圍。例如,要檢視 Tutorialspoint 自 2024 年 8 月 1 日以來所做的所有提交:

git log --author="Tutorialspoint" --since="2024-08-01"

格式化提交輸出

Git 允許您使用 --pretty 選項自定義每個提交的顯示方式。您可以指定格式或使用 Git 的預定義格式之一。

簡短格式

git log --pretty=short

自定義格式:您可以指定要檢視提交的哪些元素。

git log --pretty=format:"%h - %an, %ar : %s"

命令 **git log --pretty=format:"%h - %an, %ar : %s"** 用於以簡單的方式顯示關於每個提交的特定資訊。它將以簡寫的提交雜湊、作者、日期和提交訊息顯示每一提交。

  • %h:提交雜湊

  • %an:作者姓名

  • %ar:日期

  • %s:提交訊息

$ git log --pretty=format:"%h - %an, %ar : %s"
$ git log --pretty=format:"%h - %an, %ar : %s"
09708ca - Tutorialspoint, 15 minutes ago : color and heading changed
45ef43e - Tutorialspoint, 18 hours ago : changed top and right dimensions
9474407 - Tutorialspoint, 19 hours ago : modified the margin and font size
1bc5202 - Tutorialspoint, 23 hours ago : changed the width dimension
a989627 - tonystark, 1 year, 5 months ago : Initial commit

下表顯示了可與 **git log --pretty=format** 命令一起使用的有用的說明符。

說明符 輸出描述
%H 提交雜湊
%h 簡寫提交雜湊
%T 樹雜湊
%t 簡寫樹雜湊
%P 父雜湊
%p 簡寫父雜湊
%an 作者姓名
%ae 作者郵箱
%ad 作者日期
%ar 作者日期(相對)
%cn 提交者姓名
%ce 提交者郵箱
%cd 提交者日期
%cr 提交者日期(相對)
%s 主題
  • 當您將 **--oneline** 或 **--format** 選項與 **--graph** 結合使用時,Git 會在每個提交語句旁邊顯示一個 ASCII 圖表。

  • 命令 **git log --pretty=format:"%h %s" --graph** 以分支和合並歷史的視覺化表示形式顯示每個提交的簡寫雜湊和提交訊息。

$ git log --pretty=format:"%h %s" --graph
$ git log --pretty=format:"%h %s" --graph
* 09708ca color and heading changed
* 45ef43e changed top and right dimensions
* 9474407 modified the margin and font size
* 1bc5202 changed the width dimension
* a989627 Initial commit
  • 命令 **git log --pretty=oneline** 用於快速或簡短地概述大量提交,而無需遍歷每個提交的幾行資料。

  • 它將以簡寫的提交雜湊和提交訊息顯示每一提交。

$ git log --pretty=oneline
$ git log --pretty=oneline
45ef43e5c3664f9ff725ad7b41a80af6ab515 (HEAD -> main, origin/main, origin/HEAD) changed top and right dimensions
94744071e847ab78390eb1b8a5e013dbeee7ced2 modified the margin and font size
1bc52022c8fe3ee799ccf36bdc3e6ec0c99bb9f5 changed the width dimension
a98962782a8bc6ac91b793b634eb5786b3fb85fc Initial commit

下表顯示了 git log 的常見格式化選項。

選項 輸出描述
-p 顯示每次提交引入的補丁。
--stat 顯示每次提交中修改的檔案的統計資訊。
--shortstat 僅顯示 **--stat** 命令中的更改/插入/刪除行。
--name-only 顯示提交資訊後修改的檔案列表。
--name-status 顯示受影響的檔案列表以及新增/修改/刪除資訊。
--abbrev-commit 顯示 SHA-1 校驗和的前幾個字元,而不是全部 40 個字元。
--relative-date 以相對格式顯示日期(例如,“3 周前”),而不是使用完整日期格式。
--graph 在日誌輸出旁邊顯示分支和合並歷史的 ASCII 圖表。
--pretty 以替代格式顯示提交。選項值包括 oneline、short、full、fuller 和 format(您在其中指定自己的格式)。
--oneline 一起使用的 **--pretty=oneline --abbrev-commit** 的簡寫。

限制日誌輸出

Git log 提供了一些有用的限制選項,允許您僅顯示提交的子集。您可以使用 **-** 來顯示最後 **n** 次提交。

Git 自動將所有輸出定向到分頁器,允許您一次僅檢視一頁日誌輸出。

您可以使用時間限制選項(如 **--since** 和 **--until**)檢視特定時間間隔內的 Git 提交歷史記錄。

以下命令顯示自三週前的提交歷史

$ git log --since=3.weeks
commit 09708ca8bfee4ad20414511b2d847282b17b1c9f (Head -> main, origin/main, origin/HEAD)
Author: Tutorialspoint <tutorialspoint@example.com>
Date:   Thu Apr 18 12:15:17 2024 +0530

   color and heading changed

commit 45ef43e5c3664f9ff725ad7b41a80af6ab515
Author: Tutorialspoint <tutorialspoint@example.com>
Date:   Wed Aug 07 18:56:17 2024 +0530

   changed top and right dimensions

commit 94744071e847ab78390eb1b8a5e013dbeee7ced2
Author: Tutorialspoint <tutorialspoint@example.com>
Date:   Wed Aug 07 17:51:17 2024 +0530

   modified the margin and font size

commit 1bc52022c8fe3ee799ccf36bdc3e6ec0c99bb9f5
Author: Tutorialspoint <tutorialspoint@example.com>
Date:   Wed Aug 07 13:01:47 2024 +0530

以下命令顯示自2022-03-17的提交歷史

$ git log --since=2022-03-
commit 09708ca8bfee4ad20414511b2d847282b17b1c9f (Head -> main, origin/main, origin/HEAD)
Author: Tutorialspoint <tutorialspoint@example.com>
Date:   Thu Apr 18 12:15:17 2024 +0530

   color and heading changed

commit 45ef43e5c3664f9ff725ad7b41a80af6ab515
Author: Tutorialspoint <tutorialspoint@example.com>
Date:   Wed Aug 07 18:56:17 2024 +0530

   changed top and right dimensions

commit 94744071e847ab78390eb1b8a5e013dbeee7ced2
Author: Tutorialspoint <tutorialspoint@example.com>
Date:   Wed Aug 07 17:51:17 2024 +0530

   modified the margin and font size

:...skipping...
commit 09708ca8bfee4ad20414511b2d847282b17b1c9f (Head -> main, origin/main, origin/HEAD)

您可以使用搜索條件檢查提交列表。--author選項用於標識特定作者,而--grep用於搜尋提交訊息中的關鍵詞。

注意:您可以多次使用--author--grep選項,根據不同的條件過濾提交。新增--all-match選項可以將輸出限制為滿足所有給定--grep模式的提交。

-S選項被稱為Git的“pickaxe”(鎬)。它顯示更改給定字串出現次數的提交。

$ git log -S <string>
$ git log -S font-size
commit a98962782a8bc6ac91b793b634eb5786b3fb85fc
Author :tonystark <86562149+tonystark@users.noreply.github.com>
Date : Sat Dec 10 08:35:10 2022 +0000

   Initial commit
  
   Created from https://vercel.com/new

您可以在git log --之後指定目錄或檔名,這將過濾日誌輸出,只顯示更改了這些檔案的提交。

$ git log -- path/to/file
$ git log -- main.html
commit 09708ca8bfee4ad20414511b2d847282b17b1c9f (HEAD -> main, origin/main, origin/HEAD)
Author :Tutorialspoint <tutorialspoint@example.com>
Date : Thu Apr 18 12:15:17 2024 +0530

   color and heading changed

commit 45ef43e5c3664f9ff725ad7b41a80af6ab515
Author : Tutorialspoint <tutorialspoint@example.com>
Date :   Wed Aug 07 18:56:17 2024 +0530

   changed top and right dimensions

commit 94744071e847ab78390eb1b8a5e013dbeee7ced2
Author :Tutorialspoint <tutorialspoint@example.com>
Date : Wed Aug 07 17:51:17 2024 +0530

   modified the margin and font size

commit 1bc52022c8fe3ee799ccf36bdc3e6ec0c99bb9f5
Author :Tutorialspoint <tutorialspoint@example.com>
Date : Wed Aug 07 13:01:47 2024 +0530

下表顯示了限制git log輸出的選項

選項 輸出描述
-<n> 顯示每次提交引入的補丁。
--since, --after 將提交限制為在指定日期之後進行的提交。
--until, --before 將提交限制為在指定日期之前進行的提交。
--author 僅顯示作者條目與指定字串匹配的提交。
--committer 僅顯示提交者條目與指定字串匹配的提交。
--grep 僅顯示提交訊息包含該字串的提交。
-S 僅顯示新增或刪除與該字串匹配的程式碼的提交。

要查詢2024年4月Tutorialspoint提交的更改Git原始碼中測試檔案的提交,請執行以下命令

$ git log --pretty="%h - %s" --author='Tutorialspoint' --since="2024-4-1" --before="2024-4-30"
09708ca - color and heading changed
45ef43e - changed top and right dimensions
9474407 - modified the margin and font size
1bc5202 - changed the width dimension

要查詢提交歷史的總大小,請執行以下命令

$ git log --log-size
commit 09708ca8bfee4ad20414511b2d847282b17b1c9f (HEAD -> main, origin/main, origin/HEAD)
log size: 122
Author :Tutorialspoint <tutorialspoint@example.com>
Date : Thu Apr 18 12:15:17 2024 +0530

   color and heading changed

commit 45ef43e5c3664f9ff725ad7b41a80af6ab515
log size: 130
Author :Tutorialspoint <tutorialspoint@example.com>
Date : Wed Aug 07 18:46:17 2024 +0530

   changed top and right dimensions

commit 94744071e847ab78390eb1b8a5e013dbeee7ced2
log size: 130
Author :Tutorialspoint <tutorialspoint@example.com>
Date : Wed Aug 07 17:51:17 2024 +0530

   modified the margin and font size

:...skipping...

要反向查詢提交歷史,請執行以下命令

$ git log --reverse
commit a98962782a8bc6ac91b793b634eb5786b3fb85fc
Author: tonystark <86562149+tonystark@users.noreply.github.com>
Date: Sat Dec 10 08:35:10 2022 +0000

   Initial commit

   Created from https://vercel.com/

commit 1bc52022c8fe3ee799ccf36bdc3e6ec0c99bb9f5
Author: Tutorialspoint <tutorialspoint@example.com>
Date: Wed Aug 07 13:01:47 2024 +0530

   changed the width dimension

commit 94744071e847ab78390eb1b8a5e013dbeee7ced2
Author: Tutorialspoint 
Date: Wed Aug 07 17:51:17 2024 +0530

:...skipping...
commit a98962782a8bc6ac91b793b634eb5786b3fb85fc
Author :tonystark <86562149+tonystark@users.noreply.github.com>
Date : Sat Dec 10 08:35:10 2022 +0000

   Initial commit

   Created from https://vercel.com/new
廣告