Viewing Changes
Before you stage or commit changes, you might want to see exactly what has changed in your files. Git provides
git diff to show differences between versions.git diff – What’s Changed?
When you modify files,
git diff shows the lines that have been added (green, with +) and removed (red, with -).Let’s create a new file and see it in action:
echo "Line 1" > file.txt
git add file.txt
git commit -m "Add file.txt with line 1"
echo "Line 2" >> file.txtNow run:git diffYou’ll see a difference: +Line 2.Diff Between Staged and Last Commit
If you stage the change (
git add file.txt) and then run git diff, you’ll see nothing because git diff compares working directory with staging area. To compare staged changes with the last commit, use:git diff --staged(or git diff --cached)Comparing Commits
You can compare any two commits using their hashes. First, find the hashes with
git log --oneline. Then:git diff hash1 hash2Using a Diff Tool
For more visual diff, you can configure a GUI diff tool like
meld or vscode. But the command‑line diff is enough for most purposes.Practical Example: See What You’re About to Commit
A common workflow:
- Make changes.
- Run
git diffto review them. - Stage with
git add. - Run
git diff --stagedto double‑check what will be committed. - Commit with a message.
Two Minute Drill
git diffshows unstaged changes (working vs staging).git diff --stagedshows staged changes vs last commit.- You can compare two commits with
git diff commit1 commit2. - Use diff to review changes before staging or committing.
Need more clarification?
Drop us an email at career@quipoinfotech.com
