Branches, merge and rebase
Working in parallel, merge vs rebase, fast-forward and conflicts.
Updated
What a branch is
A branch is a parallel line of development. You start from main (the stable code), work on a feature on your branch, and main stays untouched until you decide to integrate.
Technically, a branch is just a pointer to a commit — that's why creating one is instant.
mainABCfeat/loginDECommands
| Command | What it does |
|---|---|
git switch -c feat/login |
creates the branch and switches to it |
git switch main |
switches to another branch |
git branch |
the list of local branches |
git merge feat/login |
integrates the branch into the current one |
git rebase main |
moves your commits on top of the latest main |
git branch -d feat/login |
deletes the branch (after the merge) |
How to integrate: merge vs rebase
| Merge | Rebase | |
|---|---|---|
| What it does | creates a merge commit that joins the two histories | rewrites your commits as if you'd started from the latest main |
| History | shows exactly what happened, with branches | linear, easier to read |
| Rewrites history | no | yes (new commits, new hashes) |
| Safe on shared branches | yes | no — only on your own branch, before sharing it |
| When | integrating into main (usually through a PR) |
updating your branch with the latest changes from main |
Fast-forward
If main hasn't moved since you created the branch, the merge has nothing to "join": Git just moves the main pointer forward. That's called a fast-forward.
Before the merge:
mainABfeatCDAfter the merge — main was just moved forward, onto D:
mainABCDSquash merge
All the commits from the branch become a single commit on main. Popular on GitHub: the main history stays clean (one commit per PR).
Conflicts
They happen when two branches change the same lines. Git marks the spot:
<<<<<<< HEAD
const limit = 10
=======
const limit = 20
>>>>>>> feat/pagination
You pick the right version (or combine them), delete the markers, then git add + git commit (or git rebase --continue). The editor (VS Code) has buttons for this.
How to avoid them: short branches (days, not weeks), frequently updated with main.
Naming branches
feat/login-google
fix/cart-total-rounding
chore/upgrade-next-16
The prefix says the type of change, the rest — what it's about, in kebab-case.
Summary
- A branch = a pointer to a commit; you work isolated from
main. - Merge keeps the history; rebase makes it linear but rewrites it — only on your own branch.
- Short branches = few conflicts.