DevOps
Git
Branching & Switching

Branching Basics

Branching is Git's most powerful feature. It allows you to create a separate workspace to build features or fix bugs without affecting the stable "Main" project.

1. What is a Branch?

Think of a branch as a lightweight pointer to a specific commit. Creating a branch doesn't copy all your files; it just creates a new pointer that moves forward as you make new commits.


2. Core Branching Commands

The industry is moving from the older checkout command to the modern, more intuitive switch command.

ActionClassic CommandModern Command (Recommended)
List Branchesgit branchgit branch
Create Branchgit branch [name]git branch [name]
Switch Branchgit checkout [name]git switch [name]
Create & Switchgit checkout -b [name]git switch -c [name]

3. Merging: Combining Work

Once your feature is complete, you bring it back to the main path using a Merge.


4. Deleting Branches

To keep your workspace clean, delete branches after they have been merged.

# Safe Delete (Will error if not merged)
git branch -d feature-name
 
# Force Delete (Risky!)
git branch -D accidental-branch

[!TIP] Why use switch? The git checkout command was "overloaded"—it did too many things (restored files, switched branches, created branches). Git introduced switch and restore to make these operations clearer and less prone to accidental errors.