Getting Started with Git — Chapter 3 / 4
Branches
Branches let you work on different things at the same time. They are the most powerful feature of Git. And they are simpler than you think.
A branch = a pointer
Key term: branch
A branch is not a copy of the code. It is just a small label that points to a commit. It's like a sticky note on a page of a book. The sticky note says "I am here". Moving the sticky note is instant. The book does not change.
Full guide on git branch →When you create a branch, Git creates a tiny file. This file contains the hash of the commit you are on. That's it. Nothing is copied.
Creating a branch is instant. It takes almost no disk space.
# main and feature point to commits
main → a1b2c3d
feature → e4f5g6h
HEAD → main (you are on main)
main is not special
Key term: main
main is just a name. It is the default branch that Git creates when you start a project. But it has no special power. You could call it "production" or "trunk" or anything else. It's like naming the first page of a notebook "page 1". You could also call it "intro".
Many teams use main as the "official" branch. But that is a convention, not a rule enforced by Git. Git treats all branches exactly the same.
Creating and switching branches
Here are the main commands for working with branches:
See all branches
$ git branch
# Lists all branches. The * shows which one you are on.
Create a branch
$ git branch feature/header
# Creates a branch but does NOT switch to it.
# You stay on your current branch.
Switch to a branch
$ git switch feature/header
# Moves HEAD to the branch.
# Your files change to match that branch.
Create and switch in one step
$ git switch -c feature/footer
# Creates the branch AND switches to it.
# This is the most common way to start new work.
git merge: combining two branches
Key term: merge
Merging means taking the work from one branch and bringing it into another. It's like combining two shopping lists into one. If both lists have different items, you keep all of them.
Full guide on git merge →When you finish working on a branch, you merge it into main. First, you switch to main. Then you run git merge.
$ git switch main
# Go to main first
$ git merge feature/header
# Bring the work from feature/header into main
Two ways to merge
Git can merge in two different ways. The way it chooses depends on the history.
Fast-forward
This happens when main has not changed since the branch was created. Git simply moves the main pointer forward. No new commit is created.
It's like a train on a straight track. The train just moves forward to the next station.
# Before: main is behind
A → B → C (feature)
↑ main
# After: main moves forward
A → B → C (main, feature)
Merge commit
This happens when both branches have new commits. Git creates a special commit that has two parents. This commit brings together the work from both sides.
It's like two roads that meet at a junction. A new point is created where the roads come together.
# Both branches have new commits
A → B → D → M (merge commit)
↘ C ↗
Visualizing the graph
There is a very useful command that shows you the full picture. It displays all branches and how they connect:
$ git log --graph --oneline --all
* f7a8b9c (HEAD → main) Merge branch 'feature'
|\
| * d4e5f6a (feature) Add feature
|/
* a1b2c3d Initial commit
Use this command often. It helps you see where you are and what happened. It is the best way to understand the state of your project.
Further reading
Common mistakes
- ●Thinking a branch is a copy of the code. It is just a pointer. No files are duplicated.
- ●Forgetting to switch to main before merging. You must be on the branch that receives the changes.
- ●Creating branches but never merging them. Old branches pile up and create confusion.
- ●Working directly on main instead of creating a branch. Always create a branch for new work.
Your feedback matters
Help us improve this course by sharing your thoughts.