What a branch is

Beginner · 4 min

A Git branch is a 41-byte file holding one commit ID. See branches, HEAD and the commit graph in .git, and learn why creating and switching branches is instant.

Before thisHow Git stores history

LearnThatStack · 13 min · from 1:45

What a branch is

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by LearnThatStackReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A branch is a movable pointer to one commit, stored as a tiny file containing that commit's ID.
  • HEAD says which branch you are on, and a new commit moves the current branch's pointer forward.
  • Creating a branch copies nothing, so it takes no time and costs almost no space.
  • Switching branches rewrites your working directory to match the commit that branch points at.
  • Deleting a branch deletes only the pointer. Its commits remain until Git's cleanup finds nothing reaching them.

The idea hiding behind “a copy of the project”

The folder-copy habit from the first lesson was a branch in disguise: a place to try something without breaking the working version. In many older systems a branch really was a copy of the files, so branching was slow and people avoided it.

Git went the other way. Remember from the storage lesson that a commit is a snapshot pointing at its parent. History is already a chain of snapshots. A branch is just a name attached to one link in that chain.

A branch is a file with one line in it

$ git branch
* main
$ cat .git/refs/heads/main
ecbaa7f61acdfefb8dcaf1dc96befa3c012cb231

That is the whole of main: a file in .git/refs/heads/ whose entire content is a commit ID. I measured it with wc -c and it is 41 bytes, the 40-character ID and a newline. git log follows the parent links back from that commit, and what you see as “the history of main” is everything reachable that way.

Create a second branch and watch what’s added:

$ git branch experiment
$ ls .git/refs/heads
experiment
main
$ cat .git/refs/heads/experiment
ecbaa7f61acdfefb8dcaf1dc96befa3c012cb231

A new 41-byte file with the same ID. Both names point at one commit. git log --decorate shows the labels sitting on it:

$ git log --oneline --decorate -2
ecbaa7f (HEAD -> main, experiment) Move design notes into docs
f416046 Add design notes

No files were copied, and that’s why creating a branch is instant whether your project has ten files or a hundred thousand.

HEAD: which branch am I on

One more piece. Git needs to know which branch the next commit should extend. That’s HEAD, another small file:

$ cat .git/HEAD
ref: refs/heads/main

HEAD points at a branch name, and the branch points at a commit. The (HEAD -> main, ...) in the log output is the same fact. Switching branches changes that one line:

$ git switch experiment
Switched to branch 'experiment'
$ cat .git/HEAD
ref: refs/heads/experiment

A commit moves one pointer

I made a commit on experiment, appending a line to the README:

$ git commit -m "Try an idea in the README"
[experiment 7864b1b] Try an idea in the README
 1 file changed, 1 insertion(+)
$ cat .git/refs/heads/experiment
7864b1b6a8c50a9423a65d1129e6ee4a3ae8e3eb
$ cat .git/refs/heads/main
ecbaa7f61acdfefb8dcaf1dc96befa3c012cb231

experiment moved forward to the new commit, whose parent is ecbaa7f. main didn’t move. They have drifted apart, and the graph view shows it:

$ git log --oneline --decorate --graph --all -4
* 7864b1b (HEAD -> experiment) Try an idea in the README
* ecbaa7f (main) Move design notes into docs
* f416046 Add design notes
* 5c9e9c4 Document the clear command

--all includes every branch, and --graph draws the lines. It looks like one line here because one branch simply has an extra commit. When both branches get new commits, the line forks, and you’ll see that in the next few lessons.

Switching rewrites your files

Branches share history but not necessarily contents. After git switch main, the last line of the README was gone. After git switch experiment it was back. Git rewrote the working directory to match whichever commit the branch pointed at. That is the working-directory half of the three areas lesson: the staging area and your files are brought in line with the commit HEAD leads to.

This is also why Git wants a clean state before switching. If your uncommitted edits would be overwritten, Git stops you. The next lesson shows that, and the stashing lesson shows how to handle it.

Deleting a branch deletes a label

$ git branch -D experiment
Deleted branch experiment (was 7864b1b).

Only the pointer is gone. The commit 7864b1b is still in the object store, but nothing leads to it. Git prints the old ID so you can recreate the branch if it was a mistake, with git branch experiment 7864b1b. I didn’t run that last command. Eventually Git’s cleanup removes unreachable commits, so don’t count on it for long.

Default names aren’t special either. main is a branch like any other, which Git creates for you at git init, because you set init.defaultBranch.

What the video covers

The embed plays LearnThatStack’s “Git Will Finally Make Sense After This” from 1:45 to 5:41, the chapters “The DAG”, “Branches” and “HEAD” in the creator’s list, about four minutes of a 13-minute video published in December 2025. The remainder is on the staging area, checkout versus reset versus revert, rebase and the reflog, which are covered by other lessons, so I clipped it. I couldn’t read a transcript, so I can’t say how it explains the branch as a pointer. The channel is English-language, judged from the title and description. Philomatics’ short video on a detached HEAD is linked for what happens when HEAD points at a commit instead of a branch.

Where this fits

You know a branch is a pointer. Now you can make and move them deliberately: creating and switching branches.

Check yourself

  1. 01What is actually stored when you create a branch?

    A file under .git/refs/heads named after the branch, containing the 40-character ID of the commit it points at. No files or history are copied.

  2. 02What does HEAD hold when you are on a branch?

    The name of the branch, as a line like ref: refs/heads/main. HEAD points at the branch, and the branch points at a commit.

  3. 03You commit while on a branch called experiment. Which pointers move?

    Only experiment, which now points at the new commit. main and every other branch stay where they were.

  4. 04Why can a branch be created instantly even in a huge repository?

    Because it is just a pointer to an existing commit. Nothing is duplicated, so the cost does not depend on the project's size.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark