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ā)生了變化鸥跟,無(wú)法查看具體內(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)容拟淮,并不包含新加文件干茉,請(qǐng)注意這些內(nèi)容還沒有添加到本地緩存區(qū)。
將修改內(nèi)容添加到本地緩存區(qū)很泊,通配符可以把當(dāng)前目錄下所有修改的新增的文件都自動(dòng)添加:
$ git add *
再執(zhí)行g(shù)it diff會(huì)發(fā)現(xiàn)沒有任何內(nèi)容輸出角虫,說(shuō)明當(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)間的差異昏兆》懵玻可以使用git help diff詳細(xì)查看其他參數(shù)和功能。