Branches are one of the most powerful features of Git. They allow us to work on new features, experiments, or fixes without affecting the main code. Once we understand branches, collaborating on GitHub (or any Git platform) becomes much easier.
This post explains Git branches in the simplest possible way — using examples, visuals, and step-by-step explanations.
🧩 Prerequisites
Before practicing commands related to Git branches in this post, ensure that:
- Git is installed and properly set up on our system as explained in Installing and Setting Up Git on Windows, macOS, and Linux.
- We have a local Git repository initialized and at least one file committed, following Git Repository Structure and Workflow Stages.
🌱 What Is a Branch in Git?
A branch is simply a pointer to a series of commits — a separate line of development.
Think of it like this:
- Our main branch holds the stable version of our project.
- When we create a new feature branch, we get a safe space to work on that feature without affecting the main branch.
When we’re done, we merge our changes back to the main branch.
🧠 Simple Analogy
Imagine our project is a tree:
- The main branch is the trunk
- New branches grow out from it when needed
- Later, those branches can join back into the trunk
That’s exactly how Git branches work.
🌿 Why Do We Use Branches?
Branches help us:
- Work on new features safely
- Fix bugs without touching ongoing work
- Try out ideas without breaking anything
- Collaborate with others without messing up the main project
Branches isolate our work, but also let us merge changes easily when ready.
🌳 The Default Branch: main (or master)
When we create a Git repository:
- The default starting branch is usually called the main
- Some older repositories still use master
Both work the same — it’s just a name.
But modern practice prefers main.
🔍 Exercise 1: Check and Rename Your Default Branch (main vs. master)
Depending on our Git version or system settings, our repository’s default branch may be called master or main.
Modern platforms like GitHub now use main as the standard name.
Check our current branch by running:
git branch
If we see:
* master
Rename it to main using:
git branch -m master main
This updates our local branch name to main, ensuring consistency with modern Git workflows. If it already shows main, we are good to continue.
To confirm, check our current branch using git branch, and make sure it is main before we proceed:

✨ Creating a New Branch
We can create a new branch using the command git branch <branch name>.
Exercise 2: Creating a new feature branch
Assume we want to create a new login feature called feature-login. To create a new branch, use:
git branch feature-login
This creates a branch called feature-login — but we’re still on our current branch.
To switch to the new branch, run:
git checkout feature-login

Alternatively, create and switch in one step:
git checkout -b feature-login
🔄 Switching Between Branches
If you are using newer Git versions (recommended), we can switch between branches using git switch main. To move between branches in old and new Git versions, we can use git checkout command.
Exercise 3: Switching between branches
If we are using newer Git versions (recommended), run:
git switch main

To move between branches in old and new Git versions:
git checkout main

📝 Working on a Branch
When we are on a branch, any new commits we make stay only on that branch.
Example:
git add .
git commit -m "Add login page UI"
These commits belong to our feature-login branch and don’t affect the main branch until we merge.
🔀 Merging a Branch into main
Merging means taking the changes from one branch and combining them into another.
Most commonly:
feature-branch ---> main
Git automatically combines changes and updates the target branch.
We can merge by switching to main branch and using the merge command.
🎯 When Do You Use Merging?
Use merging when:
- We’ve finished working on a feature
- We’ve fixed a bug
- We want to bring updates from
maininto your working branch - We want everyone’s code to stay in sync
Exercise 4: Merging feature branch to main branch
When our feature is ready:
1️⃣ Switch to the main branch
git switch main
2️⃣ Merge the feature branch
git merge feature-login
Now the changes from feature-login become part of main.
🧹 Deleting a Branch
Once a branch is merged and no longer needed, we can delete using the -d flag
Exercise 5: Deleting a Branch
Run:
git branch -d feature-login
This deletes the branch locally.
🌐 Branches and GitHub
When working with GitHub:
- Each feature branch is pushed separately
- We create a Pull Request to merge a branch into main.
- Team members can review before merging
- GitHub shows differences between branches automatically
A full remote (GitHub, Azure Repos etc.) branching workflow including pull requests will be explained in a future post.
🎉 Quick Summary
- A branch is a separate line of development
mainis the default stable branch- Create a branch → work safely → merge when ready
- Branches help avoid breaking our main codebase
- Perfect for features, bug fixes, and collaboration
🎁 Bonus: What If There Is a Merge Conflict?
Conflicts happen when the same lines of code are changed differently in two branches. Sometimes, Git cannot automatically combine changes — especially when both branches modify the same line in a file. When this happens, Git creates a merge conflict.
Git will show us exactly where the conflict is. We fix it manually, mark it resolved, and commit the merge. A simple conflict example will be covered in a future post.
A conflict looks like this inside a file:
<<<<<<< HEAD
this is from main
=======
this is from your feature branch
>>>>>>> feature-login
To resolve it, you must:
- Manually edit the file
- Choose which version to keep
- Remove the conflict markers
- Save, stage, and commit your changes
git add filename
git commit
Don’t worry if this feels confusing — this is just a preview. We will explore this in detail in a future blog post.