Git Diff: Reference and Examples

Last updated:
Table of Contents

Git version used: 2.7.4

Diff between two commits

Note that git diff uses /dev/null when the file didn't exist in a given commit

I.e. show diff for all files across two commits.

Use $ git diff <commit_a> <commit_b>

Example: file.txt and file2.txt are different in the two commits:

$ git diff f6f207 22d08
diff --git a/file.txt b/file.txt
index 3bd1f0e..257cc56 100644
--- a/file.txt
+++ b/file.txt
@@ -1,2 +1 @@
 foo
-bar
diff --git a/file2.txt b/file2.txt
deleted file mode 100644
index 85553e8..0000000
--- a/file2.txt
+++ /dev/null
@@ -1,2 +0,0 @@
-aaaaaa
-bbbbbb

Diff between file in two commits

I.e. view the diff for a single file across two commits.

Use $ git diff <commit_1> <commit_2> path/to/file

Example: file2.txt didn't exist in commit 22d08

$ git diff f6f207 22d08 file2.txt
diff --git a/file2.txt b/file2.txt
deleted file mode 100644
index 85553e8..0000000
--- a/file2.txt
+++ /dev/null
@@ -1,2 +0,0 @@
-aaaaaa
-bbbbbb

Diff between working directory and last commit

The last commit is called HEAD.

Use $ git diff HEAD

Note that only files that have been staged (i.e. you ran git add on them) will show up.

Example: file.txt has one extra line in the working directory when compared with the last commit.

$ git diff HEAD
diff --git a/file.txt b/file.txt
index 257cc56..3bd1f0e 100644
--- a/file.txt
+++ b/file.txt
@@ -1 +1,2 @@
 foo
+bar

Diff between current branch and master

Use $ git diff master

Example: current branch has modified file.txt and removed file2.txt when compared to the master branch.

$ git diff master
diff --git a/file.txt b/file.txt
index 3bd1f0e..86e041d 100644
--- a/file.txt
+++ b/file.txt
@@ -1,2 +1,3 @@
 foo
 bar
+baz
diff --git a/file2.txt b/file2.txt
deleted file mode 100644
index 85553e8..0000000
--- a/file2.txt
+++ /dev/null
@@ -1,2 +0,0 @@
-aaaaaa
-bbbbbb

Diff between local and remote

I.e., diff between local master and remote master.

Note that only files that have been staged (i.e. you ran git add on them) will show up.

Use $ git diff origin/master

Example:: You have one new file in the local master when compared to the remote master

$ git diff origin/master 
diff --git a/file3.txt b/file3.txt
new file mode 100644
index 0000000..a309e46
--- /dev/null
+++ b/file3.txt
@@ -0,0 +1 @@
+this is file3

Dialogue & Discussion