The Git Diff Command
Table of contents
- Introduction to git diff
- Diff of the working directory
- Diff of the staging area
- Diff between commits
- Diff between branches
- Useful options
- Visual diff tools
1 - Introduction to git diff
git diff lets you see the differences between:
- The modified files and the last commit
- The staging area and the last commit
- Any two commits
- Two branches
Output format
diff --git a/file.txt b/file.txt
index abc1234..def5678 100644
--- a/file.txt
+++ b/file.txt
@@ -1,3 +1,4 @@
Ligne 1
-Ligne supprimée
+Ligne ajoutée
+Nouvelle ligne
Ligne 3
| Symbol | Meaning |
|---|---|
--- | Source file (old) |
+++ | Destination file (new) |
- | Removed line (red) |
+ | Added line (green) |
@@ | Position in the file |
🔝 Back to table of contents
2 - Diff of the working directory
View the unstaged changes
git diff
This command shows the differences between:
- Working directory (modified files)
- Last commit (or the staging area if files are staged)
Example
# Modifier un fichier
echo "Nouvelle ligne" >> README.md
# Voir les modifications
git diff
Result:
diff --git a/README.md b/README.md
index abc1234..def5678 100644
--- a/README.md
+++ b/README.md
@@ -1,3 +1,4 @@
# Mon Projet
Description du projet
+Nouvelle ligne
Diff of a specific file
git diff README.md
git diff src/app.js
🔝 Back to table of contents
3 - Diff of the staging area
View the staged changes
git diff --staged
# ou
git diff --cached
This command shows the differences between:
- Staging area (files added with
git add) - Last commit
Example
# Modifier et stager un fichier
echo "Changement" >> README.md
git add README.md
# Voir ce qui sera commité
git diff --staged
Important difference
# Modifications PAS encore staged
git diff
# Modifications PRÊTES à être commitées
git diff --staged
🔝 Back to table of contents
4 - Diff between commits
Between two commits
git diff <commit1> <commit2>
Examples
# Entre deux commits spécifiques
git diff abc1234 def5678
# Entre le commit actuel et un ancien
git diff HEAD~3 HEAD
# Entre le dernier commit et l'avant-dernier
git diff HEAD~1 HEAD
# ou
git diff HEAD^ HEAD
View the changes of a commit
# Ce qu'un commit a changé
git diff abc1234^ abc1234
# Plus simple avec show
git show abc1234
Diff from a commit up to now
git diff abc1234 HEAD
git diff abc1234..HEAD
🔝 Back to table of contents
5 - Diff between branches
Compare two branches
git diff main feature/login
See what will change during a merge
# Ce que feature/login apporte par rapport à main
git diff main...feature/login
💡 The three dots (
...) compare from the common ancestor.
Difference between .. and ...
# Deux points : compare les extrémités
git diff main..feature # Tous les changements entre les deux
# Trois points : depuis l'ancêtre commun
git diff main...feature # Ce que feature a ajouté depuis la divergence
Visualization
A---B---C feature
/
D---E---F---G main
git diff main..feature → Compare G avec C
git diff main...feature → Compare E avec C (changements sur feature)
🔝 Back to table of contents
6 - Useful options
Display
# Voir uniquement les noms de fichiers modifiés
git diff --name-only
# Noms + statut (ajouté, modifié, supprimé)
git diff --name-status
# Statistiques (résumé)
git diff --stat
--stat example
git diff --stat HEAD~3 HEAD
Result:
README.md | 5 +++++
src/app.js | 12 ++++++------
style.css | 3 +--
3 files changed, 12 insertions(+), 8 deletions(-)
Context options
# Plus de lignes de contexte (défaut: 3)
git diff -U5
# Ignorer les espaces
git diff --ignore-all-space
git diff -w
# Ignorer les changements d'espaces en fin de ligne
git diff --ignore-space-at-eol
Filter by file type
# Seulement les fichiers .js
git diff -- '*.js'
# Seulement un dossier
git diff -- src/
# Exclure un dossier
git diff -- . ':!node_modules'
Colored diff
# Avec mots colorés (plus lisible)
git diff --word-diff
# Couleurs personnalisées
git diff --color-words
🔝 Back to table of contents
7 - Visual diff tools
Configure a diff tool
# VS Code
git config --global diff.tool vscode
git config --global difftool.vscode.cmd 'code --wait --diff $LOCAL $REMOTE'
# Meld (Linux)
git config --global diff.tool meld
# P4Merge
git config --global diff.tool p4merge
Use the configured tool
git difftool
git difftool HEAD~1 HEAD
git difftool main feature/login
Recommended VS Code extensions
| Extension | Description |
|---|---|
| GitLens | Inline diff, blame, history |
| Git Graph | Graphical visualization |
| Git History | File history |
Diff on GitHub
On GitHub, you can compare:
- Commits:
github.com/user/repo/compare/abc1234...def5678 - Branches:
github.com/user/repo/compare/main...feature
Command summary
| Command | Description |
|---|---|
git diff | Working directory vs staging/HEAD |
git diff --staged | Staging vs HEAD |
git diff A B | Commit A vs commit B |
git diff branch1 branch2 | Branch 1 vs branch 2 |
git diff --stat | Statistical summary |
git diff --name-only | File list only |
git difftool | Open in a visual tool |