Branching — Create, Switch, Merge
BeginnerBranches are free in Git — creating one takes microseconds. They let you isolate work (features, fixes, experiments) from the main codebase and merge back when ready.
Overview
A branch in Git is simply a lightweight movable pointer to a commit — just a 41-byte file on disk. This is why Git branching is instant compared to older VCS tools that copied entire directory trees. The recommended workflow is to create a branch for every piece of work (feature, bugfix, experiment), work on it in isolation, then merge or rebase it back to main. Good branch hygiene — short-lived branches, descriptive names, regular merges from main — prevents the painful "integration hell" that results from branches diverging for weeks.
Creating and Switching Branches
Branch names should be descriptive and use kebab-case. Common prefixes: feature/, fix/, hotfix/, chore/, docs/. Keep branches short-lived — the longer a branch diverges, the harder the merge.
# List all branches (* = current)
git branch
# * main
# feature/user-auth
# Create a branch and switch to it (preferred modern syntax)
git switch -c feature/payment-gateway
# Older syntax (still works everywhere)
git checkout -b feature/payment-gateway
# Switch between branches
git switch main
git switch feature/payment-gateway
# Create branch from a specific commit or tag
git switch -c hotfix/login-crash origin/main
git switch -c release/v2.0 v1.9.0
# Delete a branch (safe — checks for unmerged work)
git branch -d feature/payment-gateway
git branch -D feature/payment-gateway # force deleteFast-Forward vs Three-Way Merge
When you merge, Git first checks if the merge can be a fast-forward: if the branch being merged into has not moved since the feature branch was created, Git simply moves the pointer forward — no merge commit needed. If both branches have new commits, Git performs a three-way merge using both branch tips and their common ancestor, creating a merge commit.
// FAST-FORWARD MERGE (no divergence)
//
// Before:
// main: A ── B
// â””── C ── D ↠feature
//
// git switch main && git merge feature
//
// After (pointer just moves forward, no merge commit):
// main: A ── B ── C ── D ↠main & feature
// THREE-WAY MERGE (both branches have new commits)
//
// Before:
// main: A ── B ── E ↠main moved forward
// â””── C ── D ↠feature
//
// After:
// main: A ── B ── E ── M ↠merge commit M
// â””── C ── D ─┘
# Perform the merge
git switch main
git merge feature/payment-gateway
# Prevent fast-forward (always create a merge commit)
git merge --no-ff feature/payment-gateway
# Merge commit message appears in editor — describe the integrationResolving Merge Conflicts
Conflicts happen when two branches changed the same lines differently. Git marks the conflict in the file with <<<<<<, =======, and >>>>>>> markers. You resolve by editing the file to the desired final state, then staging and committing.
# Git marks conflicts like this:
<<<<<<< HEAD (your branch — main)
function login(email, password) {
if (!email || !password) throw new Error('Missing credentials')
=======
function login(email, password) {
validateInput(email, password) // refactored validation
>>>>>>> feature/payment-gateway
# 1. Edit the file to the correct final state:
function login(email, password) {
if (!email || !password) throw new Error('Missing credentials')
validateInput(email, password)
}
# 2. Stage the resolved file
git add src/auth.js
# 3. Complete the merge
git commit # Git pre-fills the merge commit message
# Abort a merge if you need to start over
git merge --abort
# Use a visual merge tool
git mergetool # opens configured tool (VSCode, vimdiff, etc.)Key Points to Remember
- 1A Git branch is a 41-byte file — creating, switching, and deleting branches is instant
- 2Fast-forward merge: no divergence, Git moves the pointer — clean linear history
- 3Three-way merge: both branches moved, Git creates a merge commit using the common ancestor
- 4Use git switch -c <name> (modern) or git checkout -b <name> (classic) to create and switch
- 5git merge --abort cancels an in-progress merge and returns to the pre-merge state
Interview Questions
Sign in to ask AriaWhat is the difference between a fast-forward and a three-way merge?
How do you resolve a merge conflict in Git?
What does --no-ff do in git merge and when would you use it?
Ask Aria about Branching — Create, Switch, Merge
Your personal AI tutor — ask anything about this concept
Revision Status
Personal Notes
Sign in to save personal notes for this topic.
Discussion
Sign in to join the discussion.