Git Diff Essentials
Understanding Git Diff Basics
Git diff is a powerful command for tracking and comparing code changes in version control systems. It allows developers to examine modifications between commits, branches, files, and working directories.
Core Concepts of Git Diff
Git diff provides detailed insights into code modifications through several key perspectives:
Diff Type |
Description |
Command Example |
Working Directory Changes |
Shows unstaged modifications |
git diff |
Staged Changes |
Displays changes staged for commit |
git diff --staged |
Commit Comparisons |
Reveals differences between commits |
git diff commit1 commit2 |
Practical Code Examples
## Basic diff between working directory and last commit
git diff
## Compare staged changes
git diff --staged
## Compare specific files
git diff path/to/file.txt
## Compare between branches
git diff main feature-branch
Diff Visualization Workflow
graph LR
A[Working Directory] -->|Changes| B[Staging Area]
B -->|Commit| C[Local Repository]
C -->|Push| D[Remote Repository]
The mermaid diagram illustrates how git diff tracks changes across different stages of version control, enabling precise code change monitoring.