← Back to Chapters

GitHub Branch

? GitHub Branch

? Quick Overview

Branching in GitHub allows developers to work on different features, fixes, or experiments independently without affecting the main codebase.

? Key Concepts

  • Branches represent independent lines of development
  • Main branch stays stable
  • Features and fixes live in separate branches

? Syntax / Theory

A branch can be created locally using Git or directly from GitHub. Once work is complete, branches are merged back using pull requests.

? Code Examples

? Create Branch Locally
// Create and switch to a new branch
git checkout -b feature-login
? Push Branch to GitHub
// Push local branch to remote repository
git push -u origin feature-login
? Switch Branch
// Switch to an existing branch
git checkout main
? View All Branches
// List local and remote branches
git branch -a
? Delete Branch
// Delete branch after merge
git branch -d feature-login

? Live Output / Explanation

Each command updates Git’s internal references. Branch creation does not duplicate files, it only creates pointers.

? Interactive Example

Imagine branches as parallel timelines where changes happen independently and later merge back.

?️ Use Cases

  • Feature development
  • Bug fixing
  • Experimentation
  • Team collaboration

? Tips & Best Practices

  • Use meaningful branch names
  • Keep branches small and focused
  • Delete merged branches
  • Sync frequently with main

? Try It Yourself

  • Create a branch
  • Make changes
  • Push and create PR
  • Merge and delete branch