diff Git Command Guide
The git diff command is used to show changes between the working tree and the index or a tree, changes between the index and a tree, changes between two trees, changes resulting from a merge, changes between two blob objects, or changes between two files on disk.
git diff Syntax:
Section titled “git diff Syntax:”git diff [<options>] [<commit>] [--] [<path>...]Options:
Section titled “Options:”| Option | Description |
|---|---|
| —cached, —staged | Compare staged changes with commit |
| —merge-base | Compare using merge-base of given commits |
| -p, —patch | Generate patch output (default) |
| -s, —no-patch | Suppress diff output |
| —stat | Show diff statistics |
| —raw | Generate raw diff format |
| -U | Generate diffs with context |
| —color | Show colored diff |
| —exit-code | Exit with status based on changes |
| —name-only | Show only changed file names |
| —name-status | Show file names and status |
| —no-index | Compare files outside working tree |
| —ignore-cr-at-eol | Ignore carriage return at EOL |
Parameters:
Section titled “Parameters:”| Parameter | Description |
|---|---|
| [ | Commit/tree to compare with (default: HEAD) |
| [ | Optional paths to limit comparison |
git diff Command Samples:
Section titled “git diff Command Samples:”View unstaged changes
Section titled “View unstaged changes”git diffShows changes in the working directory that aren’t staged for commit.
View staged changes
Section titled “View staged changes”git diff --stagedShows changes that are staged for the next commit.
Compare working tree with HEAD
Section titled “Compare working tree with HEAD”git diff HEADShows all changes in the working tree compared to the last commit.
Compare two commits
Section titled “Compare two commits”git diff HEAD~1 HEADShows changes between HEAD~1 and HEAD commits.
Compare with merge-base
Section titled “Compare with merge-base”git diff --merge-base mainShows changes relative to the merge-base between current branch and main.
Compare two files on disk
Section titled “Compare two files on disk”git diff --no-index file1.txt file2.txtCompare two files not in the working tree.
How do I see what I’ve changed but not staged?
Section titled “How do I see what I’ve changed but not staged?”To see unstaged changes, run:
git diffHow can I see what is staged for the next commit?
Section titled “How can I see what is staged for the next commit?”To see staged changes, use:
git diff --stagedHow do I compare my working tree with a specific commit?
Section titled “How do I compare my working tree with a specific commit?”To compare working tree with a commit, execute:
git diff <commit>How can I see only the names of changed files?
Section titled “How can I see only the names of changed files?”To show only changed file names, use:
git diff --name-onlyHow do I compare files outside the working tree?
Section titled “How do I compare files outside the working tree?”To compare files not managed by Git, use:
git diff --no-index <file1> <file2>How can I get a summary of changes without showing the diffs?
Section titled “How can I get a summary of changes without showing the diffs?”To get a summary, use:
git diff --statApplications of the git diff command
Section titled “Applications of the git diff command”- Reviewing changes before committing
- Analyzing differences between any two commits
- Debugging code changes and regressions
- Creating patches for code review
- Comparing files outside Git repositories