Branching & Merging
Work on features in isolation with branches, then integrate them with merges.
A branch is just a movable pointer to a commit. Creating one is cheap, so the
standard workflow is: branch off main, build a feature in isolation, then
merge it back. This keeps main stable and lets several efforts proceed in
parallel.
Merging combines histories. A fast-forward merge simply moves the pointer forward when there’s no divergence. A three-way merge creates a new merge commit when both branches have new work. A conflict happens when the same lines changed on both sides — Git pauses and asks you to decide.
gitGraph commit branch feature checkout feature commit commit checkout main merge feature
git switch -c feature/login # create + switch to a branch
# ...make commits...
git switch main # back to main
git merge feature/login # integrate the feature
git branch -d feature/login # delete the merged branch
# On a conflict:
git status # see conflicted files
# edit files, remove <<<< ==== >>>> markers
git add <file> && git commit # finish the merge - Create a branch, make two commits, and merge it back into main.
- Deliberately cause a conflict by editing the same line on two branches; resolve it.
- Use
git log --oneline --graph --allto visualize your branch structure. - Explain when Git can fast-forward instead of creating a merge commit.
Cheat Sheet▾
| Task | Command |
|---|---|
| New branch | git switch -c name |
| Switch | git switch name |
| List branches | git branch |
| Merge into current | git merge name |
| Delete merged | git branch -d name |
| Visualize | git log --graph --oneline --all |
| Abort a merge | git merge --abort |
Common Interview Questions▾
What is a fast-forward merge?
When the target branch has no new commits of its own, Git just moves its pointer forward to the feature branch’s tip — no merge commit is needed.
Why use feature branches?
They isolate work in progress so main stays releasable, enable parallel development, and give a natural unit for code review via pull requests.
How do you resolve a merge conflict?
Edit the marked regions to the desired result, remove the conflict markers, git add the files, and commit to complete the merge.