Branching

What is a Branch?

A branch is a separate line of development. It allows you to work on features or fixes without affecting the main codebase.

Creating Branches

# Create and switch to new branch
git checkout -b feature-branch

# Create branch without switching
git branch feature-branch

# Switch to existing branch
git checkout feature-branch

Listing Branches

# List local branches
git branch

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

Switching Between Branches

git checkout main
git checkout feature-branch

Merging Branches

# Merge feature-branch into current branch
git merge feature-branch

# Merge with a commit message
git merge feature-branch -m "Merge feature-branch"

Deleting Branches

# Delete merged branch
git branch -d feature-branch

# Force delete unmerged branch
git branch -D feature-branch

Branch Naming Conventions

Checking Out Remote Branches

# List remote branches
git branch -r

# Checkout remote branch
git checkout -b local-branch origin/remote-branch
Loading