Skip to main content

Command Palette

Search for a command to run...

Git from Zero to Hero — Part 2: Understanding Diff (What Changed?)

Updated
2 min read
M
I am a lifelong learner. I learn new things and write about them step by step.

>>> Previous: Part 1 — What is Version Control?

What is Diffing?

In the previous part, we talked about Version Control.

Now let’s understand a key concept behind it:

Diffing

Diffing means comparing two files and finding out what has changed between them.

Why Do We Need Diff?

Imagine you have two Python files:

  • prog_buggy.py → original version

  • prog_correct.py → bug-fixed version

Now the question is:

What exactly changed?

Manual vs Automatic Comparison

One way is to compare them manually:

  • Open both files

  • Put them side by side

  • Check line by line

But this is slow and error-prone.

Instead, we use a tool called diff.

Basic Diff Command

diff prog_buggy.py prog_correct.py

This command shows the difference between two files.

Example:

10c10
< return total / (len(numbers) - 1)
---
> return total / len(numbers)

What does this mean?

  • < → line removed from the first file

  • > → line added in the second file

  • 10c10 → line 10 changed

Multiple Changes Example

10c10,11
< return total / (len(numbers) - 1)
---
> # bug fixed here
> return total / len(numbers)

29c30
< main()
---
> main()

This means:

  • One line changed into two lines

  • Another change happened later in the file

Side-by-Side Comparison

If you want a visual comparison:

diff -y prog_buggy.py prog_correct.py

This shows both files side by side.

Developers often use:

diff -u prog_buggy.py prog_correct.py

Example:

@@ -7,7 +7,8 @@
- return total / (len(numbers) - 1)
+ # bug fixed
+ return total / len(numbers)

Meaning:

  • → removed lines

  • → added lines

Why is Diff Important?

Diff helps us:

  • Understand changes quickly

  • Track bugs and fixes

  • See exactly what was modified

And most importantly:

>>> Git uses this concept internally

Bonus: Useful Tools

Some tools that help visualize differences:

  • kdiff3

  • meld

  • wdiff

Final Thought

Learning how to read diff output is a very useful skill.

It will help you understand Git much better.

Next: Part 3 — Working with Patch