Git Diff Basics
Understanding Git Diff Fundamentals
Git diff is a powerful command in version control that allows developers to track and compare changes across source code files. It provides a comprehensive mechanism for code comparison and source code tracking, enabling precise identification of modifications between different Git states.
Basic Diff Operations
Developers can utilize git diff in multiple contexts:
## Compare working directory with staging area
git diff
## Compare staged changes with last commit
git diff --staged
## Compare two specific commits
git diff commit1 commit2
Diff Command Variations
Diff Command |
Purpose |
Scope |
git diff |
Working directory changes |
Unstaged modifications |
git diff --staged |
Staged changes |
Files ready for commit |
git diff HEAD |
All current changes |
Working directory and staging area |
Code Comparison Workflow
graph LR
A[Working Directory] -->|git diff| B[Unstaged Changes]
B -->|git add| C[Staging Area]
C -->|git diff --staged| D[Committed Changes]
Practical Code Example
## Initialize git repository
git init
## Create sample file
echo "Initial content" > example.txt
## First commit
git add example.txt
git commit -m "Initial commit"
## Modify file
echo "Updated content" > example.txt
## View differences
git diff
This example demonstrates basic git diff functionality for tracking source code modifications in a version control environment.