Branching and Merging

Branches let you work on features, fixes, or experiments in isolation.

Creating and switching branches

bash
# Create a new branch
git branch feature/login

# Switch to it
git checkout feature/login

# Or do both at once
git checkout -b feature/login

Merging branches

When your feature is ready, merge it back into main:

bash
# Switch to main
git checkout main

# Merge the feature branch
git merge feature/login

Handling merge conflicts

When two branches change the same line, Git can't auto-merge:

code
<<<<<<< HEAD
const port = 3000;
=======
const port = 8080;
>>>>>>> feature/login

To resolve: edit the file, choose the correct version, remove the markers, then:

bash
git add .
git commit -m "Resolve merge conflict in config"

Branch strategies

Feature branches — One branch per feature (feature/auth, feature/search)

Git Flowmain, develop, feature/*, release/*, hotfix/*

Trunk-based — Short-lived branches merged quickly to main

For small teams, simple feature branches are usually enough.