git diff Command
The git diff
command compares differences between files, specifically the differences between the staging area and the working directory.
The git diff
command shows the differences between files that have been staged and files that have been modified but not yet staged.
There are two main applications of git diff
.
Unstaged changes: git diff
View staged changes: git diff --cached
View all staged and unstaged changes: git diff HEAD
Display summary instead of the entire diff: git diff --stat
To show the differences between the staging area and the working directory:
$ git diff [file]
To show the differences between the staging area and the last commit:
$ git diff --cached [file]
or
$ git diff --staged [file]
To show the differences between two commits:
$ git diff [first-branch]...[second-branch]
Enter the following content in the hello.php
file:
<?php
echo 'tutorialpro.org: www.tutorialpro.org';
?>
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 'tutorialpro.org: www.tutorialpro.org';
+?>
git status
shows the changes since the last update or the changes that have been staged, while git diff
displays these changes line by line.
Next, let's see the effect of 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 @@
+# tutorialpro 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 'tutorialpro.org: www.tutorialpro.org';
+?>