Git Diff: Reference and Examples

Git Diff: Reference and Examples

Last updated:
Table of Contents

Git version used: 2.x

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

Only files that have been staged (git added) 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 working directory and a commit

$ git diff <commit-hash> -- path/to/filefor a single file and$ git diff -- .` for everything in the local directory

Example

$ git diff 7b22810 -- src/path/to/file.py
diff --git a/src/path/to/file.py b/src/path/to/file.py
index f283c8a3..a7aabeb1 100644
--- a/src/path/to/file.py
+++ b/src/path/to/file.py
@@ -1,14 +1,12 @@
+import pandas as pd
 import seaborn as sns
-from foo.bar.aggregations import WeightedMean

Diff between current branch and another branch

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 working directory and another branch

Use $ git diff my-other-branch -- ..

For a specific file, do $ git diff my-other-branch -- path/to/my-file instead.

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

Diff between working directory and remote branch

Only files that have been staged (git added) will show up.

Use git diff <remote_name>/<branch_name> -- .

Example: diff between the working directory and the current state of my-branch in remote origin. File file3.txt has been deleted in the current working directory, but it exists on the remote branch:

$ git fetch origin
$ git diff origin/my-branch -- .
diff --git a/file3.txt b/file3.txt
deleted file mode 100644
index 8958c52..0000000
--- a/file3.txt
+++ /dev/null
@@ -1,6 +0,0 @@
-foo
-
-bar

Diff between working directory and HEAD, including unstaged

Based on an approach suggested by radzimir on stackoverflow

By default, git diff only includes staged files in the output. This means that unstaged files (files you didn't git add) are completely ignored.

The following bash function displays the diff between the current working directory (including unstaged files) and HEAD

  • Function definition: (bash)

    full-diff() {
        echo "---------------------------"
        echo "-- Diff Unstaged <> HEAD --"
        echo "---------------------------"
    
        for next in $( git ls-files --others --exclude-standard ); do 
            git --no-pager diff --no-index /dev/null "$next"
        done
    
        echo "---------------------------"
        echo "--- Diff Staged <> HEAD ---"
        echo "---------------------------"
    
        git --no-pager diff HEAD
    }
    
  • Usage example:

    Unstaged changed are shown first, staged changes after

    $ full-diff 
    ---------------------------
    -- Diff Unstaged <> HEAD --
    ---------------------------
    diff --git a/new-file.txt b/new-file.txt
    new file mode 100644
    index 0000000..2df9f72
    --- /dev/null
    +++ b/new-file.txt
    @@ -0,0 +1 @@
    +xxxxx
    ---------------------------
    --- Diff Staged <> HEAD ---
    ---------------------------
    diff --git a/added-file.txt b/added-file.txt
    index d5f7fc3..6c00ac5 100644
    --- a/added-file.txt
    +++ b/added-file.txt
    @@ -1 +1 @@
    -added
    +assadded
    

File names only

Use option --name-status:

$ git diff --name-status other-branch
M       path/to/some-modified-file
M       path/to/another-modified-file
A       somed-added-file
D       some-deleted-file