Git Diff Command and Advanced Git Log Options

In the last blog, we explored advanced Git log features.
Now, let’s move forward and talk about the Git diff command and how it works, along with some more powerful Git log options.


What is Git Diff?

The git diff command shows the difference between files in your working directory and the staging area.
It highlights:

  • Lines added (green, prefixed with +)
  • Lines removed (red, prefixed with -)

Example: Comparing Working Directory and Staging Area

Modify a file:

echo "New line added" >> first.txt

Check status:

git status

Output:

modified: first.txt

Now run git diff:

git diff

Output:

+ New line added

This shows the difference between the working directory and the staging area.


Example: Deletions and Modifications

Delete a line in first.txt and modify another:

vim first.txt

Run:

git diff

Output:

- Old line removed
+ Updated new line

Git Diff After Staging

Stage changes:

git add first.txt

Now check:

git diff

Output:

# No difference

Because git diff by default compares working directory vs staging area. To compare staging area vs latest commit:

git diff --staged

Using Git Log with Patch Option

You can view the changes introduced in each commit using:

git log -p

This shows:

  • Which lines were added or removed
  • In which commit those changes happened

Searching for a String in Commits

To find which commit introduced a specific string:

git log -S "modified" --patch

This will show the commit(s) where the word “modified” first appeared or was changed.


Summary

In this blog, we learned:

  • How to use git diff to compare working directory and staging area.
  • How git diff --staged compares staged changes with the last commit.
  • How to use git log -p to see commit history with detailed diffs.
  • How to search commits containing a specific string using git log -S.

In the next blog, we’ll move on to explore more advanced Git commands for comparing and tracking code history.


Keep Learning 🚀

👉 Subscribe to Learning Ocean – Subscribers get coupon codes for my courses, early access to blogs and courses, and even exclusive YouTube videos.

👉 My YouTube Channel – More videos, more fun, and lots of learning!

👉 📺 Watch this topic in video form

Stay curious, keep coding, and let’s make learning fun together! 🎉