Git 基礎(chǔ)應(yīng)用(二)git diff命令

git diff 提交內(nèi)容比較

https://git.oschina.net/minasia/GitTest.git 克隆一個(gè)項(xiàng)目或者自己手動(dòng)創(chuàng)建一個(gè)git項(xiàng)目(上篇文章已講解)

git clone https://git.oschina.net/minasia/GitTest.git
cd GitTest

進(jìn)入到項(xiàng)目中,添加一個(gè)文件,編輯一個(gè)文件

$ echo 'abc' >> file1
$ echo 'new file ' >> newFile

查看當(dāng)前git狀態(tài)

$ git status
    On branch master
    Your branch is up-to-date with 'origin/master'.
    Changes 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:   file1
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
        newFile
    
    no changes added to commit (use "git add" and/or "git commit -a")

但是git status 命令只能查看哪些文件發(fā)生了變化,無法查看具體內(nèi)容的變化。如何查看修改的文件內(nèi)容呢,那就需要使用git diff命令。git diff命令的作用是比較修改的或提交的文件內(nèi)容。

diff --git a/file1 b/file1
index e4b01c6..608842c 100644
--- a/file1
+++ b/file1
@@ -3,3 +3,4 @@ file1
 
 aaa
 aa
+abc
~
~
~
~
~
~
~
(END)

上面的命令執(zhí)行后需要使用q退出。命令輸出當(dāng)前工作目錄中修改的內(nèi)容,并不包含新加文件,請注意這些內(nèi)容還沒有添加到本地緩存區(qū)。

將修改內(nèi)容添加到本地緩存區(qū),通配符可以把當(dāng)前目錄下所有修改的新增的文件都自動(dòng)添加:

$ git add *

再執(zhí)行g(shù)it diff會(huì)發(fā)現(xiàn)沒有任何內(nèi)容輸出,說明當(dāng)前目錄的修改都被添加到了緩存區(qū),如何查看緩存區(qū)內(nèi)與上次提交之間的差別呢?需要使用--cached參數(shù):

$ git diff --cached

diff --git a/file1 b/file1
index e4b01c6..608842c 100644
--- a/file1
+++ b/file1
@@ -3,3 +3,4 @@ file1
 
 aaa
 aa
+abc
diff --git a/newFile b/newFile
new file mode 100644
index 0000000..fa49b07
--- /dev/null
+++ b/newFile
@@ -0,0 +1 @@
+new file

最后提交代碼

$ git commit -m '提交代碼' 

提交后git diff與git diff --cached都不會(huì)有任何輸出了。

git diff 分支比較
# 創(chuàng)建分支
$ git branch newBranch
# 切換分支
$ git checkout newBranch
# 修改文件
$ echo 'aaaaa' >> file1
$ echo 'new new file' >> NewFile1
# 添加到緩沖區(qū)
$ git add *
# 提交
$ git commit -m '提交'

查看master分支與newBranch分支之間的差別

$ git diff master

diff --git a/NewFile1 b/NewFile1
new file mode 100644
index 0000000..e2fbd00
--- /dev/null
+++ b/NewFile1
@@ -0,0 +1 @@
+new new file
diff --git a/file1 b/file1
index e4b01c6..f2ece01 100644
--- a/file1
+++ b/file1
@@ -3,3 +3,5 @@ file1
 
 aaa
 aa
+abc
+aaaaa
diff --git a/file2 b/file2
index 3213394..bc65a8d 100644
--- a/file2
+++ b/file2
@@ -1,2 +1,3 @@
 file2
 edit file2

git diff 是一個(gè)難以置信的有用的工具,可以找出你項(xiàng)目上任意兩個(gè)提交點(diǎn)間的差異??梢允褂胓it help diff詳細(xì)查看其他參數(shù)和功能。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容