1. Courses
  2. Developer Foundations
  3. Git from the Inside
  4. Stop 3

35 minIntermediate

Branches and merging

A branch lets you work on one change without disturbing anyone else, and merging brings that work back. Once you see that a branch is only a movable label on a commit, every branch and merge command becomes predictable. This lesson builds that picture and then uses it.

By the end you can

  • Explain what a branch and HEAD are, and what moves when you commit
  • Create, switch and delete branches with git switch and git branch
  • Predict whether a merge will fast-forward or create a merge commit, and why
  • Read git log --graph, find the two parents of a merge commit, and compute a merge base in code
  • Agree branch names and merge style with your team

The everyday picture

The category team wants to try a new layout for the snacks aisle. Nobody closes the whole store for this. They try it in one aisle, take a photo each evening, and the rest of the shop keeps selling as usual. If the new layout works, it becomes the standard. If it fails, they put the old shelves back and nobody else was affected.

A Git branch is that one aisle. You keep taking snapshots (commits) of your experiment while main carries on. The surprising part is how little a branch costs. Git does not copy the shop. It writes down one note: "the snacks experiment is at photo number 47". That note is the whole branch.

A branch is a pointer, and HEAD points to the branch

You saw in Snapshots and the three areas that every commit stores its parent, so commits form a chain back in time. A is a name that points to one commit in that chain, the newest one on that line of work, called the branch . On disk it is a tiny text file. .git/refs/heads/main holds 40 hex characters and a newline, the ID of the tip commit. Creating a branch writes one more file like that. No project files are copied, which is why creating a branch takes milliseconds even in a huge repository.

answers "where am I?". Normally it does not point at a commit directly. It points at a branch: the file .git/HEAD contains ref: refs/heads/feature/discount. That one level of indirection explains what git commit does:

  1. Git builds the new snapshot from the staging area.
  2. It creates a commit whose parent is the commit HEAD currently leads to.
  3. It moves the branch that HEAD points to forward to the new commit. HEAD itself still names the same branch.

Every other branch stays where it was. That is the whole safety promise: your commits on feature/discount cannot change main. main moves only when you commit, merge or reset while HEAD points at it, or run a command that names it.

Quick check

You create a new branch in a repository with 50,000 files. What does Git write to disk?

Before git commit HEAD main feature/discount parent 4516442 cb5ae10 df98455 After git commit HEAD main stays feature/discount moved new commit 4516442 cb5ae10 df98455 845ab9c
Before: main and feature/discount point at the same commit, and HEAD points at feature/discount. After git commit: the new commit's parent is df98455, only feature/discount moves to it, and main stays where it was.

Loading the rest of the lesson…