Git Branches and HEAD
Level 2 · Branches and merges
What a branch is, why making one costs almost nothing, and why most repos call the home branch main. How HEAD shows where you are standing.
Course contentsLesson 4 of 13
Level 1 · Your first commits
Level 2 · Branches and merges
Level 3 · Undo and set aside
Level 4 · Remotes and GitHub
Level 5 · Working as a team
After this lesson you can
- Make a branch and switch to it
- Explain what the main branch is for
- Get out of a detached HEAD
12 mincovers 4 glossary entries
Worth reading first: Git Log and Git Diff
What a branch is, and how HEAD follows you
A branch is a separate line of work inside one repository. You make one to try a change while the main line stays safe. In Git, a branch is only a label: a tiny file holding one 40-character commit hash. So making a branch is instant, however big the project is. Git switch -c makes one and moves you onto it.
When you commit on a branch, its label moves forward to the new commit. The other labels stay where they were. So two people can work on two branches at once, and neither sees the other's half-done work until it is joined back.
The main branch is the home branch of a repo. New branches start from it, and finished work comes back to it. Most teams call it main. Older projects often call it master, which was Git's default name for years. Git still uses master unless you set it, and plans to switch to main in Git 3.0.
A feature branch is a short branch for one piece of work, like a new search box or one bug fix. You start it from main and commit to it as often as you like. When it works, you join it back into main and delete the branch. The work stays in main.
HEAD is Git's name for where you are standing. Most of the time it points at a branch, and the branch points at its newest commit. When you switch branches, HEAD moves with you, and Git swaps the files in your folder to match. The file .git/HEAD holds one line naming your branch.
You can also step onto an old commit by its hash, to see how the project looked then. Now HEAD points at a commit, not at a branch. Git calls this a detached HEAD. Looking around is safe. Committing there is risky, because no branch label moves forward to keep the new commits.
If you did commit in a detached HEAD, make a branch right there with git switch -c and a name. The new label then holds your commits, and nothing is lost. Switch away first, and those commits are left with no label. Git keeps them for a while, but they are hard to find.
The common mistake is a branch that lives for weeks. Main keeps moving while it waits. The longer the gap, the more lines both sides have changed, and the harder the join back. A branch of a day or two joins cleanly. A branch of a month can take an afternoon of fixes.
$ git switch -c dark-mode
Switched to a new branch 'dark-mode'
$ git branch
* dark-mode
main
$ cat .git/HEAD
ref: refs/heads/dark-modeWorked example
Lucy tries a new menu
Lucy wants to try a new menu on her café's website without breaking the live one. She runs git switch -c new-menu and makes 6 commits over two days.
Her main branch still matches the live site the whole time. The owner says yes, so she joins the branch back. Had he said no, she would delete one label and lose nothing on main.
The words for this lesson4
Check yourself
What happens to main when I commit on another branch?
Finished this lesson?
Marking it complete also marks its 4 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.