Git Branching

1/9/2025

Git Branching

Branches allow you to develop features independently from the main codebase.

Branch Commands

bash
# List branches
git branch

# Create a branch
git branch feature-login

# Switch to branch
git checkout feature-login
# or (Git 2.23+)
git switch feature-login

# Create and switch
git checkout -b feature-login
# or
git switch -c feature-login

# Delete branch
git branch -d feature-login

Merging

bash
# Switch to main branch
git checkout main

# Merge feature branch
git merge feature-login

Merge Conflicts

bash
# When conflicts occur
git merge feature-branch
# CONFLICT in file.txt

# Edit file to resolve
# Look for:
<<<<<<< HEAD
Your changes
=======
Their changes
>>>>>>> feature-branch

# After resolving
git add file.txt
git commit -m "Resolve merge conflict"

Rebasing

bash
# Rebase current branch onto main
git rebase main

# Interactive rebase
git rebase -i HEAD~3