
- Git - Home
- Git - Version Control
- Git - Basic Concepts
- Git - Command Line
- Git - Installation
- Git - First Time Setup
- Git - Basic Commands
- Git - Getting Help
- Git - Tools
- Git - Cheat Sheet
- Git - Terminology
- Git - Life Cycle
- Git - Get a Repository
- Git - Adding New Files
- Git - Recording Changes
- Git - Viewing Commit History
- Git Branching
- Git - Branches in a Nutshell
- Git - Creating a New Branch
- Git - Switching Branches
- Git - Branching and Merging
- Git - Merge Conflicts
- Git - Managing Branches
- Git - Branching Workflows
- Git - Remote Branches
- Git - Tracking Branches
- Git - Rebasing
- Git - Rebase vs. Merge
- Git - Squash Commits
- Git Operations
- Git - Clone Operation
- Git - Tagging Operation
- Git - Aliases Operation
- Git - Commit Operation
- Git - Stash Operation
- Git - Move Operation
- Git - Rename Operation
- Git - Push Operation
- Git - Pull Operation
- Git - Fork Operation
- Git - Patch Operation
- Git - Diff Operation
- Git - Status Operation
- Git - Log Operation
- Git - Head Operation
- Git - Origin Master
- Git Undoing
- Git - Undoing Changes
- Git - Checkout
- Git - Revert
- Git - Reset
- Git - Restore Operation
- Git - Rm
- Git - Switch Operation
- Git - Cherry-pick
- Git - Amend
- Git on the Server
- Git - Local Protocol
- Git - Smart HTTP Protocol
- Git - Dumb HTTP Protocol
- Git - The SSH Protocol
- Git - The Git Protocol
- Git - Getting Git on a Server
- Git - Setting up the Server
- Git - Daemon
- Git - GitWeb
- Git - GitLab
- Git - Third Party Hosted Options
- Distributed Git
- Git - Distributed Workflows
- Git - Contributing to a Project
- Git - Maintaining a Project
- Customizing Git
- Git - Configuration
- Git - Hooks
- Git - Attributes
- Git - Init
- Git - Commit
Git Diff
The git diff command in Git offers detailed analysis of changes across various repository states and objects, including working trees, indexes, trees, merge results, blob objects, and disk files.
It provides comprehensive insights into differences, making it essential for understanding and managing version control effectively.
The command git diff is a flexible Git command that:
Shows changes made to the index and working tree.
Displays the differences between an index and a specific tree (like HEAD).
Evaluates the differences between two different trees.
Displays the differences between the results of a merge.
Compares individual files or blob objects on disk.
It offers thorough insights into variations among various Git states and objects, which makes it crucial for understanding changes and efficiently handling version control.
git diff [<options>] [--] [<path>]
The command git diff [<options>] [--] [<path>...] lets us see changes in relation to the staging area, or index, which displays changes that haven't been staged for the upcoming commit yet.
The differences show changes that git add can be used to add to the index.
git diff [<options>] --no-index [--] <path> <path>
It avoids using Git's index and compares two provided paths directly on the filesystem.
Unless we are comparing routes within a working tree that is under Git management and at least one of the paths is outside of the working tree.
Or unless Git is not managing the working tree, we must use the --no-index option.
The --exit-code option, which indicates whether there are differences between the paths, is implied by this version of git diff.
git diff [<options>] --cached [--merge-base] [<commit>] [--] [<path>]
It evaluates the changes staged for the upcoming commit in comparison to a given <commit>.
In the absence of <commit>, HEAD is used by default.
All staged modifications are displayed if <commit> is not supplied and HEAD does not exist (e.g., unborn branches).
Instead of using <commit>, the --merge-base option utilizes the merge base of <commit> and HEAD.
--staged and --cached are interchangeable.
git diff [<options>] [--merge-base] <commit> [--] [<path>]
It evaluates our working tree's modifications in relation to the specified <commit>.
To compare the tip of one branch with the latest commit, use HEAD.
To compare the tip of another branch, use the name of the branch.
To find the comparison basis, --merge-base calculates the merge base between <commit> and HEAD.
For example, git diff $(git merge-base A HEAD) is equal to git diff --merge-base A.
git diff [<options>] [--merge-base] <commit> <commit> [--] [<path>]
It compares the changes of two given <commit> objects.
In order to determine the before side of the comparison, the merge base of the two commits is calculated if --merge-base is specified.
For example, git diff $(git merge-base A B) B is same as git diff --merge-base A B.
git diff [<options>] <commit> <commit> <commit> [--] [<path>]
It is used to view the changes resulting from a merge commit.
The merging itself must be the first listed <commit>, and the commits that follow are its parents.
Suffixes like ^@ and ^! make it easy to define the desired revisions.
For example, the combined diff for a merge commit A is produced by git diff A A^@, git diff A^!, and git show A.
git diff [<commit>] <commit>..<commit> [--] [<path>]
Like the form without the .., lets us see the differences between two random <commit> objects.
By default, HEAD is used for comparison when <commit> is missing from one side.
This command is helpful for comparing a commit to the current working state or for analyzing differences between certain commits.
git diff [<options>] <blob> <blob>
The Git command git diff [<options>] <blob> <blob> compares and shows the variations between the raw contents of two blob objects.
We can compare the contents of files or objects in the repository directly with this command.
Options
The git diff command has the following options:
Comparing Changes
Following is a list of commands that can be used for comparing changes:
git diff − displays unstaged changes.
git diff --cached or git diff --staged − displays staged changes.
git diff <commit1><commit2> − displays changes between two different commits.
git diff <branch1><branch2> − displays changes between two different branches.
Based on Outout Formats
Following is a list of options that are used based on output formats:
--stat − displays a summary of changes, i.e., the number of added and deleted lines, for each file.
-
--name-only − displays only the name of the files that were changed.
--name-status − displays both the name and status of the files that were changed, either added, deleted, or modified.
--color − displays colored outout, aides in readability.
Based on Limiting Output
Following are the options that are used based on limiting the output:
-U<n> − displays a unified diff with <n> lines of context, such as -U5 displays five lines of context.
--word-diff − displays the changes at the word level and highlight them.
Based on Ignoring Changes
Following are the options that are used based on ignoring changes:
--igonore-space-at-eol − ignores changes in whitespaces at the end of line.
--ignore-space-change − ignores changes in amount of whitespaces.
-ignore-all-space − ignores all the whitespaces.
Working with Patches
Following is the option that is based on working with patches:
-p or --patch − displays the difference in patch format
These options tell Git to automatically produce a patch.
Combined Options
Two or more options can be used in combination, such as, --stat and --cached.
-s
--no-patch
In Git, the -s or --no-patch option suppresses the diff machinery's output completely.
It comes in handy when we wish to conceal patch outputs from commands like git show, which by default reveal patches.
Additionally, this option can reverse the effects of options that were previously supplied in the command line or within an alias, such as --patch or --stat.
--output=<file>
Git's --output=<file> option allows us to direct a command's output to a specified file rather than the standard output (stdout).
This is helpful for immediately storing command outputs or results to a file for processing or future reference.
--output-indicator-new=<char>
--output-indicator-old=<char>
--output-indicator-context=<char>
Git allows us to customize which characters are used to signify new, old, or context lines in created patches using the --output-indicator-new=<char>, --output-indicator-old=<char>, and --output-indicator-context=<char> options.
These characters are by default + for new lines, - for existing lines, and ' ' (space for context lines).
The way diffs are graphically shown in patches can be customized with these parameters.
--raw
Git produces a machine-readable representation of file changes when the --raw option is used to generate the diff output.
For scripting and automated processing, this format is helpful.
--patch-with-raw
In Git, -p --raw is equivalent to the --patch-with-raw option.
It produces a patch with output in raw format.
--indent-heuristic
Git has an option called --indent-heuristic that activates a heuristic that modifies diff hunk boundaries to improve patch readability.
By enhancing the visual clarity of changes within diffs, this heuristic which is enabled by default aims to make them simpler to understand and review.
--no-indent-heuristic
The Git option --no-indent-heuristic turns off the heuristic that modifies diff hunk boundaries to make patches easier to read.
--minimal
The --minimal option in Git ensures that the smallest possible diff is generated by spending extra time to optimize the output.
--patience
The --patience option in Git uses the patience diff algorithm to generate a diff.
All the above mentioned options are helpful in case you are debugging, reviewing changes, or preparing for a commit.