Git diff = See EXACTLY what changed in your files.
Why is it crucial?
In short: diff = your glasses for seeing changes!
mkdir demo-diff && cd demo-diff
git init
echo "print('Version 1')" > app.py
git add . && git commit -m "Initial version"
# Modify the file
echo "print('Version 2 - Updated!')" > app.py
# See the changes
git diff # Comparison working directory vs last commits
# Add to staging
git add app.py
# See staged difference vs last commits
git diff --staged
# See difference between 2 commits
git log --oneline # Retrieve the hashes
git diff <hash1> <hash2>| Command | Compares |
|---|---|
git diff | Working directory vs last commit |
git diff --staged | Staging area vs last commit |
git diff HEAD | Working directory + staging vs last commit |
git diff <commit1> <commit2> | Between 2 specific commits |
git diff <branch1> <branch2> | Between 2 branches |
Typical format:
diff --git a/app.py b/app.py
index abc123..def456 100644
--- a/app.py # Old file
+++ b/app.py # New file
@@ -1 +1 @@ # Position of the changes
-print('Version 1') # Deleted line (red)
+print('Version 2') # Added line (green)Express reading:
--- and +++ = compared files- = deleted (red in the terminal)+ = added (green in the terminal)| Command | Use |
|---|---|
git diff --name-only | Just the names of modified files |
git diff --stat | Change statistics |
git diff --word-diff | Differences at the word level |
git diff HEAD~1 HEAD | Last commit vs the one before |
Diff = your change detector. Use it ALL THE TIME!