Skip to main content

Type Conversion in Python: int(), float(), str() and bool() Explained

Have you ever entered a number into a Python program but received an error when trying to perform a calculation? This is a common problem for beginners. It often happens because the data type of a value is different from what the program expects. For example, when you use Python's input() function, the value entered by the user is returned as a string. If you want to perform mathematical calculations with that value, you may need to convert it into a numeric type first. This process is called type conversion . In this article, we will learn what type conversion is, why it is useful, how to convert values using int() , float() , str() , and bool() , and how to avoid common errors. 1. What Is Type Conversion in Python? Type conversion is the process of converting a value from one data type to another. For example, you may convert: A string into an integer. An integer into a floating-point number. A number into a string. A value into a Boolean value. Consider this example: number = ...

Understanding git restore: Undoing Changes Safely in Git

While developing software, making mistakes is completely normal.

You may accidentally:

  • Delete important code.

  • Modify the wrong file.

  • Add debugging statements.

  • Experiment with a feature that doesn't work.

  • Stage a file by mistake.

Fortunately, Git provides several ways to recover from these situations.

One of the safest and most beginner-friendly commands is git restore.

Introduced in Git 2.23, git restore was designed to make undoing changes easier and less confusing than older commands such as git checkout.

In this article, you will learn:

  • What git restore is

  • When to use it

  • How to restore modified files

  • How to unstage files

  • The difference between restoring and deleting

  • The difference between git restore, git checkout, and git reset

  • Common mistakes and best practices

By the end of this article, you'll know how to safely discard unwanted changes without damaging your repository.


Why Do We Need git restore?

Imagine you have already committed this code:

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

Later, you accidentally change it to:

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

You don't want this change anymore.

Instead of manually editing the file, Git can restore the last committed version for you.

This is exactly what git restore is designed to do.


What is git restore?

The git restore command restores files to an earlier state.

It can:

  • Discard unwanted changes in the Working Directory.

  • Remove files from the Staging Area.

  • Restore files from a specific commit.

Unlike git commit or git add, git restore is an undo command.

It helps you recover from mistakes before they become permanent.


Basic Syntax

git restore <filename>

Example:

git restore Main.java

Git replaces the modified version of Main.java with the last committed version.


Understanding the Git Areas Again

Before using git restore, let us review Git's three main areas.

Working Directory
        │
        ▼
Staging Area
        │
        ▼
Local Repository

The behavior of git restore depends on where your changes currently exist.


Scenario 1: Restoring an Unstaged File

Suppose the latest committed file contains:

System.out.println("Welcome");

You modify it:

System.out.println("Welcome to Git");

Run:

git status

Output:

Changes not staged for commit:

    modified: Main.java

You decide to discard these changes.

Run:

git restore Main.java

Now open the file.

It becomes:

System.out.println("Welcome");

The unwanted modification has disappeared.


What Happened?

Git copied the last committed version from the repository into the Working Directory.

The modified version was discarded.

The lifecycle looked like this:

Repository
      │
      ▼
Working Directory

Modified File

↓

git restore

↓

Working Directory matches Repository

Checking Status After Restore

Run:

git status

Output:

nothing to commit, working tree clean

This means:

  • No modified files

  • No staged files

  • Working Directory matches the latest commit


Scenario 2: Restoring Multiple Files

Suppose your project contains:

Main.java

Student.java

Database.java

All three files have been modified.

Instead of restoring them one by one, run:

git restore Main.java Student.java Database.java

Git restores all selected files.


Scenario 3: Restoring Every Modified File

Suppose you accidentally modified dozens of files.

Instead of specifying every filename, use:

git restore .

The dot (.) means:

Current Directory

Git restores every modified tracked file in the current directory and its subdirectories.

Use this command carefully because it discards all unstaged modifications.


Does git restore Delete Files?

No.

It restores tracked files to a previous version.

Suppose you modify:

Main.java

Running:

git restore Main.java

does not delete the file.

It replaces the current contents with the previous version.


Can git restore Recover Deleted Files?

Suppose you accidentally delete:

Student.java

Git detects:

deleted: Student.java

Run:

git restore Student.java

Git restores the deleted file from the latest commit.

This works only if the file existed in the repository.


Scenario 4: Unstaging a File

Suppose you run:

git add Main.java

Later you realize:

"I don't want this file in the next commit."

Instead of deleting the file, simply remove it from the Staging Area.

Run:

git restore --staged Main.java

The file moves:

Staging Area
      │
      ▼
Working Directory

The file still exists.

Your changes are still present.

Only the staging information is removed.


Understanding --staged

The option:

--staged

means:

Restore changes in the Staging Area.

Without --staged:

git restore Main.java

Git restores the Working Directory.

With:

git restore --staged Main.java

Git restores the Staging Area.


Combining Both Options

Suppose you want to:

  • Remove a file from staging.

  • Discard its modifications completely.

Run:

git restore --staged --worktree Main.java

The file returns to the latest committed version.

Both:

  • Working Directory

  • Staging Area

are restored.


Restoring From a Specific Commit

Git can restore a file from an older commit.

Example:

git restore --source=<commit-hash> Main.java

Suppose:

Commit A

↓

Commit B

↓

Commit C

You can restore Main.java exactly as it existed in Commit A.

This is useful when recovering older versions of files.


git restore vs git checkout

Before Git 2.23, developers commonly used:

git checkout Main.java

to discard changes.

The problem was that git checkout performed multiple unrelated tasks:

  • Switch branches

  • Restore files

This confused beginners.

Git introduced:

git switch

for switching branches.

and:

git restore

for restoring files.

Today, using git restore for file restoration is the clearer and recommended approach.


git restore vs git reset

These commands have different purposes.

CommandPurpose
git restoreRestore file contents
git resetMove branch pointers and optionally unstage or reset commits

A simple way to remember:

  • git restore focuses on files.

  • git reset focuses on history and commit positions.

We will study git reset in detail later.


Real-World Example

Suppose you are developing an Online Banking System.

You modify:

Login.java

Transfer.java

README.md

While testing, you accidentally break Transfer.java.

Instead of manually fixing it, run:

git restore Transfer.java

Git immediately restores the last committed version.

The other modified files remain unchanged.


Common Beginner Mistakes

Mistake 1

Running:

git restore .

without checking the repository.

This discards all unstaged modifications.

Always verify with:

git status

first.


Mistake 2

Thinking git restore deletes files.

It restores tracked files.

It is not a file deletion command.


Mistake 3

Expecting git restore to recover files that were never committed.

If a new file has never been committed, Git has no previous version to restore.


Mistake 4

Confusing:

git restore

with

git reset

These commands solve different problems.


Best Practices

  • Review changes with git diff before restoring.

  • Use git status to understand the current state.

  • Restore individual files whenever possible.

  • Avoid using git restore . unless you are certain.

  • Commit important work before experimenting with risky changes.


Useful git restore Commands

CommandPurpose
git restore Main.javaDiscard modifications in one file
git restore file1 file2Restore multiple files
git restore .Restore all modified tracked files
git restore --staged Main.javaUnstage a file
git restore --staged --worktree Main.javaUnstage and discard changes
git restore --source=<commit> Main.javaRestore a file from a specific commit

Summary

The git restore command provides a safe and straightforward way to undo unwanted changes in Git. Whether you need to discard modifications in the Working Directory, remove files from the Staging Area, or restore files from an earlier commit, git restore offers a clear and focused solution.

Unlike older commands such as git checkout, git restore has a single purpose: restoring files. This makes it easier to learn and reduces the risk of accidental mistakes.

Understanding git restore is an important step toward becoming confident in Git, because it allows you to experiment freely, knowing that you can safely undo changes when necessary.

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...