LinuxCommandLibrary

git-diff

Show differences between commits, working tree, etc

TLDR

Show unstaged changes

$ git diff
copy

Show all uncommitted changes (including staged ones)
$ git diff HEAD
copy

Show only staged (added, but not yet committed) changes
$ git diff --staged
copy

Show changes from all commits since a given date/time (a date expression, e.g. "1 week 2 days" or an ISO date)
$ git diff 'HEAD@[{3 months|weeks|days|hours|seconds ago]}'
copy

Show diff statistics, like files changed, histogram, and total line insertions/deletions
$ git diff --stat [commit]
copy

Output a summary of file creations, renames and mode changes since a given commit
$ git diff --summary [commit]
copy

Compare a single file between two branches or commits
$ git diff [branch_1]..[branch_2] [path/to/file]
copy

Compare different files from the current branch to another branch
$ git diff [other_branch]:[path/to/file2] [path/to/file1]
copy

SYNOPSIS

git diff [options] [tree-ish [tree-ish]] [--] [path...]
git diff [options] --cached [tree-ish] [--] [path...]
git diff [options] --staged [tree-ish] [--] [path...]
git diff [options] --no-index [--] path path

PARAMETERS

--patch or -p
    Generate a patch (diff output). This is the default behaviour.

--stat
    Generate a diffstat, showing a summary of changes (files changed, lines added/deleted).

--name-only
    Show only the names of affected files.

--name-status
    Show names and status (A, C, D, M, R, T, U, X, B) of affected files.

--check
    Warn if the changes introduce whitespace errors.

--color / --no-color
    Turn on or off colored diff output.

--word-diff
    Show word-by-word differences instead of line-by-line.

--unified= or -U
    Generate diffs with n lines of context around the changes.

--binary
    Output a binary diff that can be applied with git apply.

--cached or --staged
    Compare the index (staging area) with the specified tree-ish (defaulting to HEAD).

--no-index
    Compare two arbitrary files or directories outside of a Git repository.

...
    Limit the diff to changes within the specified file(s) or directory/directories.


    A commit hash, branch name, tag name, or other Git object reference to compare against.

DESCRIPTION

The git-diff command is a fundamental Git utility used to display differences between various parts of your Git repository. It can compare the working directory with the staging area (index), the working directory with the last committed state (HEAD), the staging area with HEAD, or even differences between any two commits, branches, or tags.

It's an indispensable tool for reviewing changes before committing, understanding modifications made by others, debugging inconsistencies, or simply exploring the evolution of a codebase. By default, git-diff shows line-by-line textual differences (hunks) in a unified format, highlighting additions and deletions. It supports numerous options to customize the output, allowing users to view concise summaries, file names only, or detailed word-by-word comparisons.

CAVEATS

git-diff typically doesn't show line-by-line differences for binary files; it merely indicates that they differ. Detailed binary diffs often require external tools or Git attribute configurations. For very large files or extensive changes across many files, the output can be substantial and resource-intensive. It also primarily focuses on tracked files; untracked files are ignored unless specifically referenced with --no-index.

COMMON COMPARISON SCENARIOS

git-diff is highly versatile. By default, with no arguments, it compares your working directory with the staging area (index). Adding a single tree-ish (e.g., git diff HEAD) compares the working directory with the last commit. Using --cached (or --staged) compares the staging area with the last commit. Comparing two arbitrary commits is done with git diff commit1 commit2. The commit1...commit2 syntax shows changes on commit2 since the common ancestor with commit1.

OUTPUT TYPES

Beyond the default patch output, git-diff offers specialized views. --stat provides a concise summary of affected files and line changes. --name-only lists only the filenames. --name-status adds the file status (e.g., modified, added, deleted). For detailed word-level changes, --word-diff is invaluable, especially for text documents.

HISTORY

As a core command, git-diff has been an integral part of Git since its inception in 2005. It leverages the underlying diffing algorithms but applies them to Git's object model (blobs and trees). Its functionality has steadily evolved, adding more powerful options for output formatting, context control, and diverse comparison scenarios, adapting to the growing needs of version control users.

SEE ALSO

git status(1), git add(1), git commit(1), git log(1), git show(1), diff(1)

Copied to clipboard