In this lesson, you will learn how to inspect your file modifications using Git’s diff tools before you finalize them in a commit.
The Power of Visibility
When working in your project, it’s easy to lose track of exactly what you’ve typed, deleted, or rearranged. Simply running git status tells you which files have changed, but it doesn’t show you the nature of those changes. Before you commit your work, it is a best practice to review the exact lines of code you are about to save to the project history.
Using git diff
To see the difference between your current working directory and your last commit, use the command:
git diff
When you run this, Git will display a patch-style output. Lines that were removed will appear in red with a minus sign (-), while new lines you’ve added will appear in green with a plus sign (+).
Think of this as a "pre-flight check" for your code. If you see changes that you don't recognize or that were accidentally introduced (like a stray console.log or a debugging comment), git diff makes them immediately obvious, allowing you to fix them before they become a permanent part of your project history.
Refining Your View
If you have a large project and want to look at the changes in one specific file, you can point the tool directly at it:
git diff filename.txt
This keeps your output clean and allows you to focus only on the section of the project you are currently working on. By making a habit of checking your diffs before every commit, you ensure that your history remains clean, intentional, and free of accidental edits. It turns your commit process from a "blind" action into a deliberate review of your contributions.
Key Takeaway: Use git diff to review your line-by-line changes before committing, ensuring your project snapshots are always exactly what you intended.