Welcome to the most powerful superpower in Git โ Branching! โก
This is where developers go from โjust codingโ โ โbuilding like pros.โ
If Git commits are your time machine, then branches are parallel universes ๐ช โ
each branch lets you experiment, fix, or build features without touching the main project.
git branch โ Create, List, Delete BranchesA branch is simply a pointer to a specific commit โ your own copy of the project timeline.
git branch feature-login
This creates a new branch called feature-login (but doesnโt switch to it yet).
git branch
Youโll see:
* main
feature-login
โญ The * marks your current branch.
git branch -d feature-login
or force delete (if not merged yet):
git branch -D feature-login
๐ก Mental Model:
main โ your production line
feature-* โ your R&D labs
You can have infinite labs working in parallel, all under Gitโs watch.
git checkout โ Switch BranchesCreating branches is cool.
But switching between them is where the magic happens.
git checkout feature-login
Now your working directory instantly reflects that branchโs version.
Every file flips to match its last commit โ no drama, no copy-paste.
git checkout -b feature-login
This does both in one line. โก
Letโs say youโre building an e-commerce app:
# On main branch
git checkout -b feature-cart
You add cart.js and commit:
git add cart.js
git commit -m "Add cart functionality"
Switch back:
git checkout main
Now, your cart code vanishes (temporarily) โ
but itโs safe in the feature-cart branch. ๐
git merge โ Combine BranchesAfter finishing your new feature, youโll want to bring it back to main.
git checkout main
git merge feature-cart
If Git can merge automatically โ youโll see:
Fast-forward
and your main branch now includes all updates from feature-cart. ๐
main
โโโ commit A (initial)
โโโ commit B (UI update)
โโโ commit C (security patch)
\
โโโ feature-cart
โโโ commit D (cart feature)
After merge:
main โ A โ B โ C โ D โ
Sometimes, Git canโt decide whose changes to keep โ thatโs a merge conflict. ๐ค
Donโt panic โ hereโs your calm mode checklist ๐งโโ๏ธ๐
Both main and feature-cart modified the same line in index.js.
When merging:
git merge feature-cart
Git says:
CONFLICT (content): Merge conflict in index.js
Automatic merge failed; fix conflicts and commit the result.
code index.js
<<<<<<< HEAD
console.log("Welcome to ShopEasy!");
=======
console.log("Welcome to ShopSmart!");
>>>>>>> feature-cart
console.log("Welcome to ShopEasy โ Smart & Fast!");
git add index.js
git commit -m "Resolve merge conflict in index.js"
Done โ
Your branches are successfully merged!
๐ก Pro Tip: Conflicts arenโt errors โ theyโre conversations between two developers.
Clean branches = clean workflow.
Follow these pro naming standards ๐
| Type | Naming Format | Example |
|---|---|---|
| ๐ Feature | feature/<name> |
feature/auth-system |
| ๐ Bugfix | bugfix/<issue-id> |
bugfix/404-page |
| โ๏ธ Hotfix | hotfix/<short-desc> |
hotfix/payment-crash |
| ๐งช Experiment | experiment/<idea> |
experiment/ui-revamp |
| ๐งฐ Release | release/<version> |
release/v2.1.0 |
๐ง Why this matters:
Easier collaboration
Cleaner pull requests
Instantly recognizable purpose
Letโs simulate a small workflow ๐
# Step 1: Create new feature branch
git checkout -b feature-search
# Step 2: Add code + commit
git add search.js
git commit -m "Add search feature"
# Step 3: Merge to main
git checkout main
git merge feature-search
# Step 4: Delete branch (cleanup)
git branch -d feature-search
โ Done! Youโve completed a full branch โ develop โ merge โ clean cycle.
| Command | Purpose | Analogy |
|---|---|---|
git branch |
Create / view branches | Open new workspace |
git checkout |
Switch branches | Jump between timelines |
git merge |
Combine branches | Merge parallel universes |
git branch -d |
Delete branch | Close finished workspace |
Branch = Experiment lab (safe playground)
Main = Production (never break it)
Merge = Bringing your lab results into production
Conflicts = Team discussions in code form
Visualize it like a tree ๐ณ:
The trunk is your
main, every branch grows new features.
Once tested, merge them back to strengthen the trunk. ๐ช
Youโll learn:
How to connect Git with GitHub โ push, pull, clone, and collaborate like real-world teams.
๐ Continue your journey โ 04-Git-Remotes
โ๏ธ Written with โค๏ธ by Mahesh Shukla โ branching out to better code, one commit at a time.