git diff

Updated: 09 August 2024

Show the changes required to get from branch1 to branch2

git diff branch1..branch2

which is equivalent to

git diff branch1 branch2

Diff of one file path

git diff branch1..branch2 -- myfile.cs

Diff of a directory between branches

git diff master..mybranch path/to/dir

Diff of one file between version in given commit and local HEAD

git diff 2f43f286 index.php

Show the number of lines changed

git diff --stat
git diff --numstat

Diff from a common ancestor

git diff [options] commitA...commitB [--] [path...]

This form is to view the changes on the branch containing and up to commitB, starting at a common ancestor of both commit

git diff A...B

is equivalent to

git diff $(git merge-base A B) B

Use separate tool (e.g. Meld) to view diff

git difftool --dir-diff branch1 branch2

Diff two directories not tracked by Git

git diff --no-index Downloads Downloads-copy

Highlight changed words using only colors

git diff --color-words

Generate diffs with n lines of context instead of the usual three

git diff --unified=0
git diff -U0

Show only names of changed files

git diff --name-only master media-coverage

Diff two arbitrary files, not necessarily under git controld

git diff --no-index path/1/code.php path/2/code.php

diff algorithms

How different are Git algorithms?

In Git, there are four diff algorithms, namely Myers, Minimal, Patience, and Histogram. The Minimal and the Histogram algorithms are the improved versions of the Myers and the Patience respectively.

git diff --minimal foo bar
git diff --patience foo bar
git diff --histogram foo bar
git diff --anchored=<text> foo bar

git diff --diff-algorithm=myers foo bar
git diff --diff-algorithm=minimal foo bar
git diff --diff-algorithm=patience foo bar
git diff --diff-algorithm=histogram foo bar

Leave a comment