What a Branch Is
Topic 23

What a Branch Is

Branching

You are about to try something on Sandpiper that might not work — a new page for the sightings, laid out differently. You want to try it without disturbing the version the club is using today.

That is what a branch is for. And the thing to get straight before anything else is what a branch is: a name that points at a commit. Not a copy of your files. Not a folder. Not a separate project.

The mental picture

Commits form a chain, each one remembering the one before it. A branch is a sticky label attached to one of those commits — and when you make a new commit, the label moves along with you, so it always marks your latest work.

That is the entire mechanism. Creating a branch means writing a new sticky note; it involves no copying, which is why it is instantaneous even on an enormous project.

The label moves forward as you commit
a3f9c21Add the home page
5b2a9f3Add basic styling
c8d1e07← main points here

main is not special

You have been on a branch since Chapter 2 without noticing. git init created one and called it main, because of the setting from Chapter 1, and every commit you have made has moved that label forward.

main has no special powers. It is a label like any other. What makes it matter is a convention: everyone agrees that main holds the real version of the project. That agreement is what makes the team workflow in Chapter 8 a meaningful choice rather than a technical requirement.

Why branch at all

Because you want to work on something without disturbing the version that works.

With a branch, the experiment and the working site exist at the same time, in the same folder, and you move between them with a command. If the experiment fails you delete the label and nothing about the working site was ever at risk. If it succeeds you merge it in.

None of this requires another person. Branching alone is the difference between "I will try this and hope" and "I will try this and it costs nothing if it fails".

What switching actually changes

Here is the part that startles everyone the first time. When you switch to a different branch, Git rewrites the files in your folder to match the commit that branch points at.

Same folder, different contents. Open index.html on one branch and it says one thing; switch, open it again, and it says another. Nothing was lost: the other version is attached to the other label, and switching back brings it straight back.

Where you are right now

List the branches, with a marker on the current one
git branch
Sandpiper today
* main

One branch, and the asterisk says you are on it. git status says the same thing on its first line, which is why that line has been sitting there being ignored for four chapters.

Press q if git branch opens a pager — the same escape as Chapter 3.

Common Confusions
  • "A branch copies my project." Nothing is copied. A branch is a label pointing at a commit, and the commits themselves are shared between branches until the work actually diverges.
  • "Branching is a team feature." Branching alone is what lets you try something risky and abandon it in one command. You get the entire benefit before anyone else is involved.
  • "main is the original branch, so it is safer or more important." It is a name, and its authority comes from a convention people agree on. Technically it is exactly like every other branch.
  • "Switching branches will lose the work I have not finished." The work stays attached to the branch you made it on. Git also refuses to switch if a switch would overwrite uncommitted changes, which is the next topic.
Why It Matters
  • "Label that moves" is the model that makes merging comprehensible. Without it, a merge looks like magic and a conflict looks like punishment.
  • Every team workflow in Chapter 8 is built out of branches, so this has to be solid before GitHub arrives and adds a second copy of everything.
  • Knowing that a branch costs nothing is what makes it reasonable to create one for a change that takes ten minutes — which is exactly how professionals work.

Knowledge Check

What is a branch, mechanically?

  • A name pointing at a commit, which moves forward as you make new ones
  • A copy of the project folder, kept separately so the original is untouched
  • A list of the changes made since the branch was created from its parent
  • A separate repository that is joined back to the original when work finishes

What happens to the files in your folder when you switch branches?

  • Git rewrites them to match the commit that the branch points at
  • They stay as they are, and only Git's internal records change position
  • They are copied into a subfolder named after the branch you switched to
  • They are merged with the contents of the branch you are switching to

What makes main special?

  • Only a convention: everyone agrees it holds the real version of the project
  • Git protects it automatically and refuses to accept broken commits on it
  • It is created first, so every other branch is technically derived from it
  • It is the only branch that can be pushed to GitHub or shared with others

Why is creating a branch effectively instant, even on a huge project?

  • Because it writes one small label instead of copying any of the project files
  • Because Git compresses the copied files before writing them to the disk
  • Because only the files changed since the last commit are copied across
  • Because the copy happens gradually in the background after the command returns

You got correct