Sub topic 1
What a Branch Really Is
Prerequisites
This chapter assumes you can run git add, git commit, git push, and git pull. It also assumes you know that a commit is a snapshot of your project with a message and a parent. You should have a remote on GitHub, GitLab, or Bitbucket, although this chapter only uses a local repository.
A branch is a pointer
A Git branch is a small file that holds the hash of one commit. It does not hold a copy of your files. Creating a branch writes about 41 bytes: a 40-character hash and a newline. Because of this, creating, switching, and deleting branches is fast, and a repository with thousands of branches costs almost nothing extra.
Each commit stores a reference to its parent. Following those references from a branch's commit back to the first commit gives you that branch's history. The branch name is only a label on the newest commit.
HEAD and the current branch
HEAD is a special pointer that says where you are now. Normally it points to a branch name, and that branch name points to a commit. When you commit, Git creates the new commit with the current commit as its parent. Git then moves the branch that HEAD points to forward to the new commit.
In the diagram, arrows between commits point from each commit to its parent when you read the history backward. Here the chain is drawn left to right in the order the commits were made.
Seeing it in a real repository
Run these commands in an empty directory. They create a repository with two commits on main, then add a branch.
git init -b main demo
cd demo
git commit --allow-empty -m "First commit"
git commit --allow-empty -m "Second commit"
git switch -c feature/login-form
git commit --allow-empty -m "Start login form"
git log --oneline --decorate --graph --all
cat .git/HEAD
cat .git/refs/heads/main
cat .git/refs/heads/feature/login-formThe git log output shows labels such as HEAD -> feature/login-form and main next to commit hashes. The cat commands show the internals. .git/HEAD contains ref: refs/heads/feature/login-form. Each file under .git/refs/heads/ contains one commit hash.
git switch -c creates the branch at your current commit and moves HEAD to it. The older equivalent is git checkout -b.
What this means in practice
- Branching is cheap. You do not need to avoid branches for performance reasons. Any cost comes from the work of combining them later.
- Deleting a branch deletes a label. The commits remain until Git's garbage collection removes unreachable ones, which by default happens after a grace period of weeks.
git reflogcan often help you recover them. - Branches move. Commits, merges, and rebases all change which commit a branch name points to. The later chapters on merging and rebasing build on this idea.
Common errors
| Mistake | What happens | Fix |
|---|---|---|
| Committing while `HEAD` is detached (for example after `git checkout <hash>`) | The new commits have no branch label and can be lost | Run `git switch -c rescue-branch` before leaving |
| Thinking a branch is a folder or a copy | You avoid branching to save space | Remember it is only a pointer to a commit |
| Forgetting which branch you are on | Commits land on the wrong branch | Check `git status` or `git branch --show-current` first |
Exercise
You run git branch hotfix while on main, which points to commit C. Which commit does hotfix point to, and which branch does HEAD point to?
Show answer
hotfix points to commit C, the same commit as main. HEAD still points to main, because git branch creates the branch but does not switch to it. Use git switch hotfix to move HEAD.
Exercise
Why is creating a branch in a repository with a 5 GB history as fast as creating one in a tiny repository?
Show answer
Creating a branch only writes a small file containing one commit hash. It does not copy any files or history, so the repository size does not matter.