Git Branching & Workflow

Difficulty: Beginner

Question

Explain merge vs rebase. What branching strategy do you prefer and why?

Answer

Merge creates a merge commit that combines two branches. History shows exactly when branches diverged and merged.

Rebase replays commits from one branch onto another. Creates a linear history but rewrites commit hashes.

Golden rule: Never rebase commits that have been pushed and shared with others.

Branching strategies: 1. Git Flow: main + develop + feature/release/hotfix branches. Good for scheduled releases. 2. Trunk-Based: Short-lived feature branches merged to main. Good for CI/CD. 3. GitHub Flow: Simple - main + feature branches with PRs. Good for most teams.

Code examples

Merge vs Rebase

# Merge: preserves history, creates merge commit
git checkout main
git merge feature/login
# History:   main ──●──●──●──M──
#                    \      /
#           feature   ●──●──

# Rebase: linear history, rewrites commits
git checkout feature/login
git rebase main
git checkout main
git merge feature/login  # fast-forward
# History:   main ──●──●──●──●──●──
#                            (feature commits replayed)

# Interactive rebase: clean up before merge
git rebase -i main
# pick abc123 Add login form
# squash def456 Fix typo in login
# squash ghi789 Fix another typo
# Result: 3 commits squashed into 1 clean commit

# GOLDEN RULE:
# NEVER rebase commits that others have pulled
# Only rebase YOUR local commits before pushing

Use merge for shared branches (preserves history). Use rebase for local cleanup before pushing (clean, linear history).

Common Git Operations

# Undo last commit (keep changes)
git reset --soft HEAD~1

# Undo last commit (discard changes)
git reset --hard HEAD~1

# Undo a specific commit (creates new commit)
git revert abc123

# Stash changes temporarily
git stash
git stash pop  # apply and remove
git stash list # see all stashes

# Cherry-pick: apply specific commit to another branch
git checkout main
git cherry-pick abc123  # apply just that commit

# Find which commit introduced a bug
git bisect start
git bisect bad          # current commit is bad
git bisect good abc123  # this commit was good
# Git binary-searches between good and bad

# See who changed each line
git blame src/app.js

# Search commit messages
git log --grep="fix login" --oneline

Know these operations by heart. git reset --soft (undo commit, keep staged), git revert (safe undo for shared branches), git bisect (find bugs).

GitHub Flow Workflow

# 1. Create feature branch from main
git checkout main
git pull origin main
git checkout -b feature/user-auth

# 2. Make commits (small, focused)
git add src/auth/
git commit -m "Add login form component"
git add src/api/
git commit -m "Add auth API endpoints"

# 3. Push and create PR
git push -u origin feature/user-auth
# Create PR on GitHub
# Add description, screenshots, test instructions

# 4. Code review
# Reviewers comment, request changes
# Push fixes to same branch
git add .
git commit -m "Address review feedback"
git push

# 5. Merge PR (squash merge recommended)
# On GitHub: Squash and merge
# Deletes feature branch

# 6. Pull latest main
git checkout main
git pull origin main

GitHub Flow is simple and works for most teams: main is always deployable, feature branches are short-lived, PRs are required for all changes.

Key points

Concepts covered

Branching, Merge vs Rebase, Git Flow, Trunk-Based, Cherry Pick