github-guru

๐ŸŒฟ 03 โ€“ Branching & Merging

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.


๐ŸŒฑ 1. git branch โ€” Create, List, Delete Branches

A branch is simply a pointer to a specific commit โ€” your own copy of the project timeline.

โž• Create a New Branch

git branch feature-login

This creates a new branch called feature-login (but doesnโ€™t switch to it yet).

๐Ÿ” List All Branches

git branch

Youโ€™ll see:

* main
  feature-login

โญ The * marks your current branch.

โŒ Delete a Branch

git branch -d feature-login

or force delete (if not merged yet):

git branch -D feature-login

๐Ÿ’ก Mental Model:

You can have infinite labs working in parallel, all under Gitโ€™s watch.


๐Ÿ”„ 2. git checkout โ€” Switch Branches

Creating branches is cool.
But switching between them is where the magic happens.

๐Ÿš€ Switch to a Branch

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.

๐Ÿง  Shortcut: Create + Switch Together

git checkout -b feature-login

This does both in one line. โšก

๐ŸŒˆ Real-World Example

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. ๐Ÿ›’

๐Ÿงฌ 3. git merge โ€” Combine Branches

After finishing your new feature, youโ€™ll want to bring it back to main.

๐Ÿ’ฅ Merge Your Work

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. ๐ŸŽ‰

๐Ÿงฉ Visual Example

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 โœ…

โš”๏ธ 4. Resolving Merge Conflicts (Step-by-Step)

Sometimes, Git canโ€™t decide whose changes to keep โ€” thatโ€™s a merge conflict. ๐Ÿ˜ค
Donโ€™t panic โ€” hereโ€™s your calm mode checklist ๐Ÿง˜โ€โ™‚๏ธ๐Ÿ‘‡

๐Ÿงฑ Scenario:

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.

โš™๏ธ Step-by-Step Fix

  1. Open the conflicted file:
code index.js
  1. Youโ€™ll see markers like this:
<<<<<<< HEAD
console.log("Welcome to ShopEasy!");
=======
console.log("Welcome to ShopSmart!");
>>>>>>> feature-cart
  1. Manually edit and keep the correct version:
console.log("Welcome to ShopEasy โ€” Smart & Fast!");
  1. Mark it resolved:
git add index.js
  1. Commit the fix:
git commit -m "Resolve merge conflict in index.js"
  1. Done โœ…

    Your branches are successfully merged!

๐Ÿ’ก Pro Tip: Conflicts arenโ€™t errors โ€” theyโ€™re conversations between two developers.

๐Ÿงญ 5. Branch Naming Best Practices

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:


๐Ÿงฉ Real Project Flow (Example)

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.


โšก Summary Cheatsheet

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

๐Ÿง  Brain Hack

Visualize it like a tree ๐ŸŒณ:

The trunk is your main, every branch grows new features.
Once tested, merge them back to strengthen the trunk. ๐Ÿ’ช


๐ŸŒˆ Next Up: โ€œRemote Repositoriesโ€ ๐ŸŒ

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.