← 返回命令列表

Linux command

git-diff 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Show unstaged changes

git diff

Show staged changes

git diff --staged

Diff between branches

git diff [branch1]..[branch2]

Diff specific file

git diff [file.txt]

Show word-level diff

git diff --word-diff

Show stat summary

git diff --stat

Diff between commits

git diff [commit1] [commit2]

说明

git diff shows differences between commits, branches, working directory, and staging area. It is essential for reviewing changes before committing and understanding what changed between versions. By default, it shows unstaged changes (working directory vs index). With --staged, it shows what will be committed. When given two commits or branches, it shows all changes between them.

参数

--staged, --cached
Compare staged changes to last commit.
--stat
Show diffstat summary.
--word-diff
Show word-level changes.
--color-words
Colored word diff.
--name-only
Show only changed file names.
--name-status
Show names and change status.
-U _n_, --unified _n_
Lines of context.
--no-index
Compare files outside repository.
--diff-filter _filter_
Filter by change type (A, D, M, R).
-b, --ignore-space-change
Ignore whitespace changes.
-w, --ignore-all-space
Ignore all whitespace.

FAQ

What is the git-diff command used for?

git diff shows differences between commits, branches, working directory, and staging area. It is essential for reviewing changes before committing and understanding what changed between versions. By default, it shows unstaged changes (working directory vs index). With --staged, it shows what will be committed. When given two commits or branches, it shows all changes between them.

How do I run a basic git-diff example?

Run `git diff` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does --staged, --cached do in git-diff?

Compare staged changes to last commit.