Git
01 / 06

Branching & Merging

Git Branching & Merging

Branching is one of Git's most powerful features. It enables parallel development, feature isolation, and safe experimentation. Understanding merge strategies and rebase is critical for team workflows.

Branch Operations

# Create a branch
git branch feature-login
git checkout -b feature-login       # Create and switch (older syntax)
git switch -c feature-login          # Create and switch (modern syntax)
git switch -c feature-login main     # Branch off from main explicitly

# List branches
git branch                   # Local branches
git branch -a                # All branches (local + remote)
git branch -r                # Remote-tracking branches only
git branch -v                # Show last commit on each branch
git branch --merged          # Branches already merged into current
git branch --no-merged       # Branches not yet merged

# Switch branches
git checkout main
git switch main              # Modern syntax
git switch -                 # Switch to previously checked-out branch

# Rename a branch
git branch -m old-name new-name
git branch -M main           # Force rename to main (even if branch exists)

# Delete branches
git branch -d feature-login          # Safe delete (only if merged)
git branch -D feature-login          # Force delete (even if unmerged)
git push origin --delete feature-login  # Delete remote branch

Merging

Merging integrates changes from one branch into another. Git supports several merge strategies - fast-forward, recursive, and squash merges each have different use cases.

# Standard merge (creates merge commit if needed)
git checkout main
git merge feature-login

# Fast-forward only (fails if a merge commit would be needed)
git merge --ff-only feature-login

# No-fast-forward (always create merge commit, preserves branch history)
git merge --no-ff feature-login

# Squash merge (combine all feature branch commits into one staged change)
git merge --squash feature-login
git commit -m "Add login feature"   # Then commit the squashed result

# Abort a merge with conflicts
git merge --abort

# Resolving conflicts manually:
# 1. Edit conflicting files (look for <<<<<<, =======, >>>>>>>)
# 2. git add <resolved-files>
# 3. git commit

# Use a merge tool
git mergetool

Rebase

Rebase rewrites commit history by replaying commits on top of another base. It produces a cleaner, linear history but should never be used on shared/public branches.

# Rebase current branch onto main
git checkout feature-login
git rebase main

# Interactive rebase - edit, reorder, squash commits
git rebase -i HEAD~3          # Rebase last 3 commits
git rebase -i main            # Interactive rebase onto main

# In interactive rebase, commands:
# pick   - use commit as-is
# reword - use commit but edit message
# squash - meld into previous commit
# fixup  - like squash but discard commit message
# drop   - remove commit entirely

# Abort or continue rebase
git rebase --abort
git rebase --continue          # After resolving conflicts
git rebase --skip              # Skip current commit

# Cherry-pick - apply specific commits to current branch
git cherry-pick abc123
git cherry-pick abc123 def456  # Multiple commits
git cherry-pick main~2         # Relative reference
git cherry-pick --no-commit abc123  # Apply without committing

Keep your own version of these notes — editable, searchable, and organised by your stack.

Start free