Skip to main content
ANVISoftware Solutions
Lesson 5 of 15Beginner14 min

Branches

By the end of this lesson

Create and switch branches to work on things independently.

A branch is a movable pointer to a commit. That is the entire definition, and holding onto it makes everything else about branching straightforward.

It is worth saying what a branch is not, because the wrong mental model causes real confusion. It is not a copy of your files. It is not a folder. It is not a snapshot taken before risky work. Creating a branch writes a small file containing one commit identifier, and that is all it does.

Commits form a chain; a branch is a pointerCommits are linked to their parents, forming a chain. The main branch points at one commit. Creating a feature branch adds a second pointer to the same commit. New commits on the feature branch extend the chain from there. Merging brings those commits back into main. A branch is only a movable pointer, which is why creating one is instant.C1C2C3C6mainF1F2feature branchmergeCreating a branch adds a pointer. No files are copied, which is why it is instant.
Commits form a chain; each branch is a pointer into it

When you make a commit, two things happen. The new commit is written, pointing back at the commit you were on. Then the branch pointer moves forward to the new commit. The pointer follows your work, which is what "movable" means.

Switching branches moves the pointer Git considers current, and updates the files in your working directory to match that branch's commit. Your folder contents change; nothing is copied anywhere. This is why switching is close to instant even on a large project.

Three terms that appear in nearly every Git message:

main
The conventional name for the branch holding the work everyone agrees on. It has no special powers — it is a branch like any other. Its importance comes from team agreement, not from Git.
HEAD
Git's note about where you currently are. Normally it points at a branch, and that branch points at a commit. When Git says HEAD, read it as "the commit you are sitting on right now".
Detached HEAD
What you get when you check out a commit directly instead of a branch. HEAD points at the commit with no branch in between, so commits you make have nothing following them. Useful for looking around; create a branch before doing work there.
Creating, switching and tidying up branches
Shell
# Where am I, and what branches exist?
git branch

# Create a branch and move onto it in one step
git switch -c add-expense-export

# ... make commits as normal ...

# Go back
git switch main

# See each branch with its latest commit subject
git branch -v

# Delete a branch once its work is merged
git branch -d add-expense-export
  • git branch with no arguments lists local branches and marks the current one. It creates nothing.
  • git switch -c creates a new branch pointing at your current commit, then makes it current. The -c stands for create.
  • git switch main moves you back. Your working files change to match main, and any commits you made on the other branch stay on that branch, untouched.
  • git branch -d refuses to delete a branch whose commits have not been merged anywhere. That refusal is a safety check, not an error to work around.

Summary

  • A branch is a movable pointer to a commit, not a copy of your files
  • Committing moves the current branch pointer forward to the new commit
  • HEAD is Git's note about where you are; main is a branch made important by agreement, not by Git
  • Switching updates your working files to match a different commit, which is why it is fast
  • Uncommitted changes belong to your working directory, not to a branch

Practice

Attempt each one before opening the solution. Getting it wrong first is how the idea sticks.

Try it yourself

Prove that switching does not copy anything

On main, commit a file called notes.txt containing one line. Create a branch, add a second line, and commit it there.

Switch back to main and open notes.txt. Then switch to the branch again and open it once more.

Show solution

On main the file has one line. On the branch it has two. The same path on disk shows different contents depending on which commit the current branch points at.

Nothing was duplicated. Git replaced the working copy of the file to match the commit you switched to. Once you have seen this happen, the phrase "a branch is a pointer" stops being abstract.

Shell
git switch -c add-second-note
# add a line to notes.txt in your editor
git add notes.txt
git commit -m "Add a second note about expense categories"

git switch main       # notes.txt has one line
git switch add-second-note   # notes.txt has two

Think about it

Think about it

You are halfway through an edit, with nothing committed, and you need to look at main. What are your options, and what happens if you try to switch straight away?

Show solution

If your edit touches a file that differs between the two branches, Git refuses to switch and says so, because switching would overwrite work it cannot recover.

If the file is identical on both branches, Git carries your uncommitted change across with you. That is convenient and occasionally surprising.

Your options are to commit the work, or set it aside with git stash. Stashing is covered later in this course and exists precisely for this interruption.

Knowledge check

Nothing is recorded and there is no score. The explanation appears either way.

What does creating a branch actually do?
You have uncommitted changes and you switch branches successfully. Where are those changes now?

Saved in this browser only.