← Back to Chapters

Git Branch

? Git Branch

? Quick Overview

Git Branching allows developers to work on multiple features, bug fixes, or experiments in parallel without disturbing the main codebase.

? Key Concepts

  • Branches are pointers to commits
  • Main (or master) is the default branch
  • Branches isolate work safely

? What is a Git Branch?

A Git branch is a lightweight pointer to a commit. Branches allow independent development and easy integration.

? Viewing Branches

? View Code Example
# Lists all local branches
git branch

? Creating a New Branch

? View Code Example
# Creates a new branch
git branch feature-xyz

? Switching Branches

? View Code Example
# Switches to an existing branch
git checkout feature-xyz

⚡ Create & Switch Together

? View Code Example
# Creates and switches to a new branch
git checkout -b feature-xyz

? Merging Branches

? View Code Example
# Merge feature branch into main
git checkout main
git merge feature-xyz

?️ Deleting a Branch

? View Code Example
# Deletes a merged branch
git branch -d feature-xyz

? Remote Branches

? View Code Example
# Lists remote branches
git branch -r

? Tips & Best Practices

  • Create one branch per feature
  • Merge frequently
  • Delete unused branches

? Try It Yourself

  • Create and switch branches
  • Make commits
  • Merge and delete branches