Git Branches

Work on new ideas safely in branches, then switch between them, list them and clean them up.

Lesson 6 of 30Beginner17 min read

Imagine you want to try a new design for your website's menu, but the current version works and you don't want to break it. In Git, you create a branch: a separate line of work where you can commit freely. The main branch stays exactly as it was until you decide to bring your changes in.

Branches are cheap and fast in Git, so developers use them constantly. A typical team creates a new branch for every feature or bug fix, works on it, and merges it back when it's ready. Several people can each work on their own branch at the same time without getting in each other's way.

In this lesson you'll create branches, switch between them, see how commits on one branch don't affect another, and tidy up branches you no longer need.

What a branch really is

A branch is just a movable label pointing at a commit. When you make a new commit on a branch, the label moves forward to it.

HEAD is Git's name for "where you are now". It normally points at the branch you've switched to. When you commit, the branch that HEAD points to moves forward, and the other branches stay where they were.

That's why creating a branch is instant: Git doesn't copy your files, it just writes a new label.

Creating and switching branches

Terminal
git branch menu-redesign        # create a branch
git switch menu-redesign        # move to it
git switch -c footer-links      # create AND switch in one step
git switch main                 # go back to main

When you switch, Git updates the files in your folder to match that branch. Files you've changed but not committed come with you, as long as they don't clash with the other branch. To be safe, commit your work before switching.

Older tutorials use git checkout name and git checkout -b name. They still work, but git switch is clearer and was added for exactly this job.

Listing, renaming and deleting

  • git branch lists local branches. The current one has a * next to it.
  • git branch -v also shows the latest commit on each branch.
  • git branch -m new-name renames the current branch.
  • git branch -d name deletes a branch that has been merged.
  • git branch -D name force-deletes a branch even if it isn't merged. Its commits will be lost, so use it carefully.

Naming branches

Good branch names say what the work is about: add-search, fix-login-redirect, update-readme. Many teams add a prefix such as feature/, fix/ or docs/. Use lowercase words joined by hyphens, and avoid spaces, which aren't allowed.

Git Branches Syntax

Branch commands

git branch                 # list
git branch <name>          # create
git switch <name>          # move to a branch
git switch -c <name>       # create and switch
git branch -m <new-name>   # rename current
git branch -d <name>       # delete (merged)

Git Branches Summary

  • A branch is a lightweight label pointing at a commit; HEAD shows where you are.
  • git switch -c name creates a branch and switches to it; git switch name moves between branches.
  • Commits on one branch don't change other branches.
  • git branch lists branches; -d deletes merged branches, -D forces deletion.
  • Do new work on branches and keep main working.

Git Branches Examples

Each example shows the code, the output it produces and what each part does.

Example 1

Create a branch and commit on it

Terminal
git switch -c dark-mode
git add styles.css
git commit -m "Add dark colour scheme"
git branch
Output
Switched to a new branch 'dark-mode'
[dark-mode 52ac9e1] Add dark colour scheme
 1 file changed, 18 insertions(+)
* dark-mode
  main
Line-by-line explanation
  1. Line 1Creates the branch and moves to it.
  2. Lines 2 to 3This commit only exists on dark-mode.
  3. Line 4The * shows the current branch.
Example 2

Branches are independent

Terminal
git log --oneline -2
git switch main
git log --oneline -2
Output
52ac9e1 (HEAD -> dark-mode) Add dark colour scheme
9d41e07 (main) Add contact form
Switched to branch 'main'
9d41e07 (HEAD -> main) Add contact form
6b2a1f3 Style the navigation bar
Line-by-line explanation
  1. Line 1On dark-mode, the newest commit is the dark colour scheme.
  2. Line 2Switching to main puts the files back the way they are on main.
  3. Line 3main doesn't contain the dark mode commit. It stays safe until you merge.
Example 3

See branches as a graph

Terminal
git log --oneline --graph --all
Output
* 52ac9e1 (dark-mode) Add dark colour scheme
* 9d41e07 (HEAD -> main) Add contact form
* 6b2a1f3 Style the navigation bar
* 3f9c2ab Add home page
Line-by-line explanation
  1. Line 1--all includes every branch. dark-mode is one commit ahead of main.

Common Git Branches Mistakes

Committing on the wrong branch

Don't
# forgot to switch, so the experiment lands on main
git commit -m "Try new layout"
Do
git switch -c new-layout
git commit -m "Try new layout"

Run git status or git branch before committing to check where you are.

Spaces in branch names

Don't
git switch -c new header
Do
git switch -c new-header

Deleting the branch you're on

Don't
git branch -d dark-mode   # while on dark-mode
Do
git switch main
git branch -d dark-mode

Git refuses to delete the branch that HEAD points to.

Git Branches in Real Projects

Branches on a team

On a product team, a developer starts each ticket with something like git switch -c fix/checkout-total, commits their work there, and opens a pull request on GitHub when it's ready. Meanwhile, main is what gets deployed to the live website, so it must always work.

Git Branches FAQ

What is the difference between git switch and git checkout?

git checkout is the older command and does two jobs: switching branches and restoring files. Git 2.23 split those jobs into git switch for branches and git restore for files, which makes mistakes less likely. git checkout still works, so you'll see it in older tutorials and scripts.

How do I tell which branch I'm on?

Run git branch --show-current to print just the name. git status also starts with "On branch ...", and git branch marks the current branch with an asterisk. Many terminal prompts and editors show it too.

How do I create a branch from a specific commit or another branch?

Give the starting point after the new name: git switch -c hotfix main starts from main, and git switch -c investigate 3f9c2ab starts from that commit. Without a starting point, the new branch begins at your current commit.

Why does git branch -d say the branch is not fully merged?

-d only deletes a branch whose commits are already part of your current branch (or its upstream), so you can't lose work by accident. If you're sure you don't need those commits, git branch -D name deletes it anyway. The commits can usually still be found through git reflog for a while afterwards.

How do I list branches with the most recently used first?

Run git branch --sort=-committerdate. The minus sign sorts in descending order, so the branch with the newest commit comes first. It's useful in a project with many old branches when you're trying to find what you worked on last.

Git Branches Exercises

Answer each one and check it instantly. If you get stuck, open the hint before the answer.

Fill in the blankseasy

Complete the command that creates a branch called about-page and switches to it.

git   about-page
Multiple choiceeasy

How can you tell which branch you're on in git branch output?

Multiple choicemedium

You commit on feature-x, then switch to main. Is the new commit on main?

Predict the outputmedium

What does git branch -d old-idea do if old-idea has commits that were never merged?

Multiple choicemedium

Which command deletes a branch that has already been merged?

Git Branches Quiz

5 questions to check what you learned.

  1. Which command switches to an existing branch called main?

    • git branch main
    • git move main
    • git switch main
    • git main
  2. What is HEAD?

    • The first commit
    • A pointer to where you are now, usually the current branch
    • The main branch
    • The newest file
  3. Why is creating a branch so fast in Git?

    • A branch is just a label pointing at a commit; no files are copied
    • Git compresses all files
    • Branches are stored online
    • Git creates branches in the background
  4. Which older command does the same as git switch -c fix-nav?

    • git branch -c fix-nav
    • git new fix-nav
    • git checkout -b fix-nav
    • git checkout fix-nav --create
  5. What is a branch in Git?

    • A copy of the whole project folder
    • A remote server
    • A backup file
    • A movable label pointing at a commit, used for a separate line of work