GtGit · Lesson 3 of 8

Branches

A branch is a parallel timeline for your code. Experiment freely on a branch — if it works, merge it in; if not, delete it and main never knew.

The default branch is usually called main. Creating a branch is instant and free — git just writes a 43-byte file pointing at a commit. The universal workflow: never work directly on main; make a branch per feature or fix, merge it when done.

Bash
git branch                   # list branches (* = current)
git switch -c add-login      # create branch and switch to it

# ...edit files, add, commit as usual...
git add .
git commit -m "Add login page"

git switch main              # back to main
# Your login changes 'disappear' — they live on the branch.

git switch add-login         # and they're back
Bash
# Merge the branch into main:
git switch main
git merge add-login
# Fast-forward or merge commit — either way, main now has the work.

git branch -d add-login      # delete the merged branch

# See the shape of history:
git log --oneline --graph --all
◆ Note
Older tutorials use 'git checkout -b name' — it still works, but 'git switch' (for branches) and 'git restore' (for files) split checkout's two jobs into clearly-named commands. Prefer them.