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 Merge: Combining Branches with git merge

In the previous articles, you learned how to:

  • Create branches

  • Switch between branches

  • Delete branches

  • Work independently on different features

But branches are meant to be temporary workspaces.

Once a feature is complete and tested, you usually want to bring those changes back into the main project.

This process is called merging.

Git provides the git merge command to combine the history of two branches into one.

Whether you're working alone or collaborating with a team, merging is one of the most common Git operations you'll perform.

In this article, you will learn:

  • What merging is

  • Why merging is needed

  • How git merge works

  • Fast-forward merge

  • Three-way merge

  • Merge commits

  • Viewing merge history

  • Common mistakes

  • Best practices

By the end of this article, you'll understand how Git safely combines work from different branches.


What is Git Merge?

Git Merge is the process of combining the changes from one branch into another.

For example:

main

feature-login

After completing work on feature-login, you can merge it into main.

The result is that the main branch now contains the new feature.


Why Do We Need Merge?

Imagine you are building an Online Shopping Website.

The project currently has a stable version.

Three developers are working simultaneously:

  • Developer A → Login System

  • Developer B → Shopping Cart

  • Developer C → Payment Gateway

Each developer works on a separate branch.

Eventually, all completed work needs to become part of the main application.

Git Merge makes this possible.


Visual Representation

Initially:

main

↓

Commit A

↓

Commit B

Create a branch:

feature-login

Repository:

Commit A

↓

Commit B
       │
       └────────► feature-login

Making Changes in the Feature Branch

Switch to:

feature-login

Add two commits.

Commit A

↓

Commit B

↓

Commit C

↓

Commit D

Meanwhile:

main

still points to:

Commit B

The new work exists only in the feature branch.


Merging the Feature

First, switch to the branch that should receive the changes.

Usually:

git switch main

Now merge:

git merge feature-login

Git combines the changes from feature-login into main.


Important Rule

Always remember:

You merge into the branch you are currently on.

Example:

git switch main
git merge feature-login

Meaning:

"Take the changes from feature-login and add them to main."


Before and After Merge

Before:

main

↓

Commit B

feature-login

↓

Commit D

After:

main

↓

Commit D

feature-login

↓

Commit D

Both branches now include the completed work.


Fast-Forward Merge

The simplest type of merge is called a Fast-Forward Merge.

Suppose:

Commit A

↓

Commit B

Create:

feature-login

Add:

Commit C

↓

Commit D

Meanwhile, main has not changed.

Git simply moves the main pointer forward.

Before:

main

↓

B

feature-login

↓

D

After:

main

↓

D

No extra merge commit is created because the history is completely linear.


Why Is It Called "Fast-Forward"?

Git does not need to combine two different histories.

It simply moves the branch pointer forward.

Think of it as moving a bookmark to the latest page in a book.


Three-Way Merge

Now consider a different situation.

Suppose both branches have changed.

Initial history:

Commit A

↓

Commit B

After branching:

Main branch:

Commit E

Feature branch:

Commit C

↓

Commit D

The histories have diverged.

Now Git must combine both sets of changes.


Visual Representation

        C
        │
        D
       /
A──B
       \
        E

Git creates a new commit:

        C
        │
        D
       / \
A──B      M
       \ /
        E

Where:

M

is called a Merge Commit.


What is a Merge Commit?

A merge commit is a special commit that has two parent commits.

Unlike a normal commit, it records the combination of two development histories.

This preserves information about how the branches were joined.


Viewing Merge History

Run:

git log --oneline --graph

Example output:

*   Merge branch 'feature-login'
|\
| * Add login page
| * Create login form
* | Update README
|/
* Initial Commit

The graph clearly shows where the branches split and merged.


Automatic Merge

Most merges happen automatically.

Git compares:

  • Added lines

  • Deleted lines

  • Modified lines

If the changes do not overlap, Git combines them without asking for user intervention.


When Does Git Need Help?

Suppose:

main

changes:

System.out.println("Welcome");

Feature branch changes the same line to:

System.out.println("Welcome User");

At the same time,

main changes it to:

System.out.println("Welcome Student");

Now Git cannot determine which version should be kept.

This situation is called a merge conflict.

We will study merge conflicts in detail in the next article.


Real-World Example

Suppose your team develops a Hospital Management System.

Branches:

feature-patient

feature-billing

feature-appointments

Each developer completes and tests their feature independently.

Once approved, the team switches to main and merges each feature branch.

The application gradually grows while keeping the production code stable.


Merging Multiple Branches

Suppose you have completed:

feature-login

feature-dashboard

feature-payment

Each branch can be merged separately:

git switch main

git merge feature-login

git merge feature-dashboard

git merge feature-payment

After each successful merge, the main branch gains the new functionality.


Should You Delete the Branch After Merging?

Usually, yes.

Once a feature has been merged successfully and is no longer needed:

git branch -d feature-login

This keeps your repository clean.

Remember:

Deleting the branch does not remove the merged commits, because those commits are now reachable from main.


Common Beginner Mistakes

Mistake 1

Trying to merge while on the wrong branch.

Always verify your current branch with:

git branch

The branch marked with * is the one that will receive the merge.


Mistake 2

Deleting a branch before confirming the merge was successful.

Check:

git log --oneline --graph

or test your application before deleting the feature branch.


Mistake 3

Assuming every merge creates a merge commit.

Fast-forward merges often do not create one.


Mistake 4

Ignoring merge conflicts.

Conflicts must be resolved before the merge can be completed.


Best Practices

  • Commit your work before starting a merge.

  • Switch to the correct destination branch before merging.

  • Test the project after every merge.

  • Delete merged feature branches when they are no longer needed.

  • Use descriptive feature branch names.

  • Review the commit history after important merges.


Useful Merge Commands

CommandPurpose
git merge feature-loginMerge a branch into the current branch
git log --graphView branch and merge history
git log --oneline --graphCompact graphical history
git branch -d feature-loginDelete a merged branch

Summary

Git Merge is the mechanism that combines work from different branches into a single development history. Whether Git performs a simple fast-forward merge or creates a merge commit through a three-way merge depends on how the branches have evolved.

Understanding merging is essential because nearly every collaborative Git workflow relies on it. By merging carefully, testing thoroughly, and keeping branches organized, you can integrate new features without disrupting the stability of your project.

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