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

Terminal
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 --oneline
Output
8e9e1b4 (HEAD -> main) Second commit
bb54941 First commit

Branches live in .git/refs/heads/. Here's the whole main branch:

Terminal
cat .git/refs/heads/main
Output
8e9e1b4c2f7a9d3e5b1c8f0a6d4e2b9c7a5f3e1d

One commit hash: a 41-byte file, with no copy of any code.

Creating a branch

Terminal
git branch feature
cat .git/refs/heads/feature
Output
8e9e1b4c2f7a9d3e5b1c8f0a6d4e2b9c7a5f3e1d

The same hash: a second note on the same page. git log --oneline now shows both names on one commit:

Output
8e9e1b4 (HEAD -> main, feature) Second commit
bb54941 First commit
bb54941
8e9e1b4HEAD → mainfeature

That's why Git branches are instant and free, even in a project with 100,000 files.

Watching branches split

1.Commit on main

Terminal
echo "v3" >> app.txt
git commit -am "Third commit on main"
git log --oneline --all
Output
8217d08 (HEAD -> main) Third commit on main
8e9e1b4 (feature) Second commit
bb54941 First commit

Only main moved. feature stayed put.

bb54941
8e9e1b4feature
8217d08HEAD → main

2.Switch to feature and commit

Terminal
git switch feature
echo "feature work" > feature.txt
git add feature.txt
git commit -m "Add feature file"
git log --oneline --graph --all
Output
* 611344a (HEAD -> feature) Add feature file
| * 8217d08 (main) Third commit on main
|/
* 8e9e1b4 Second commit
* bb54941 First commit

The history has split into two paths:

bb54941
8e9e1b4
8217d08main
611344aHEAD → feature

8e9e1b4 is the common ancestor: the last commit both branches share.

See the branch picture in a real terminal
git log --oneline --graph --all in a Mac terminal: 611344a (HEAD -> feature) Add feature file, 8217d08 (main) Third commit on main on a side line, joining at 8e9e1b4 Second commit and bb54941 First commit.

Files change when switching

On feature, the folder has feature.txt, and app.txt has two lines. Switching to main changes that:

Terminal
git switch main
ls
cat app.txt
Output
app.txt
v1
v2
v3

feature.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:

  • main always 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:

Terminal
git switch --detach bb54941
Output
HEAD is now at bb54941 First commit

This 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

ConceptWhat it really is
CommitA snapshot, plus a pointer to its parent
BranchA name pointing to one commit, stored in a 41-byte file
HEADA pointer to the current branch
Making a commitAdds a snapshot and moves the current branch onto it
Switching branchesMoves HEAD and updates the files to match
Common ancestorThe last commit two branches share
Detached HEADHEAD pointing at a commit instead of a branch