git diff
Inspecting 更改使用 git diff — comparing 工作树, staged, and committed 状态.
你将学到
- 如何 see unstaged, staged, and committed 更改
- Comparing 之间分支 and 提交
- 读取 diff 输出格式
- Using --stat for summary 的更改
概念讲解
What git diff Does
git diff shows the differences between various states of your repository. It is the answer to "什么更改?" at every stage of the Git workflow.
git diff
With no arguments, it shows unstaged changes — modifications in your working directory that have not been git 添加-ed.
The Three Diff Views
git diff # unstaged changes (working tree vs staging area)
git diff --staged # staged changes (staging area vs last commit)
git diff HEAD # all changes (working tree vs last commit)
These three commands cover the most common questions:
- "什么有 I 更改但不 staged?" →
git diff - "什么有 I staged 但不 committed?" →
git diff --staged - "什么将是不同如果 I 提交现在?" →
git diff HEAD
Reading the Diff Output
diff --git a/login.js b/login.js
index 1a2b3c4..5d6e7f8 100644
--- a/login.js
+++ b/login.js
@@ -10,5 +10,8 @@ function login(user) {
const session = createSession(user);
- return session;
+ if (session.isValid) {
+ return session;
+ }
+ throw new Error('Invalid session');
}
--- /fileand+++ b/file— the old and new versions@@ -10,5 +10,8 @@— the line numbers (old file lines 10-14, new file lines 10-17)- Lines starting with
-are removed (red) - Lines starting with
+are added (green) - Lines with no prefix are context (unchanged)
Comparing Branches and Commits
git diff main feature-login # what differs between two branches
git diff HEAD~1 HEAD # what changed in the last commit
git diff abc1234 def5678 # what changed between two commits
git diff main..feature-login # same as above (two-dot)
git diff main...feature-login # changes on feature-login since it diverged from main
The three-dot (...) syntax is particularly useful: it shows only the changes made on the feature branch, not changes that happened on main since the branch point.
The --stat Summary
For a quick overview without the full diff:
git diff --stat
login.js | 5 +++--
style.css | 2 ++
README.md | 8 ++++++++
3 files changed, 13 insertions(+), 2 deletions(-)
This shows which files changed and how many lines were added or removed — perfect for a quick review.
Word-Level Diffs
For long lines with small changes, use word-level diffing:
git diff --word-diff
git diff --color-words
This highlights changed words within a line instead of showing the entire old and new line.
示例代码
# See unstaged changes (what you changed but haven't staged)
echo "new line" >> README.md
git diff
# Stage the file and see staged changes
git add README.md
git diff # now shows nothing (no unstaged changes)
git diff --staged # shows the staged changes
git diff --cached # same as --staged (older name)
# See all changes (staged + unstaged) vs last commit
git diff HEAD
# Compare two branches
git diff main feature-login
# See what changed in the last commit
git diff HEAD~1 HEAD
# Summary of changes (file-level, no content)
git diff --stat
git diff --staged --stat
# Compare a specific file between branches
git diff main feature-login -- src/login.js
# See changes on feature branch since it diverged from main
git diff main...feature-login --stat
# Word-level diff for small changes in long lines
git diff --word-diff
# See what a specific commit changed
git show --stat abc1234
Covers three 必要 diff views (unstaged, staged, HEAD), comparing 分支 and 提交, --stat summary, file-特定 diffs, three-点语法 for 分支 divergence, and word-level diffs. --stat flag 是 quickest 方式到 see 什么 files 更改.
动手试一试
- Code Diff
Compare two versions 的 code visually — 相同概念 as git diff.
- File Hash Checksum
Git uses 内容 hashes 到 detect 更改 — see 如何 hashing 工作.
常见错误
错误写法
运行 git diff and seeing 无之后你 staged 你的更改
正确做法
git diff shows 仅 unstaged 更改. 之后 git 添加, 你的更改是 staged and git diff shows 无. Use git diff --staged (or git diff --cached) 到 see staged 更改, or git diff HEAD 到 see 所有更改对比最后提交.
错误写法
Confusing two-点 and three-点 diff 语法
正确做法
git diff ..B shows difference 之间提示的 and B (所有更改 on 两者都边). git diff ...B shows 仅更改 on B 由于它 diverged 从 . three-点是 通常什么你 想要当 reviewing feature 分支.
相关资源
相关代码片段
- Git Log Snippet
git log and git diff 工作 together 到 explore history.
相关速查表
- Git Cheatsheet
diff flags and comparison commands.
练习 git diff
3 个练习
查阅不认识的术语
新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。
动手做项目
通过引导式项目应用所学,含起始代码与解决方案。
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。