Why Branches Exist

What this is. A branch is a separate line of history. You can make changes on it, commit them, and the branch you came from is completely untouched until you decide to merge.

The point is not organization. The point is that you can try something without risking what already works.

The commands.

git branch lists branches, marking the current one.

git branch name creates one.

git switch name moves to it.

git switch -c name creates and moves in one step, which is what you will actually type.

git merge name brings that branch's commits into the current one.

✦ Branches are cheap, and that is the whole design

In older version control systems a branch meant copying the entire project and merging was genuinely painful, so people avoided branching. Git made branches almost free, which changed the workflow: you branch for a two line fix without thinking about it. If a branch turns out to be a bad idea, you switch away and delete it, and nothing was ever at risk.

The same work, with and without a branch

ON MAIN

edit main directly
commit

it breaks

now main is broken
and the history says so

ON A BRANCH

git switch -c try-something
edit and commit freely

it breaks

git switch main
main never knew
Compare → The branch is not extra bureaucracy. It is the thing that lets you be careless in a contained way, which is what experimenting requires.
SANKOFA SANDBOX
The portfolio is here and already has a .gitignore. Type `cd portfolio` to begin.
learner:~$

Try it → Run `cd portfolio`, `git init`, `git add .`, `git commit -m "Initial commit"`. Then `git branch` to see where you are, then `git switch -c add-contact-page`, then `git branch` again.

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Merging, and What a Conflict Really Is →
← Back