Git & Command Line Tutorial · Branching and Collaboration · Lesson 13 of 20
Branches
Work on features in isolation with branches: create, switch, list and delete them, and understand that a branch is just a movable pointer to a commit.
What a branch is
A branch is a lightweight, movable label pointing at a commit. main is just a branch. When you commit, the current branch moves forward. HEAD means "where I am now".
Branches let you build a feature, fix a bug and try an experiment at the same time without them interfering.
| Command | Does |
|---|---|
git branch |
List branches (* marks the current one) |
git switch -c feature/login |
Create a branch and switch to it |
git switch main |
Switch to an existing branch |
git branch -d feature/login |
Delete a merged branch (-D forces) |
git log --oneline --graph --all |
See all branches as a graph |
Name branches by purpose: feature/checkout, fix/login-redirect, chore/upgrade-deps.
git init -b main --quiet shop && cd shop
echo "home page" > index.html && git add . && git commit -m "Add home page" --quiet
git switch -c feature/cart --quiet
echo "cart page" > cart.html && git add . && git commit -m "Add cart page" --quiet
git switch main --quiet
echo "on main, cart.html exists? $(test -f cart.html && echo yes || echo no)"
echo "fix typo" >> index.html && git commit -am "Fix typo on home page" --quiet
git branch
git log --oneline --graph --all
Output
on main, cart.html exists? no feature/cart * main * 45d1f66 Add cart page | * 3182aee Fix typo on home page |/ * 555b727 Add home page
Run this on your computer — see “Get Started” for setup.
Test yourself
What is a Git branch?
A copy of all filesA movable pointer to a commitA remote serverA folder in .git with the code
Show answer
Answer: A movable pointer to a commit