Git
Branching & Merging
Branches let you work on features in isolation without affecting the main codebase. You create a branch, make changes, then merge it back when ready. This is the heart of collaborative Git workflows.
Working with branches
# Create and switch to a new branch
git checkout -b feature/login
# (modern equivalent)
git switch -c feature/login
# List branches
git branch
# Switch between branches
git switch main
# Delete a branch after merging
git branch -d feature/login
Merging
Bring a feature branch's changes into your current branch:
git switch main
git merge feature/login
If both branches changed the same lines, you get a merge conflict that you resolve manually.
Resolving merge conflicts
Git marks conflicts in the file like this:
<<<<<<< HEAD
const title = "Home";
=======
const title = "Dashboard";
>>>>>>> feature/login
Edit the file to keep what you want, remove the <<<, ===, >>> markers, then:
git add conflicted-file.js
git commit
Merge vs Rebase
Two ways to integrate changes:
- Merge creates a merge commit, preserving the branching history.
- Rebase replays your commits on top of the target branch for a linear history.
git rebase main # replay current branch on top of main
💡
Golden rule: never rebase commits that you've already pushed and others may have pulled — it rewrites history and causes conflicts for your teammates.
Watch & Learn
A recommended video to watch alongside this chapter.
More “Branching & Merging” videos on YouTube