git 查詢文件的改動

在開發過程中,經常需要查看文件的改動,而git是一個強大的版本控制工具,提供了多種方式來幫助我們查詢文件的改動。

一、查看某個文件的版本歷史

使用Git命令行,可以通過以下命令來查看某個文件的版本歷史:

$ git log 文件路徑

例如,我們要查看文件index.html的版本歷史,可以輸入以下命令:

$ git log index.html

這樣會顯示出所有與該文件相關的提交記錄,顯示結果類似于以下信息:

commit a8e15de3d1d741ff7d6b8ca65107eac875f72dbf (HEAD -&gt; master) Author: John Doe <johndoe> Date:   Fri Jun 18 14:06:11 2021 +0800      Update index.html  commit 42b8df272a7f0f113a3dabb376e9b6b113cba302 Author: John Doe <johndoe> Date:   Thu Jun 17 16:47:53 2021 +0800      Add index.html</johndoe></johndoe>

其中每個提交記錄都對應著一個版本,包含了提交的作者、時間和提交說明等信息。

二、查看某個文件的具體改動

有時候,我們只需要查看某個文件的具體改動內容,可以使用以下命令:

$ git log -p 文件路徑

例如,我們要查看文件index.html的具體改動,可以輸入以下命令:

$ git log -p index.html

這樣會顯示出每個提交記錄對該文件的具體改動內容,顯示結果類似于以下信息:

commit a8e15de3d1d741ff7d6b8ca65107eac875f72dbf (HEAD -&gt; master) Author: John Doe <johndoe> Date:   Fri Jun 18 14:06:11 2021 +0800      Update index.html  diff --git a/index.html b/index.html index 7f3e5c2..181575f 100644 --- a/index.html +++ b/index.html @@ -1,4 +1,5 @@       -    <title>Hello World</title> +    <title>Welcome to My Site</title> <h1>Hello World</h1>      <p>This is a sample website.</p>      <p>It is still under construction.</p>      commit 42b8df272a7f0f113a3dabb376e9b6b113cba302 Author: John Doe <johndoe> Date:   Thu Jun 17 16:47:53 2021 +0800      Add index.html  diff --git a/index.html b/index.html new file mode 100644 index 0000000..7f3e5c2 --- /dev/null +++ b/index.html @@ -0,0 +1,4 @@ + + + +    <title>Hello World</title> + + +    <h1>Hello World</h1> +    <p>This is a sample website.</p> +    <p>It is still under construction.</p> + +</johndoe></johndoe>

其中,“@@”之后的內容表示改動的具體位置和內容。

三、查看某個文件的修改者

如果想要查看某個文件的修改者,可以使用以下命令:

$ git blame 文件路徑

例如,我們要查看文件index.html的修改者,可以輸入以下命令:

$ git blame index.html

這樣會顯示出每行代碼的修改者和修改時間等信息,顯示結果類似于以下信息:

42b8df27 (John Doe 2021-06-17 16:47:53 +0800 1)  42b8df27 (John Doe 2021-06-17 16:47:53 +0800 2)  42b8df27 (John Doe 2021-06-17 16:47:53 +0800 3)  42b8df27 (John Doe 2021-06-17 16:47:53 +0800 4)     <title>Hello World</title> 42b8df27 (John Doe 2021-06-17 16:47:53 +0800 5)  42b8df27 (John Doe 2021-06-17 16:47:53 +0800 6)  42b8df27 (John Doe 2021-06-17 16:47:53 +0800 7)     <h1>Hello World</h1> ... a8e15de3 (John Doe 2021-06-18 14:06:11 +0800  23)     <title>Welcome to My Site</title> a8e15de3 (John Doe 2021-06-18 14:06:11 +0800  24)  a8e15de3 (John Doe 2021-06-18 14:06:11 +0800  25)  a8e15de3 (John Doe 2021-06-18 14:06:11 +0800  26)     <h1>Hello World</h1> ...

其中,每行代碼前面的一串字符是該行代碼所在的提交記錄的哈希值,后面的信息是修改者、時間等。通過這個命令,我們可以清晰地了解每行代碼的修改記錄以及修改者。

總結:

? 版權聲明
THE END
喜歡就支持一下吧
點贊12 分享