Skip
Arish's avatar

13. What is Branching?


What is Branching?

Branching is one of Git's most powerful features. A branch allows you to diverge from the main line of development and work independently without affecting other branches.

Why Use Branches?

1. Parallel Development

Work on multiple features simultaneously:

main ────●────●────●────●────● │ feature-a └──●──●──● │ feature-b └──●──●

2. Isolated Changes

Keep experimental work separate from stable code:

bash
1# Create a branch for experiments
2git checkout -b experiment
3
4# Try risky changes safely
5# If it works: merge
6# If it fails: delete the branch

3. Team Collaboration

Each team member works on their own branch:

main ────●────●────●────● │ alice-feature └──●──●──● │ bob-bugfix └──●──●

How Branches Work

The Pointer Concept

A branch is simply a pointer to a commit:

HEAD ↓ main ──●──●──● ↑ branch pointer

Creating a branch just creates a new pointer:

main ──●──●──● ↑ feature (new branch)

HEAD Pointer

HEAD points to your current branch:

bash
1# Check where HEAD points
2cat .git/HEAD
3# ref: refs/heads/main

Branch Naming Conventions

Common Patterns

bash
1# Feature branches
2feature/user-authentication
3feature/shopping-cart
4feature/payment-integration
5
6# Bug fix branches
7bugfix/login-error
8fix/header-alignment
9
10# Hotfix branches (urgent fixes)
11hotfix/security-patch
12
13# Release branches
14release/v1.0.0
15release/2024-01

Best Practices

  1. Use lowercase - feature/login not Feature/Login
  2. Use hyphens - feature-name not feature_name
  3. Be descriptive - fix/user-login-timeout not fix/bug1
  4. Include ticket numbers - feature/JIRA-123-user-profile

Common Branching Strategies

1. Git Flow

main ─────●─────────────────●───────● \ / / develop ────●────●────●───●────●───● \ / feature ●────●
  • main - Production code
  • develop - Integration branch
  • feature/* - New features
  • release/* - Release preparation
  • hotfix/* - Emergency fixes

2. GitHub Flow

main ────●────●────●────●────● \ / feature ●──●──●

Simpler workflow:

  1. Branch from main
  2. Make changes
  3. Open Pull Request
  4. Merge to main

3. Trunk-Based Development

main ────●────●────●────●────● \ / feature ●──● (short-lived)
  • Short-lived feature branches
  • Merge frequently
  • Feature flags for incomplete work

Visualizing Branches

Command Line

bash
1# Simple log with branches
2git log --oneline --graph --all
3
4# Example output:
5* abc1234 (HEAD -> main) Latest commit
6| * def5678 (feature) Feature work
7|/
8* 789abcd Previous commit

GUI Tools

  • GitKraken - Visual branching
  • SourceTree - Free Git GUI
  • VS Code - Built-in Git graph extensions

Summary

Branches in Git:

  • ✅ Allow parallel development
  • ✅ Keep changes isolated
  • ✅ Enable team collaboration
  • ✅ Are just pointers (lightweight)
  • ✅ Can be created/deleted easily

Next, let's learn how to create and switch between branches!