mirror of
https://github.com/mintlify/docs.git
synced 2026-09-14 13:35:46 +08:00
323 lines
4.3 KiB
Plaintext
323 lines
4.3 KiB
Plaintext
---
|
|
title: git diff
|
|
description: Show changes between commits, commit and working tree, etc
|
|
---
|
|
|
|
Display differences between various Git objects.
|
|
|
|
## Syntax
|
|
|
|
```bash
|
|
git diff [options] [<commit>] [<commit>] [--] [<path>]
|
|
```
|
|
|
|
## Description
|
|
|
|
`git diff` shows what has changed in your repository. You can compare working directory changes, staged changes, commits, branches, and more.
|
|
|
|
## Common usage
|
|
|
|
Show unstaged changes:
|
|
|
|
```bash
|
|
git diff
|
|
```
|
|
|
|
Show staged changes:
|
|
|
|
```bash
|
|
git diff --staged
|
|
```
|
|
|
|
Compare two commits:
|
|
|
|
```bash
|
|
git diff commit1 commit2
|
|
```
|
|
|
|
## Options
|
|
|
|
<ParamField path="--staged, --cached" type="flag">
|
|
Show changes staged for commit
|
|
|
|
```bash
|
|
git diff --staged
|
|
```
|
|
</ParamField>
|
|
|
|
<ParamField path="--name-only" type="flag">
|
|
Show only names of changed files
|
|
|
|
```bash
|
|
git diff --name-only
|
|
```
|
|
</ParamField>
|
|
|
|
<ParamField path="--name-status" type="flag">
|
|
Show names and change types (Added, Modified, Deleted)
|
|
</ParamField>
|
|
|
|
<ParamField path="--stat" type="flag">
|
|
Show summary statistics
|
|
|
|
```bash
|
|
git diff --stat
|
|
```
|
|
</ParamField>
|
|
|
|
<ParamField path="--color-words" type="flag">
|
|
Show word-level differences
|
|
</ParamField>
|
|
|
|
## Examples
|
|
|
|
### Unstaged changes
|
|
|
|
```bash
|
|
git diff
|
|
```
|
|
|
|
Shows changes in working directory not yet staged.
|
|
|
|
### Staged changes
|
|
|
|
```bash
|
|
git diff --staged
|
|
```
|
|
|
|
Shows changes that will be included in the next commit.
|
|
|
|
### Compare branches
|
|
|
|
```bash
|
|
git diff main feature-branch
|
|
```
|
|
|
|
### Compare commits
|
|
|
|
```bash
|
|
git diff abc123 def456
|
|
```
|
|
|
|
### Specific file
|
|
|
|
```bash
|
|
git diff path/to/file.txt
|
|
```
|
|
|
|
### Changes since last commit
|
|
|
|
```bash
|
|
git diff HEAD
|
|
```
|
|
|
|
### Changes between branches
|
|
|
|
```bash
|
|
git diff main..feature-branch
|
|
```
|
|
|
|
## Understanding diff output
|
|
|
|
```diff
|
|
diff --git a/file.txt b/file.txt
|
|
index 1234567..abcdefg 100644
|
|
--- a/file.txt
|
|
+++ b/file.txt
|
|
@@ -1,4 +1,5 @@
|
|
This line stays
|
|
-This line was removed
|
|
+This line was added
|
|
+Another new line
|
|
This line stays too
|
|
```
|
|
|
|
- `---` lines removed
|
|
- `+++` lines added
|
|
- `@@ -1,4 +1,5 @@` shows line numbers
|
|
- Context lines (unchanged) also shown
|
|
|
|
## Common workflows
|
|
|
|
### Review before committing
|
|
|
|
```bash
|
|
# Check unstaged changes
|
|
git diff
|
|
|
|
# Stage some files
|
|
git add file1.txt
|
|
|
|
# Check what's staged
|
|
git diff --staged
|
|
|
|
# Commit
|
|
git commit
|
|
```
|
|
|
|
### Compare with remote
|
|
|
|
```bash
|
|
git diff origin/main
|
|
```
|
|
|
|
### See what changed in last commit
|
|
|
|
```bash
|
|
git diff HEAD~1 HEAD
|
|
```
|
|
|
|
### Compare specific files between branches
|
|
|
|
```bash
|
|
git diff main feature-branch -- path/to/file.txt
|
|
```
|
|
|
|
## Filtering output
|
|
|
|
### Show only file names
|
|
|
|
```bash
|
|
git diff --name-only
|
|
```
|
|
|
|
### Show statistics
|
|
|
|
```bash
|
|
git diff --stat
|
|
```
|
|
|
|
Output:
|
|
```
|
|
file1.txt | 10 +++++-----
|
|
file2.txt | 5 +++++
|
|
2 files changed, 10 insertions(+), 5 deletions(-)
|
|
```
|
|
|
|
### Show only specific file types
|
|
|
|
```bash
|
|
git diff -- '*.js'
|
|
```
|
|
|
|
## Word-level diff
|
|
|
|
See changes at word level instead of line level:
|
|
|
|
```bash
|
|
git diff --color-words
|
|
```
|
|
|
|
Useful for prose and documentation.
|
|
|
|
## Viewing diffs
|
|
|
|
### In external tool
|
|
|
|
```bash
|
|
git difftool
|
|
```
|
|
|
|
Configure your preferred tool:
|
|
|
|
```bash
|
|
git config --global diff.tool vscode
|
|
```
|
|
|
|
### Pager navigation
|
|
|
|
- `Space` - Scroll down
|
|
- `b` - Scroll up
|
|
- `q` - Quit
|
|
- `/pattern` - Search
|
|
- `n` - Next match
|
|
|
|
## Advanced usage
|
|
|
|
### Ignore whitespace
|
|
|
|
```bash
|
|
git diff -w
|
|
```
|
|
|
|
### Show function names
|
|
|
|
```bash
|
|
git diff -p
|
|
```
|
|
|
|
### Compare working directory to remote
|
|
|
|
```bash
|
|
git diff HEAD origin/main
|
|
```
|
|
|
|
### Unified diff with more context
|
|
|
|
```bash
|
|
git diff -U10
|
|
```
|
|
|
|
Shows 10 lines of context instead of default 3.
|
|
|
|
## Diff between commits
|
|
|
|
### Last commit vs working directory
|
|
|
|
```bash
|
|
git diff HEAD
|
|
```
|
|
|
|
### Two commits ago vs now
|
|
|
|
```bash
|
|
git diff HEAD~2
|
|
```
|
|
|
|
### Specific commits
|
|
|
|
```bash
|
|
git diff abc123..def456
|
|
```
|
|
|
|
## Common scenarios
|
|
|
|
### What did I change?
|
|
|
|
```bash
|
|
git diff
|
|
```
|
|
|
|
### What am I about to commit?
|
|
|
|
```bash
|
|
git diff --staged
|
|
```
|
|
|
|
### What's different from main branch?
|
|
|
|
```bash
|
|
git diff main
|
|
```
|
|
|
|
### What changed in the last commit?
|
|
|
|
```bash
|
|
git diff HEAD~1
|
|
```
|
|
|
|
## Best practices
|
|
|
|
- Review `git diff` before `git add`
|
|
- Review `git diff --staged` before `git commit`
|
|
- Use `--stat` for quick overview
|
|
- Use `--name-only` when you just need file list
|
|
- Configure a visual diff tool for complex changes
|
|
|
|
## Related commands
|
|
|
|
- [git status](/git/status) - See changed files
|
|
- [git show](/git/show) - Show commit contents
|
|
- [git log](/git/log) - View commit history
|
|
- [git add](/git/add) - Stage changes
|