Git and GitHub · Part 2 · Branches and merging
What a branch really is
Not a copy, a bookmark
Branches sound like Git copies the whole project. It doesn't.
Picture the commits as pages in a notebook, each one continuing from the page before. A branch is a sticky note on one page, with a name written on it:
- Creating a branch sticks a new note on a page. No pages are copied.
- Making a commit adds a page, and the note being used moves onto it.
- Deleting a branch peels the note off. The pages stay.
A branch is a tiny file
cd ~/Desktop
mkdir branch-practice
cd branch-practice
git init
echo "v1" > app.txt
git add app.txt
git commit -m "First commit"
echo "v2" >> app.txt
git commit -am "Second commit"
git log --oneline8e9e1b4 (HEAD -> main) Second commit
bb54941 First commitBranches live in .git/refs/heads/. Here's the whole main branch:
cat .git/refs/heads/main8e9e1b4c2f7a9d3e5b1c8f0a6d4e2b9c7a5f3e1dOne commit hash: a 41-byte file, with no copy of any code.
Creating a branch
git branch feature
cat .git/refs/heads/feature8e9e1b4c2f7a9d3e5b1c8f0a6d4e2b9c7a5f3e1dThe same hash: a second note on the same page. git log --oneline now shows both names on one commit:
8e9e1b4 (HEAD -> main, feature) Second commit
bb54941 First commitThat's why Git branches are instant and free, even in a project with 100,000 files.
What HEAD is
Branches point to commits. HEAD points to the branch being worked on:
cat .git/HEADref: refs/heads/mainIn the notebook, HEAD is the finger on the current sticky note. A new commit moves only that note forward.
Watching branches split
1.Commit on main
echo "v3" >> app.txt
git commit -am "Third commit on main"
git log --oneline --all8217d08 (HEAD -> main) Third commit on main
8e9e1b4 (feature) Second commit
bb54941 First commitOnly main moved. feature stayed put.
2.Switch to feature and commit
git switch feature
echo "feature work" > feature.txt
git add feature.txt
git commit -m "Add feature file"
git log --oneline --graph --all* 611344a (HEAD -> feature) Add feature file
| * 8217d08 (main) Third commit on main
|/
* 8e9e1b4 Second commit
* bb54941 First commitThe history has split into two paths:
8e9e1b4 is the common ancestor: the last commit both branches share.
See the branch picture in a real terminal

Files change when switching
On feature, the folder has feature.txt, and app.txt has two lines. Switching to main changes that:
git switch main
ls
cat app.txtapp.txt
v1
v2
v3feature.txt is gone and app.txt has a third line: Git rewrote the folder to match the commit main points to. Nothing is lost. git switch feature brings feature.txt back, because every snapshot is saved in .git.
Why branches matter
With everything on main, an urgent bug fix has to wait behind half-finished work, experiments are risky, and people working at once get in each other's way. Teams solve this the same way:
mainalways works. It's what's live.- Every feature or fix gets its own branch.
- When it's done and tested, it's merged back into
main. This is also how pull requests on GitHub work.
Detached HEAD
HEAD can also point straight at a commit instead of a branch:
git switch --detach bb54941HEAD is now at bb54941 First commitThis is detached HEAD: looking at an old page without holding any note. It's fine for looking around, but commits made here belong to no branch and are easy to lose. git switch main goes back.
Summary
| Concept | What it really is |
|---|---|
| Commit | A snapshot, plus a pointer to its parent |
| Branch | A name pointing to one commit, stored in a 41-byte file |
HEAD | A pointer to the current branch |
| Making a commit | Adds a snapshot and moves the current branch onto it |
| Switching branches | Moves HEAD and updates the files to match |
| Common ancestor | The last commit two branches share |
| Detached HEAD | HEAD pointing at a commit instead of a branch |