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 switchandgit 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 branch is a name that points to one commit in that chain, the newest one on that line of work, called the branch tip. 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.
HEAD 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:
- Git builds the new snapshot from the staging area.
- It creates a commit whose parent is the commit HEAD currently leads to.
- 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?
A branch is a name that holds one commit ID, so creating it writes about 41 bytes. No files are copied and no commit is made; the branch starts by pointing at the commit you are on. That is why branching is instant in Git.
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.