All courses

Branches as parallel lines of work

Explain a branch as a movable pointer to a line of commits. Create one for an experiment, switch between branches, and merge or delete it depending on how the experiment went.

Builds on: Git commits as snapshots you can return to

Read it card by card Start the whole course

Try something risky without risking main

Your notes app works and everything is committed. You want the agent to try a bold change: move storage from a text file to an SQLite database. It might take several commits, and it might turn out to be a dead end. Meanwhile you want a working version you can switch back to at any moment, without copying the whole folder. How do you keep two lines of work apart?

A branch is a sticky note on a commit

Your commits form a chain, each pointing back to the one before. A **branch** is just a name that points at one commit, like a movable sticky note. `main` is simply the first branch. A new branch copies no files. It is one more note. When you commit, the note for the branch you are on moves forward to the new commit. Switching branches rewrites your project files to match the commit that branch points to. Files git was never asked to save are left where they are.

The experiment routine

1. Commit everything first, so nothing is half-saved. 2. `git switch -c <name>` creates a branch at the current commit and moves you onto it. 3. Work there and commit as usual. Only this branch's pointer moves. 4. `git switch <name>` jumps between branches. `git branch` lists them; `*` marks yours. 5. **It worked:** `git switch main`, then `git merge <name>`, which brings that branch's commits into the branch you are on. Then `git branch -d <name>` removes the now-unneeded name. 6. **It failed:** `git switch main`, then `git branch -D <name>`.

Walking through both endings

You run `git switch -c try-sqlite`, let the agent make the change, and commit it there. Then `git switch main`: your folder shows the old text-file version again, and the app still works. Switch back to compare whenever you like. If it's good, merge it into `main` (from `main`). If it's a mess, delete the branch with `-D`. Capital D forces the delete, because git refuses `-d` on work that was never merged.

The happy path in the terminal

After the first command the agent edits the files, then you save its work in a commit. The experiment worked. While you were away, `main` gained no new commits, so the merge is a **fast-forward**: git just slides the `main` note forward to the branch's commit.
$ git switch -c try-sqlite
Switched to a new branch 'try-sqlite'
$ git add .
$ git commit -m "Store notes in SQLite"
[try-sqlite 9b7e4d2] Store notes in SQLite
$ git switch main
Switched to branch 'main'
$ git merge try-sqlite
Updating 3f2a1c0..9b7e4d2
Fast-forward
$ git branch -d try-sqlite
Deleted branch try-sqlite (was 9b7e4d2).

Remember

A branch is a cheap, movable name for a line of commits. Branch off before an experiment. If it worked, switch to `main` and merge the branch in. If it didn't, delete the branch with `-D`. Either way, `main` stays untouched until you decide.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.