Git Branching - The Ultimate Guide for Beginners & Pros!

Master Git branching! Create, switch, delete, and work with branches! Git's most powerful feature!

Introduction

Branching is Git's superpower! Let's master it!

What You Will Learn

  • What is a Git branch
  • Creating, listing, switching branches
  • Deleting branches
  • Branch naming conventions
  • Feature branch workflow

Prerequisites

What is a Branch?

A branch in Git is just a lightweight movable pointer to a commit! The default branch is usually main!

Think of branches as separate workspaces! You can experiment without affecting the main code!

graph LR
    C1 -- main --> C2
    C2 -- main --> C3
    C2 -- feature-login --> C4
    C4 -- feature-login --> C5

Git Branch Commands!

List Branches

# List local branches
git branch

# List all branches (local + remote)
git branch -a

Create a Branch

# Create a new branch (stay on current branch)
git branch feature-login

# Create AND switch to new branch (RECOMMENDED!)
git switch -c feature-login
# OR (old way)
git checkout -b feature-login

Switch Branches

# Switch to main branch
git switch main

# OR (old way)
git checkout main

Delete a Branch

# Delete a local branch that's been merged
git branch -d feature-login

# Force delete (DANGEROUS!)
git branch -D feature-login

The Feature Branch Workflow!

The most common Git workflow!

  1. Create a new branch from main for each feature/bugfix!
  2. Work on the branch!
  3. Commit your changes!
  4. Push to GitHub!
  5. Open a Pull Request!
  6. Merge!
  7. Delete the branch!

Branch Naming Conventions!

Use good, consistent names!

  • Features: feature/user-authentication, feature/checkout
  • Bugfixes: bugfix/login-error, bugfix/header-layout
  • Hotfixes: hotfix/critical-bug-prod
  • Releases: release/v1.0, release/v1.1

Best Practices!

  1. Branch often: Small branches = easier merges!
  2. Keep branches focused: One branch = one feature/fix!
  3. Delete merged branches: Keep your repo clean!
  4. Pull main often: Avoid big merges later!

Summary

Git branching is a game-changer! Use branches to experiment safely!

Related Articles

Previous Tutorial

Commits & History

Next Tutorial

Let's learn merging! → Merging & Merge Conflicts