git diff Command
The git diff command compares file differences, that is, it compares the differences between the staging area and the working directory.
The git diff command shows the differences between files written to the staging area and files that have been modified but not yet written to the staging area.
git diff has two main application scenarios.
- Unstaged changes:git diff
- View staged changes: git diff --cached
- View all staged and unstaged changes:git diff HEAD
- Display a summary rather than the entire diff:git diff --stat
Display the differences between the staging area and the working directory:
$ git diff [file]
Display the differences between the staging area and the last commit:
$ git diff --cached [file]
或
$ git diff --staged [file]
Display the differences between two commits:
$ git diff [first-branch]...[second-branch]
Enter the following content in the hello.php file:
Use git status to view the status:
$
git status
-s
A README
AM hello.php
$
git diff
diff
--git
a
/
hello.php b
/
hello.php
index e69de29..69b5711
100644
a
/
hello.php
+++ b
/
hello.php
@@
0
,
0
1
,
3
@@
<
?php
echo'Runoob Tutorial:www.runoob.com'
;
+?
git status shows the changes since your last commit or the changes written to the staging area, while git diff shows line by line what those changes specifically are.
Next let's look at the effect of executing git diff --cached:
$
git add
hello.php
$
git status
-s
A README
A hello.php
$
git diff
--cached
diff
--git
a
/
README b
/
README
new
file
mode
100644
index 0000000..8f87495
/
dev
/
null
+++ b
/
README
@@
0
,
0
1
@@
Runoob Git Test
diff
--git
a
/
hello.php b
/
hello.php
new
file
mode
100644
index 0000000..69b5711
/
dev
/
null
+++ b
/
hello.php
@@
0
,
0
1
,
3
@@
<
?php
echo'Runoob Tutorial:www.runoob.com'
;
+?
Git Basic Operations