Skip to main content

Python type() Function

When learning Python, you will work with different types of data, such as numbers, text, decimal values, and Boolean values. Python provides several built-in functions to help you work with these data types. One of the most useful functions is the type() function . The type() function tells you the data type of a value or variable. It helps you understand what kind of data Python is working with and can make debugging your programs easier. For example, consider these variables: age = 20 price = 99.50 name = "Rahul" is_student = True Although all four variables store data, their data types are different. Python allows us to check these types using type() . Let's understand how it works with simple examples. 1. What Is the type() Function in Python? The type() function is a built-in Python function that returns information about the type of an object. In beginner-level programming, you can use it to check whether a value is an integer, a floating-point number, a string,...

Understanding git diff: Comparing Changes Before You Commit

Imagine you have been working on a Java project for several hours. You have modified multiple files, fixed a few bugs, and added a new feature.

Before creating a commit, you ask yourself:

  • What exactly changed?

  • Which lines of code were added?

  • Which lines were removed?

  • Did I accidentally change something I shouldn't have?

  • What am I about to commit?

Instead of opening every file manually and comparing the changes, Git provides a powerful command:

git diff

The git diff command compares different versions of your files and shows exactly what has changed.

It is one of the most useful commands in Git because it allows you to review your work before creating a commit.

Professional developers use git diff regularly to verify their changes and avoid accidental mistakes.

In this article, you will learn:

  • What git diff is

  • How Git compares files

  • Different types of git diff

  • Understanding the output

  • Viewing staged and unstaged changes

  • Comparing commits

  • Comparing branches

  • Common mistakes

  • Best practices

By the end of this article, you will be able to confidently inspect every change in your repository.


What is git diff?

The git diff command compares two versions of files and displays the differences between them.

Basic syntax:

git diff

It does not change your files.

It only shows the differences.

Think of it as a comparison tool.


Why is git diff Important?

Suppose you modified a file yesterday.

Today you don't remember what you changed.

Instead of searching manually, simply run:

git diff

Git immediately shows:

  • Added lines

  • Removed lines

  • Modified lines

This helps you verify your work before creating a commit.


Understanding Comparison

Suppose your original code was:

System.out.println("Hello");

Now you modify it to:

System.out.println("Hello Git");

Git compares these two versions.

The difference is:

  • Old line removed

  • New line added

This comparison is called a diff.


How Git Compares Files

Git compares:

Old Version
      │
      ▼
Current Version

It identifies:

  • Added lines

  • Deleted lines

  • Modified lines

Git does not compare screenshots or compiled programs.

It compares the actual text content.


First Example

Suppose your file contains:

public class Main
{
    public static void main(String args[])
    {
        System.out.println("Hello");
    }
}

Save and commit the file.

Now change:

System.out.println("Hello");

to

System.out.println("Hello Git");

Run:

git diff

Git may display:

diff --git a/Main.java b/Main.java
index 8c9f123..5a2b891 100644
--- a/Main.java
+++ b/Main.java
@@ -4,5 +4,5 @@

-        System.out.println("Hello");
+        System.out.println("Hello Git");

At first, this output may look confusing.

Let us understand it line by line.


Understanding diff --git

Example:

diff --git a/Main.java b/Main.java

This tells Git that it is comparing two versions of the same file.

The prefixes:

a/

and

b/

represent:

  • Old version

  • New version

respectively.


Understanding the --- Line

Example:

--- a/Main.java

This represents the original version of the file.


Understanding the +++ Line

Example:

+++ b/Main.java

This represents the modified version.


Understanding the @@ Line

Example:

@@ -4,5 +4,5 @@

This line tells Git where the changes occurred.

For beginners, you do not need to memorize this syntax.

Simply remember:

It tells Git which section of the file is being compared.


Understanding Removed Lines

Lines beginning with:

-

represent removed lines.

Example:

- System.out.println("Hello");

This line existed before but no longer exists.


Understanding Added Lines

Lines beginning with:

+

represent newly added lines.

Example:

+ System.out.println("Hello Git");

This line is part of the new version.


Visual Representation

Original File
      │
      ▼
System.out.println("Hello");

      │
      ▼
Modified File

System.out.println("Hello Git");

Git highlights:

- Hello
+ Hello Git

git diff Shows Unstaged Changes

The basic command:

git diff

compares:

Working Directory
        │
        ▼
Staging Area

If you modify a file but have not staged it, Git displays those changes.

Example:

Committed
        │
Modify File
        ▼
Working Directory

Run:

git diff

Git displays the modifications.


What Happens After git add?

Suppose you run:

git add Main.java

Now run:

git diff

Git may display:

(no output)

Why?

Because there are no unstaged changes.

The latest version is already in the Staging Area.


Viewing Staged Changes

To compare the Staging Area with the latest commit, use:

git diff --staged

or

git diff --cached

Git now displays the staged changes that will be included in the next commit.


Understanding the Three Comparisons

Git can compare three important areas.

Working Directory vs Staging Area

git diff

Shows unstaged changes.


Staging Area vs Repository

git diff --staged

Shows staged changes.


Commit vs Commit

git diff <commit1> <commit2>

Compares two commits.


Visual Diagram

Working Directory
        │
        │ git diff
        ▼
Staging Area
        │
        │ git diff --staged
        ▼
Repository

Comparing Two Commits

Suppose your history is:

Commit A

↓

Commit B

↓

Commit C

You can compare:

Commit A and Commit C

using:

git diff <commitA> <commitC>

Git displays every change introduced between those commits.


Comparing Two Branches

Suppose you have:

main

feature-login

Compare them:

git diff main feature-login

Git shows the differences between both branches.

This becomes extremely useful during team development.


Comparing One File

Suppose you only want to inspect:

Main.java

Run:

git diff Main.java

Git displays changes only for that file.


Comparing Multiple Files

git diff Main.java Student.java

Git compares both files.


Viewing Word-Level Differences

Git can highlight changes at the word level.

Example:

git diff --word-diff

Instead of entire lines, Git highlights individual changed words.

This is useful for documentation and text files.


Real-World Example

Suppose you are building a Library Management System.

You modify:

Login.java

Book.java

README.md

Before committing, run:

git diff

Git shows:

  • Login validation updated.

  • Book search improved.

  • README installation instructions changed.

You notice an accidental change inside Book.java.

Instead of committing immediately, you correct the mistake.

This is why professional developers always review their changes first.


Why Use git diff Before Every Commit?

Without checking differences, you might accidentally commit:

  • Debug code

  • Temporary print statements

  • Passwords

  • API keys

  • Unfinished features

  • Formatting mistakes

Running:

git diff

takes only a few seconds and helps prevent these problems.


Common Beginner Mistakes

Mistake 1

Thinking git diff changes files.

It doesn't.

It only displays differences.


Mistake 2

Running git diff after staging files and expecting output.

Remember:

git diff

shows only unstaged changes.

Use:

git diff --staged

for staged changes.


Mistake 3

Ignoring the diff before committing.

Always review your changes.


Mistake 4

Trying to memorize every symbol.

Focus first on:

  • + means added.

  • - means removed.

The remaining details become easier with practice.


Best Practices

  • Run git diff before staging changes.

  • Run git diff --staged before committing.

  • Review every modified file.

  • Never commit code you haven't reviewed.

  • Use file-specific comparisons when working on large projects.

  • Make reviewing changes a habit.


Useful git diff Commands

CommandPurpose
git diffShow unstaged changes
git diff --stagedShow staged changes
git diff --cachedSame as git diff --staged
git diff Main.javaCompare one file
git diff file1 file2Compare multiple files
git diff commit1 commit2Compare two commits
git diff main featureCompare two branches
git diff --word-diffShow word-level differences

Summary

The git diff command is one of Git's most valuable tools for reviewing changes before they become part of your project's history.

It compares different versions of files and highlights additions, deletions, and modifications. Whether you are reviewing unstaged changes, staged changes, commits, or branches, git diff helps you understand exactly what has changed.

Using git diff regularly improves code quality, prevents accidental commits, and helps maintain a clean and reliable Git history.

Comments

Popular posts from this blog

Understanding Comments in Java: A Beginner’s Guide

When learning Java programming, one of the first concepts you’ll come across is comments . Comments may not affect how a program runs, but they play a crucial role in making code more understandable, readable, and maintainable. Let’s dive deeper into what comments are, why they’re important, and the different types of comments in Java. What are Comments in Java? A comment in Java is a piece of text written within a program that is ignored by the compiler . In other words, comments are notes for humans, not instructions for machines. They help developers explain the purpose of code, describe logic, or temporarily disable certain lines of code without deleting them. Why are Comments Important? 📌 Readability – Comments make code easier to understand for you and others. 📌 Maintenance – When you revisit your code after months, comments remind you what you were doing. 📌 Collaboration – In team projects, comments explain code to fellow developers. 📌 Debugging – You c...

Java Program to Check Voting Eligibility

In this program, we’ll take the user’s age as input and check whether the person is eligible to vote or not. In India, the minimum age required to vote is 18 years . 💻 Program Code // Java Program to Check Voting Eligibility import java.util.Scanner; public class VotingEligibility { public static void main(String[] args) { // Create Scanner object to take input from user Scanner sc = new Scanner(System.in); // Ask user to enter age System.out.print("Enter your age: "); int age = sc.nextInt(); // Check eligibility if (age >= 18) { System.out.println("You are eligible to vote."); } else { System.out.println("You are not eligible to vote yet."); System.out.println("You can vote after " + (18 - age) + " year(s)."); } sc.close(); // Close the scanner } } 🧠 Explanation import java.util.Scanner; → Imp...

Java Program to Calculate Total Marks and Average of Five Subjects

💡 Program Explanation In this program: The user will input marks of five subjects . The program will calculate the total marks and average . It will then display both results clearly. 🧑‍💻 Java Code: // Java Program to Calculate Total Marks and Average of Five Subjects import java.util.Scanner; // Importing Scanner class for user input public class TotalAndAverage { public static void main(String[] args) { // Creating Scanner object to take input from user Scanner sc = new Scanner(System.in); // Declaring variables for five subjects double subject1, subject2, subject3, subject4, subject5; // Taking marks input for each subject System.out.print("Enter marks of Subject 1: "); subject1 = sc.nextDouble(); System.out.print("Enter marks of Subject 2: "); subject2 = sc.nextDouble(); System.out.print("Enter marks of Subject 3: "); subject3 = sc.nextDo...