Viewing Changes with git diff
Summary: See what has changed using git diff
before committing.
Version control is at the heart of modern software development, helping teams and individuals track and manage changes in code. One essential tool that comes with Git, the most popular version control system, is git diff
. This command allows you to see differences between commits, branches, files, and more. In this article, we'll delve into how git diff
works and how you can use it to understand what has changed in your project before committing.
Why Use git diff
?
Before you commit your changes to the repository, it’s crucial to review what exactly has changed. This helps you:
- Catch mistakes before they’re saved.
- Understand the scope and impact of your changes.
- Ensure only relevant code is staged for commit.
The Basics of git diff
When you make changes to files in your working directory, those changes aren't automatically saved in Git. To see what's changed since your last commit, use:
git diff
This command compares your working directory with the staging area (also called the index). It shows you all modifications that haven’t yet been staged (with git add
).
Sample Output:
diff --git a/app.py b/app.py
index 3c4e12e..5fa21a6 100644
--- a/app.py
+++ b/app.py
@@ -1,6 +1,8 @@
import sys
-def main():
- print("Hello, world!")
+def main():
+ print("Hello, Git!")
+
main()
Red lines (-
) show removals, green lines (+
) show additions.
Diff Between Staged and Unstaged Changes
Suppose you've already staged some changes with git add
, but you made further edits before committing. To see what's different between your staged and unstaged files, run:
git diff
This will only show unstaged changes.
-
To see what’s staged and ready to be committed, use:
git diff --cached
or
git diff --staged
Comparing With Previous Commits
You can also use git diff
to compare your work with past commits, or even between two different commits:
-
Comparing with the last commit:
git diff HEAD
-
Comparing two specific commits:
git diff commit1 commit2
Replace
commit1
andcommit2
with actual commit hashes.
Diffing Between Branches
Want to see what's different between your current branch and another branch?
git diff main
This shows the difference between your current branch and main
.
To compare two arbitrary branches:
git diff branch-a branch-b
Limiting Diffs to Specific Files
You can narrow your diff to specific files:
git diff app.py
Or for staged changes:
git diff --cached app.py
A More Readable Diff: --color-words
and --stat
If you're dealing with large diffs, try:
-
Colored diffs:
git diff --color-words
-
Summary of changes:
git diff --stat
Using git diff
in Daily Workflow
Here's a typical workflow involving git diff
:
-
Edit your files.
-
Check for any changes:
git diff
-
Review modifications.
-
Stage changes selectively:
git add file1.py
-
Check what will be committed:
git diff --cached
-
Commit:
git commit -m "Describe your changes"
Wrapping Up
git diff
is a powerful command that helps you preview and understand code changes before they are staged or committed. By using git diff
regularly, you can make more informed decisions, improve code quality, and avoid accidental bugs in your commit history. Try incorporating git diff
into your workflow and see the difference it makes!
Further Reading: