G
GuideDevOps
Lesson 4 of 18

Branching & Merging

Part of the Git & Version Control tutorial series.

Understanding Branches

A branch is a lightweight pointer to a commit. At any time, one branch is "active" (HEAD).

Main Branch

Modern Git uses main (previously master) as the default primary branch:

git branch -a
# Shows all branches, active one marked with *

Creating Branches

Create New Branch

# Create branch
git branch feature/user-auth
 
# Create and switch
git checkout -b feature/user-auth
 
# Newer syntax
git switch -c feature/user-auth

Switching Branches

# Switch to existing branch
git checkout main
 
# Newer syntax
git switch main
 
# Create and switch in one command
git switch -c feature/new-api

Branch Naming Conventions

Good naming helps organize work:

feature/user-authentication    # New feature
bugfix/login-redirect-loop     # Bug fix
hotfix/security-patch          # Production emergency
docs/api-reference             # Documentation
refactor/database-queries      # Refactoring

Listing Branches

# Local branches
git branch
 
# All branches (local + remote)
git branch -a
 
# Remote branches
git branch -r
 
# Delete branch information
git branch -d feature/old-branch
 
# Force delete
git branch -D feature/unmerged-branch

Tracking Branches

When you clone a repository, your local main automatically tracks origin/main.

# Set upstream for your branch
git push -u origin feature/auth
 
# Check tracking
git branch -vv
# Shows which remote branch each local branch tracks
 
# View current tracking
git status

Merging Branches

Fast-Forward Merge

When the target branch hasn't changed since you branched off:

git checkout main
git merge feature/quick-fix

Three-Way Merge

When both branches have changes. Creates a merge commit:

git checkout main
git merge feature/long-feature

Both strategies happen automatically depending on the history.

Merge Options

# No fast-forward (always creates merge commit)
git merge --no-ff feature/auth
 
# Squash commits into one
git merge --squash feature/auth
git commit -m "Add authentication system"
 
# Rebase instead of merge (rewrites history)
git merge --rebase feature/auth

Common Merge Scenarios

Feature Complete - Ready to Merge

# Ensure main has latest changes
git checkout main
git pull
 
# Merge feature branch
git merge feature/payment-gateway
 
# Delete feature branch
git branch -d feature/payment-gateway
 
# Clean up remote
git push origin --delete feature/payment-gateway

Multiple Developers

# Developer A: working on feature
git checkout -b feature/search
# ... commits ...
git push -u origin feature/search
 
# Developer B: working on different feature
git checkout -b feature/filters
# ... commits ...
git push -u origin feature/filters
 
# Later, merge both
git checkout main
git pull
git merge feature/search
git merge feature/filters
git push

Comparing Branches

# See what's in feature branch but not main
git diff main feature/auth
 
# See commits not yet merged
git log main..feature/auth
 
# See commits merged in main but not feature
git log feature/auth..main

Handling Stale Branches

# Update local tracking of remote branches
git fetch
 
# Prune deleted remote branches from local
git fetch --prune
 
# See which branches are merged and can be deleted
git branch --merged
 
# Delete merged branches
git branch -d feature/done feature/complete

Best Practices

✓ Create branches for features, not main development
✓ Name branches descriptively
✓ Keep branches focused on one feature/fix
✓ Delete merged branches to keep repo clean
✓ Pull latest changes before merging
✓ Review changes before merging
✓ Use pull requests for team collaboration