Branches: Experiment Without Fear
Branches let you work on a new idea in a parallel copy, without disturbing your working version โ then merge it back when it's ready. It's Git's most powerful feature, and simpler than it sounds.
Imagine you want to try a big new feature, but you don't want to risk breaking the version that currently works. Branches are Git's answer: create a parallel line of work, experiment there freely, and when it's ready, merge it back into the main version. If it doesn't work out, you just abandon the branch - the main version was never touched. Branching is Git's superpower, and the heart of how teams work.
What a branch is
A branch is an independent line of development - a parallel copy of your project where you can work without affecting the main version.
git branch new-feature # create a branch
git checkout new-feature # switch to it (or: git switch new-feature)
# ... work and commit on the branch, safely ...
git checkout main # switch back to the main line
git merge new-feature # bring the finished work into mainmain) stays working while you build something new on a separate branch. Try a risky redesign, a new feature, a wild idea - all on a branch. If it works, merge it in; if it doesn't, delete the branch and main is exactly as it was, untouched. This is how developers work on several things at once without stepping on each other or breaking the stable version, and it's why branching is considered Git's killer feature.Merging brings it together
When a branch's work is ready, merging combines it back into the main line. Usually this is smooth; sometimes Git needs your help.
| Concept | What it means |
|---|---|
| main branch | The stable, working version of your project |
| feature branch | A safe place to build something new |
| merge | Combine a branch's work back into another |
| merge conflict | Two branches changed the same line - you decide |
A merge conflict sounds scary but just means Git can't automatically tell which of two changes to the same line to keep, so it asks you to choose. You edit the file to the version you want and finish the merge. Conflicts are normal and routine, not a disaster - just Git being careful not to guess.
Parallel work, safely
- A branch is a parallel line of work that doesn't affect the main version.
- Experiment fearlessly - if a branch fails, delete it; main is untouched.
- Merge brings a finished branch's work back into main.
- Merge conflicts are routine - Git asking you to choose between two changes.
๐ฏ Key takeaways
- A branch is an independent, parallel line of work that doesn't affect the main version.
- Branches make experimentation safe: if a branch's idea fails, delete it and main is untouched.
- Merging combines a finished branch's work back into the main line.
- Merge conflicts are routine, not disasters - Git asking you to choose when two branches changed the same line.
Frequently asked
Why would I use a branch instead of just working directly on my main code?
Because branches let you keep a stable, working version safe while you build or experiment - which is enormously valuable even solo, and essential in a team. Working directly on your main code means every half-finished experiment and risky change is sitting in your one and only version; if you're midway through a big change and something urgent comes up, or the experiment goes badly, your working version is now broken or messy. With a branch, your main line stays clean and functional while all the in-progress, might-not-work stuff happens on the side. You can try a bold idea on a branch, and if it doesn't pan out, you simply abandon the branch and your main version never knew it happened - no cleanup, no risk. You can also juggle multiple things: a branch for a new feature, another for a bug fix, switching between them without their changes tangling together. In a team, branches are what let many people work simultaneously without overwriting each other - everyone works on their own branch and merges when ready. Even alone, the habit of 'main stays working, new work happens on a branch' gives you a stable base to always return to and the freedom to experiment without consequences. It's the same fearlessness theme as the rest of Git, applied to working on more than one thing at once. For very small solo tinkering you might work directly on main and that's fine, but as soon as you're doing anything you're unsure about or anything substantial, a branch keeps your safe version safe while you play.
Merge conflicts sound terrifying - what actually happens and how do I handle one?
Merge conflicts have a scary reputation but are genuinely routine and manageable once you understand what they are. A conflict happens only in a specific situation: two branches you're merging have both changed the same lines of the same file in different ways, so Git can't automatically decide which version to keep - and rather than guess and possibly pick wrong, it stops and asks you. That's actually Git being careful and respectful of your work, not a breakage. When it happens, Git marks the conflicting spots in the file, showing you both versions - the change from one branch and the change from the other - clearly delineated. Your job is simply to edit that file to be how you want it: keep one version, keep the other, or combine them - whatever's correct - and remove the markers Git added. Then you tell Git the conflict is resolved and complete the merge. That's it. Most conflicts are small and obvious (you can see both changes and know which you want), and they're a normal part of collaborating - experienced developers handle them regularly without drama. A few things make them easier: they're less frequent and smaller when people work on different areas and merge often (rather than letting branches drift far apart for a long time), and when one does occur, slow down and read both sides before deciding. The key mindset shift is that a conflict isn't an error you did something wrong to cause - it's just Git surfacing a decision only a human can make, because two changes overlapped. Handle a couple of them and the fear disappears; they become a minor, familiar step rather than something to dread. And remember the safety net still applies: your committed work on both branches is safe throughout, so you can't lose anything by working through a conflict.
Related lessons
Seeing History & Undoing Mistakes
The real payoff of committing arrives now: you can look back through your project's whole โฆ
Version Control with GitWorking with Others (and GitHub)
Git becomes truly powerful when shared. Remotes and GitHub let a whole team โ or the wholeโฆ
Version Control with GitGood Git Habits
Git rewards a few simple habits โ commit often with clear messages, ignore what shouldn't โฆ