Git Diff Fundamentals
What is Git Diff?
Git diff is a powerful command that allows developers to compare changes between different states of a repository. It helps track modifications in files, compare commits, branches, and understand the exact differences in code.
Basic Diff Commands
Comparing Working Directory and Staging Area
git diff
This command shows unstaged changes in your working directory compared to the staging area.
Comparing Staged Changes
git diff --staged
Displays changes that have been staged but not yet committed.
Diff Comparison Types
Comparison Type |
Command |
Description |
Working vs Staged |
git diff |
Unstaged changes |
Staged vs Last Commit |
git diff --staged |
Staged changes |
Between Commits |
git diff commit1 commit2 |
Changes between specific commits |
Between Branches |
git diff branch1..branch2 |
Differences across branches |
Advanced Diff Options
Detailed Diff Visualization
git diff --word-diff
Shows changes at the word level, helpful for precise tracking.
Ignoring Whitespace Changes
git diff -w
Filters out whitespace modifications for cleaner comparisons.
Workflow Example with Diff
gitGraph
commit id: "Initial Commit"
branch feature
checkout feature
commit id: "Add new function"
commit id: "Modify implementation"
checkout main
merge feature
By understanding Git diff fundamentals, developers using LabEx can effectively track and manage code changes with precision and clarity.