Branches & Merging
What is a Branch?
A branch is a parallel timeline of your project. The default branch is called main (or master in older repos). When you create a new branch, you get an independent copy of the history where you can make changes without affecting main.
╲
feature D ─── E
Branch Commands
You only need a handful of commands to work with branches effectively. Here are all the essential ones:
# List all local branches (* = current branch) git branch # List all branches including remote git branch -a # Create a new branch git branch feat/user-auth # Switch to a branch (modern syntax) git switch feat/user-auth # Old syntax (still works) git checkout feat/user-auth # CREATE and switch in one command git checkout -b feat/payment-flow # Or modern: git switch -c feat/payment-flow # Delete a merged branch git branch -d feat/user-auth # Force delete (even if unmerged) git branch -D feat/user-auth # Rename current branch git branch -m new-name
feat/ — new features (feat/user-login)fix/ — bug fixes (fix/null-pointer)hotfix/ — urgent production fixes (hotfix/security-patch)chore/ — maintenance (chore/update-deps)docs/ — documentation only (docs/api-guide)Merging Branches
When your feature is complete and tested, you merge it back into main. Git performs the merge automatically in most cases.
# 1. Switch to the branch you want to merge INTO git switch main # 2. Merge the feature branch into main git merge feat/user-auth # 3. Delete the feature branch (optional, keeps things clean) git branch -d feat/user-auth
Fast-forward vs 3-way Merge
Git uses different strategies depending on whether the base branch has diverged from the feature branch:
main has NOT moved since the branch was created. Git simply moves the main pointer forward. No merge commit created. Cleanest history.(branch was at B, main also at B)
main has moved forward since branching. Git creates a merge commit combining both timelines. Shows the true history of parallel work.feat: ╲─ D ─ E ─╯
Merge Conflicts: What They Are & How to Resolve Them
A merge conflict happens when both branches modified the same lines of the same file. Git can't decide which version to keep — it asks you to decide manually.
# Git puts conflict markers in the file: <<<<<<< HEAD const greeting = "Hello"; # Your version (main) ======= const greeting = "Hi there"; # Their version (feature branch) >>>>>>> feat/update-greeting # TO RESOLVE: # 1. Delete ALL conflict markers (<<<, ===, >>>) # 2. Keep whichever version you want (or combine them) # 3. Save the file # After resolving all conflicts: git add . git commit -m "merge: resolve conflict in greeting message"
Using VS Code to Resolve Conflicts
VS Code has built-in merge conflict resolution UI. When you open a conflicted file, it shows buttons: "Accept Current Change", "Accept Incoming Change", "Accept Both Changes". Click the one you want and save. Much easier than manual editing.
git stash — Save Work in Progress
Sometimes you're in the middle of work but need to switch branches urgently (to fix a bug, help a colleague). git stash temporarily shelves your changes without committing.
# Stash current uncommitted changes git stash # Stash with a description git stash push -m "WIP: half-done login form" # List all stashes git stash list # Apply most recent stash (keeps it in list) git stash apply # Apply AND remove from list git stash pop # Apply a specific stash git stash apply stash@{2} # Delete a stash git stash drop stash@{0}
git stash do?Using the repo from Lesson 3 (or a new one):
1. Create a branch:
git checkout -b feat/add-bio2. Edit
README.md — change the first line to "# My Amazing Project"3. Commit:
feat: update project title in README4. Switch back to main:
git switch main5. Edit the same first line of README.md differently — "# My Awesome Project"
6. Commit on main:
docs: rename project in README7. Merge the feature branch:
git merge feat/add-bio8. Resolve the conflict, stage it, and commit the merge
9. Run
git log --oneline --graph to see the branching history
💡 Show hints
- Conflicts appear as
<<<<<<< HEAD...=======...>>>>>>>in the file - Delete all conflict markers and keep the text you want
- After editing:
git add README.mdthengit commit - VS Code shows conflict resolution buttons — much easier than manual editing