git diff

關於如何使用git diff 理解的差異

參考博文 https://www.git-tower.com/learn/git/ebook/cn/command-line/advanced-topics/diffsjava

 

git diff 相關命令

查看已暫存和未暫存的修改

若是 git status 命令的輸出對於你來講過於模糊,你想知道具體修改了什麼地方,能夠用 git diff命令。 稍後咱們會詳細介紹 git diff,你可能一般會用它來回答這兩個問題:當前作的哪些更新尚未暫存? 有哪些更新已經暫存起來準備好了下次提交? 儘管 git status 已經經過在相應欄下列出文件名的方式回答了這個問題,git diff 將經過文件補丁的格式顯示具體哪些行發生了改變。git

假如再次修改 README 文件後暫存,而後編輯 CONTRIBUTING.md 文件後先不暫存, 運行 status 命令將會看到:github

 git status










$On branch masterChanges to be committed:(use "git reset HEAD <file>..." to unstage)modified: READMEChanges not staged for commit:(use "git add <file>..." to update what will be committed)(use "git checkout -- <file>..." to discard changes in working directory)modified: CONTRIBUTING.md

要查看還沒有暫存的文件更新了哪些部分,不加參數直接輸入 git diffless

 git diff













$diff --git a/CONTRIBUTING.md b/CONTRIBUTING.mdindex 8ebb991..643e24f 100644--- a/CONTRIBUTING.md+++ b/CONTRIBUTING.md@@ -65,7 +65,8 @@ branch directly, things can get messy.Please include a nice description of your changes when you submit your PR;if we have to read the whole diff to figure out why you're contributingin the first place, you're less likely to get feedback and have your change-merged in.+merged in. Also, split your changes into comprehensive chunks if your patch is+longer than a dozen lines.If you are starting to work on a particular area, feel free to submit a PRthat highlights your work in progress (and note in the PR title that it's

此命令比較的是工做目錄中當前文件和暫存區域快照之間的差別, 也就是修改以後尚未暫存起來的變化內容。spa

若要查看已暫存的將要添加到下次提交裏的內容,能夠用 git diff --cached 命令。(Git 1.6.1 及更高版本還容許使用 git diff --staged,效果是相同的,但更好記些。)code

 git diff --staged






$diff --git a/README b/READMEnew file mode 100644index 0000000..03902a1--- /dev/null+++ b/README@@ -0,0 +1 @@+My Project

請注意,git diff 自己只顯示還沒有暫存的改動,而不是自上次提交以來所作的全部改動。 因此有時候你一會兒暫存了全部更新過的文件後,運行 git diff 後卻什麼也沒有,就是這個緣由。ip

像以前說的,暫存 CONTRIBUTING.md 後再編輯,運行 git status 會看到暫存先後的兩個版本。 若是咱們的環境(終端輸出)看起來以下:get

 git add CONTRIBUTING.md
    CONTRIBUTING.md
 git status










$$echo'# test line'>>$On branch masterChanges to be committed:(use "git reset HEAD <file>..." to unstage)modified: CONTRIBUTING.mdChanges not staged for commit:(use "git add <file>..." to update what will be committed)(use "git checkout -- <file>..." to discard changes in working directory)modified: CONTRIBUTING.md

如今運行 git diff 看暫存先後的變化:it

$ git diff







diff --git a/CONTRIBUTING.md b/CONTRIBUTING.mdindex 643e24f..87f08c8 100644--- a/CONTRIBUTING.md+++ b/CONTRIBUTING.md@@ -119,3 +119,4 @@ at the## Starter ProjectsSee our [projects list](https://github.com/libgit2/libgit2/blob/development/PROJECTS.md).+# test line

而後用 git diff --cached 查看已經暫存起來的變化:(--staged 和 --cached 是同義詞)io

$ git diff --cached












diff --git a/CONTRIBUTING.md b/CONTRIBUTING.mdindex 8ebb991..643e24f 100644--- a/CONTRIBUTING.md+++ b/CONTRIBUTING.md@@ -65,7 +65,8 @@ branch directly, things can get messy.Please include a nice description of your changes when you submit your PR;if we have to read the whole diff to figure out why you're contributingin the first place, you're less likely to get feedback and have your change-merged in.+merged in. Also, split your changes into comprehensive chunks if your patch is+longer than a dozen lines.If you are starting to work on a particular area, feel free to submit a PRthat highlights your work in progress (and note in the PR title that it's

git diff 能夠查看修改的內容和最近版本庫裏面的區別

git diff filename

也能夠查看兩個版本庫裏面文件的差異,顯示狀態

git diff --name-status head origin

 查看兩個版本庫裏面某一個文件的差別

注意最好先cd 到相應的文件夾下面不然可能由於路徑問題,找不到文件。

git diff head origin src/com/hp/auth/LogAgent.java

查看兩個版本庫裏面修改的文件,能夠完整顯示文件的路徑。

$ git diff --name-only head origin  

相關文章
相關標籤/搜索