Skip to main content

Branch

Introduction: Why Branching Matters πŸŒ³β€‹

Imagine working on a big project with your team. Someone is fixing bugs while others are building new features. How do you all work together without stepping on each other's toes? Git branches are the answer!

What is a Branch? πŸŒΏβ€‹

A branch is a parallel version of your code where you can work independently without affecting the main codebase.

Real-World Analogy: Writing a Book πŸ“šβ€‹

Main Story (main branch)
Chapter 1 βœ“
Chapter 2 βœ“
Chapter 3 βœ“
↓
β”Œβ”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
↓ ↓
Experimental Alternative
Ending Branch Subplot Branch
β”‚ β”‚
β”‚ Try different β”‚ Add new
β”‚ conclusion β”‚ character
β”‚ β”‚
↓ Like it? Merge! ↓ Didn't work? Delete!
β”‚ βœ—
Main Story
Chapter 4 βœ“ (with new ending)

Key Concept:

  • Main branch = The official, working version
  • Feature branches = Experimental playgrounds
  • Merge = Bring changes back when ready
  • Delete = Discard failed experiments

Visual: How Branching Works​

main ●────●────●────●──────●
↓ β†—
feature ●───●───● (created, worked on, merged back)

Timeline:

  1. Start from main branch (commit 2)
  2. Create feature branch
  3. Work on feature independently
  4. Merge back to main when ready
  5. Main branch now has your feature!

Why Use Branches? πŸ’‘β€‹

1. Safe Experimentation πŸ§ͺ​

Try new ideas without breaking working code!

main ●────●────● (stable, always works)
↓
experiment ●───● (test crazy idea safely)

2. Parallel Development πŸ‘₯​

Multiple features at once:

main ●────●────────●────●
↓ β†—
feature-login ●───●───● (Team A works here)
↓ β†—
feature-payments ●───● (Team B works here)

3. Organized Workflow πŸ“‹β€‹

Keep different types of work separated:

main Production-ready code
↓
develop Integration branch
↓
feature/* New features
↓
hotfix/* Urgent bug fixes

4. Easy Rollback βͺ​

Mistakes happen. Delete bad branches!

main ●────●────● (safe and sound)
↓
bad-idea ●───● ← Delete this, no harm done!

Real-World Benefits​

βœ… Isolation - Work on features without affecting others βœ… Collaboration - Multiple developers work simultaneously βœ… Experimentation - Try ideas risk-free βœ… Organization - Separate bugs, features, hotfixes βœ… Safety - Main branch stays stable βœ… Rollback - Abandon bad ideas easily

Creating and Switching Branches πŸš€β€‹

Modern way (Git 2.23+):

git switch -c feature-login

Traditional way (works everywhere):

git checkout -b feature-login

What happens:

Before:
main ●────●────● ← You are here
↑

After:
main ●────●────●
↓
feature-login ● ← You are now here (new branch)

Both commands:

  1. Create a new branch called feature-login
  2. Switch to it immediately
  3. Start from current commit

Method 2: Create Then Switch (Two Steps)​

Step 1: Create the branch

git branch feature-login

Step 2: Switch to it

git switch feature-login # Modern way
# or
git checkout feature-login # Traditional way

Visual:

Step 1:
main ●────●────● ← You are still here
↓
feature ● (branch created but you haven't moved)

Step 2:
main ●────●────●
↓
feature ● ← Now you moved here

Quick Reference: Branch Commands​

TaskModern CommandTraditional Command
Create & switchgit switch -c namegit checkout -b name
Switch onlygit switch namegit checkout name
Create onlygit branch namegit branch name
List branchesgit branchgit branch
Modern vs Traditional

Modern (git switch):

  • βœ… Clear and intuitive
  • βœ… Less error-prone
  • βœ… Available Git 2.23+

Traditional (git checkout):

  • βœ… Works everywhere
  • ⚠️ Does many things (confusing)
  • ⚠️ Easy to make mistakes

Recommendation: Use git switch if available!

Example Workflow: Creating a Login Feature​

# 1. Make sure you're on main branch
git switch main

# 2. Get latest changes
git pull origin main

# 3. Create new feature branch
git switch -c feature-login

# 4. Now you can work safely!
# Make changes to files...

# 5. Check what branch you're on
git branch
# Output:
# main
# * feature-login ← The * shows current branch

Visual Timeline:

main ●────●────● (pulled latest)
↓
└─● ← Created feature-login, started working

Merging Branches πŸ”€β€‹

Merging brings changes from one branch into another.

Basic Merge Workflow​

Step 1: Switch to destination branch

git switch main # Go to branch receiving changes

Step 2: Merge the feature branch

git merge feature-login

Visual:

Before merge:
main ●────●────●
↓
feature-login ●───●───● (3 new commits)

After merge:
main ●────●────●─────● (merged all changes)
β†—
feature-login ●───●───● (branch still exists)

Complete Example: Feature Development​

# You're on main branch
git switch main

# Get latest changes
git pull origin main

# Create feature branch
git switch -c feature-user-profile

# Work on feature
# ... make changes, commit ...
git add .
git commit -m "Add user profile page"

# More work...
git commit -m "Add profile edit functionality"

# Feature complete! Switch back to main
git switch main

# Merge feature into main
git merge feature-user-profile

# Push to remote
git push origin main

Timeline:

main ●────●─────────────● (merged feature)
↓ β†—
feature ●───●───●───● (2 commits)

Merge Conflicts (When Things Collide) βš οΈβ€‹

What is a conflict? When two branches modify the same part of a file differently.

Example scenario:

main Modified line 10 in app.js
↓
●
↓
feature-login Also modified line 10 in app.js
●

❌ Conflict! Git doesn't know which version to keep.

How Git shows conflicts:

// app.js
<<<<<<< HEAD (current branch)
const title = "Welcome to Dashboard";
=======
const title = "User Login Page";
>>>>>>> feature-login (incoming branch)

Resolving:

  1. Open the file
  2. Choose which version to keep (or combine them)
  3. Remove conflict markers (<<<<<<<, =======, >>>>>>>)
  4. Save the file
  5. Stage and commit
# After manually fixing conflicts
git add app.js
git commit -m "Resolve merge conflict in app.js"
Fast-Forward Merge

If main hasn't changed since you branched, Git does a fast-forward merge:

Before:
main ●────●
↓
feature ●───●───●

After (fast-forward):
main ●────●───●───● ← Just moves pointer forward!

No merge commit neededβ€”cleaner history! ✨

Deleting Branches πŸ—‘οΈβ€‹

Once merged, feature branches can be deleted to keep things tidy.

Safe Delete (Merged Only)​

git branch -d feature-login

What it does:

  • βœ… Deletes branch if already merged
  • ❌ Refuses if not merged (safety!)

Visual:

Before:
main ●────●────●─────● (feature merged)
β†—
feature-login ●───●───● ← Delete this

After:
main ●────●────●─────● (commits remain)
β†—
(branch deleted)

Force Delete (Caution!) βš οΈβ€‹

git branch -D feature-experiment

What it does:

  • 🚨 Deletes branch even if NOT merged
  • ⚠️ DANGER: Commits may be lost!

Use when:

  • Abandoning failed experiments
  • Removing mistake branches
  • You're absolutely sure!
Never Do This
# ❌ DON'T delete the current branch
git switch feature-login
git branch -d feature-login # ERROR!

# βœ… DO switch away first
git switch main
git branch -d feature-login # SUCCESS!

Delete Remote Branch​

# Delete local branch
git branch -d feature-login

# Delete from GitHub/remote
git push origin --delete feature-login

Visual:

Local: feature-login deleted βœ“
↕
Remote: feature-login deleted βœ“ (after push --delete)

Renaming Branches πŸ“β€‹

Rename Current Branch​

git branch -m new-name

Example:

# You're on branch: feature-login-page
git branch -m feature-auth-system

# Now branch is called: feature-auth-system

Rename Other Branch​

git branch -m old-name new-name

Example:

git branch -m bugfix-typo bugfix-spelling-errors

Visual:

Before:
main ●────●
↓
feature-login-page ●───●

After rename:
main ●────●
↓
feature-auth-system ●───● (same commits, new name!)
Why Rename?

Good reasons:

  • Scope changed during development
  • Found better, clearer name
  • Standardize naming convention
  • Fix typos

Remember: Branch names are just labels. Renaming is safe! 🏷️

Local vs Remote Branches πŸŒβ€‹

Two copies of your work exist:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Your Computer (Local) β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β”‚ main β”‚ ← Local branch β”‚
β”‚ β”‚ feature-auth β”‚ β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
↕ (push/pull)
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ GitHub (Remote) β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β”‚ origin/main β”‚ ← Remote branch β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Understanding the Names​

NameMeaningLocation
mainYour local main branchYour computer
origin/mainRemote main branchGitHub
feature-loginYour local featureYour computer
origin/feature-loginRemote featureGitHub

Key Point: origin = default name for remote repository (usually GitHub)

Pushing to Remote (Share Your Work) ⬆️​

First time pushing a new branch:

git push -u origin feature-login

What -u does:

  • Sets up tracking between local and remote branch
  • Future pushes can just use git push
  • Creates connection for easy sync

Visual:

Before push:
Local: main ●────●
↓
feature ●───●───● ← Only on your computer

Remote: main ●────● ← Team can't see your work

After push:
Local: feature ●───●───●
↕ (tracking)
Remote: origin/feature ●───●───● ← Now team can see!

Subsequent pushes:

git push # Simple! (tracking already set up)

Pulling from Remote (Get Others' Work) ⬇️​

git pull origin main

What happens:

  1. Fetch latest changes from GitHub
  2. Merge them into your current branch

Visual:

Remote: main ●────●────●────● (teammates pushed new commits)
↕
Local: main ●────● ← Your branch is behind

After pull:
Local: main ●────●────●────● ← Now up to date!

Fetch vs Pull: What's the Difference? πŸ€”β€‹

This confuses many beginners! Let's make it crystal clear.

The Key Difference​

git fetch = Download + Review (safe, no changes to your code)
git pull = Download + Merge (automatic, changes your code)

Git Fetch (Look Before You Leap) πŸ”β€‹

Downloads changes but doesn't apply them.

Fetch all branches from remote repository
git fetch origin

The command goes out to that remote project and pulls down all the data from that remote project that you don't have yet. After you do this, you should have references to all the branches from that remote, which you can merge in or inspect at any time.

If you clone a repository, the command automatically adds that remote repository under the name origin. So, git fetch origin fetches any new work that has been pushed to that server since you cloned (or last fetched from) it.

note

git fetch command only downloads the data to your local repository — it doesn't automatically merge it with any of your work or modify what you're currently working on. You have to merge it manually into your work when you're ready.

You can also fetch a specific branch:

Fetch a branch name new-branch from remote repository
git fetch origin new-branch

This will fetch the new-branch branch from the remote repository.

Once you have fetched a branch, and you want to merge it into your current branch, you can use the git merge command:

Merge a new-branch into current branch
git merge origin/new-branch

This will merge the new-branch branch into the current branch.

Pulling branches​

To pull branches, you can use the git pull command. This command takes one argument, which is the name of the remote repository.

Pull all branches from remote repository
git pull origin

This will pull all branches from the remote repository. You can also pull a specific branch using the -b flag:

Pull a branch name new-branch from remote repository
git pull origin new-branch

This will pull the new-branch branch from the remote repository.

git pull command to automatically fetch and then merge that remote branch into your current branch. This may be an easier or more comfortable workflow for you; and by default, the git clone command automatically sets up your local master branch to track the remote master branch (or whatever the default branch is called) on the server you cloned from. Running git pull generally fetches data from the server you originally cloned from and automatically tries to merge it into the code you're currently working on.

git fetch vs git pull​

git fetch​

The git fetch command will fetch all branches from a remote repository. But it will not merge these branches into your local branches.

To understand it better, let's imagine that you have a local branch called main and a remote branch called main. If you run git fetch origin, it will fetch the main branch from the remote repository. However, it will not merge this branch into your local main branch. Your local branch won't have the latest changes that remote branch have, unless You merge this branch manually by git merge origin/main

Merging branch means that you are merging the changes from the remote branch into your local branch. This means that your local branch will be updated with the changes from the remote branch.

git pull​

The git pull command will fetch all branches from a remote repository and merge them into your local branches. This means that your local branches will be updated with the changes from the remote branches.

So, git pull origin main = git fetch origin/main + git merge origin/main

origin/branch-name vs branch-name​

origin/branch-name is the remote branch

branch-name is the local branch

Both branches can have different commits. When we run git fetch, it will fetch the latest commits from the remote repository, and put then in origin/branch-name. But it will not update the local branch.

Different teams use different branching strategies. Here are the most popular:

Quick Comparison​

WorkflowBest ForComplexityRelease Frequency
GitHub FlowSmall teams, continuous deployment⭐ SimpleVery frequent
Git FlowScheduled releases, larger teams⭐⭐⭐ ComplexPeriodic releases
Trunk-BasedAdvanced teams, CI/CD⭐⭐ MediumContinuous
GitLab FlowProduction + staging envs⭐⭐ MediumFrequent
Feature BranchAny team size⭐ SimpleFlexible

Learn More​

Choosing a Workflow

Start simple: If you're new, use GitHub Flow or Feature Branch Workflow.

Need structure: Use Git Flow for scheduled releases.

Advanced CI/CD: Consider Trunk-Based Development.

No perfect choice: Workflows can be adapted! Pick one and adjust as needed.

Branch Naming Conventions πŸ“›β€‹

Good branch names help teams understand what everyone is working on at a glance!

Why Naming Matters​

❌ Bad: stuff, test, asdf, new-branch
βœ… Good: feature/user-login, bugfix/cart-crash, hotfix/security-patch

Benefits of good names:

  • Team knows what you're working on
  • Easy to find specific branches
  • Clear project organization
  • Better collaboration

The 6 Golden Rules​

Follow these guidelines for professional branch naming:

1. Short and Descriptive βœοΈβ€‹

Balance brevity with clarity.

❌ Too vague: fix, update, new
βœ… Just right: bugfix/login-error, feature/dark-mode
❌ Too long: feature/add-new-user-authentication-system-with-oauth2

Examples:

  • feature/login-form βœ…
  • bugfix/cart-crash βœ…
  • hotfix/security-patch βœ…

2. Use Lowercase πŸ”‘β€‹

Easier to type and read.

❌ Wrong: Feature/UserLogin, BugFix/cart-crash
βœ… Right: feature/user-login, bugfix/cart-crash

3. Hyphen-Separated (kebab-case) πŸ”—β€‹

Use hyphens instead of spaces or underscores.

❌ Wrong: feature/userlogin, feature/user_login, feature/userLogin
βœ… Right: feature/user-login

More examples:

  • prepare-release-v2 βœ…
  • issue-426-fix βœ…
  • ui-redesign βœ…

4. Prefix with Type πŸ·οΈβ€‹

Quickly identify branch purpose!

Common Prefixes:

PrefixPurposeExample
main / masterProduction codemain
developDevelopment integrationdevelop
feature/New featuresfeature/shopping-cart
bugfix/Bug fixesbugfix/login-error
hotfix/Urgent production fixeshotfix/security-issue
release/Release preparationrelease/v2.1.0
wip/Work in progresswip/experimental-ui

Real Examples:

feature/user-authentication
bugfix/payment-validation
hotfix/critical-crash
release/v1.5.0
wip/new-algorithm

Combining Prefixes:

feature/issue-123/user-profile
hotfix/bug-456/memory-leak

List branches by type:

git branch --list 'feature/*'

Output:

feature/login
feature/cart
feature/checkout

5. Be Consistent πŸŽ―β€‹

Team-wide standards matter!

βœ… Team uses feature/ β†’ Everyone uses feature/
βœ… Team skips prefixes β†’ Nobody uses prefixes
❌ Mixed approach β†’ Confusion!

Pick ONE convention and stick to it!

6. Don't Fear Renaming πŸ”„β€‹

Branch names are just labelsβ€”renaming is safe!

Rename current branch:

git branch -m better-name

Rename any branch:

git branch -m old-name new-name

When to rename:

  • Found a clearer name
  • Feature scope changed
  • Fixed a typo
  • Standardizing conventions

Key Takeaways πŸŽ―β€‹

Branching empowers you to: βœ… Experiment safely without breaking working code βœ… Work on multiple features simultaneously βœ… Collaborate with teams without conflicts βœ… Organize different types of work (features, bugs, hotfixes) βœ… Keep production code stable

Remember:

  • Create branches for each feature/fix
  • Use clear, descriptive names
  • Merge when ready, delete when done
  • Fetch to review, pull to update
  • Local and remote branches are separate (until you push!)

Master these commands:

git switch -c feature-name # Create and switch
git merge feature-name # Merge changes
git branch -d feature-name # Delete merged branch
git fetch origin # Download changes (safe)
git pull origin main # Download + merge

You're now ready to:

  • Create feature branches
  • Merge changes safely
  • Collaborate with teams
  • Use professional naming conventions
  • Choose a branching workflow

Keep practicing, and branching will become second nature! πŸš€