Lesson 4 of 6 · 20 min
Branches: experiment without breaking anything
Create parallel versions of your project and merge them back when they work.
What a branch is
A branch is a parallel line of work. Your main branch stays stable while you try a risky idea on a side branch. If the idea fails, throw the branch away — main is untouched. If it works, merge it back.
This is exactly how open source contributions work: you never edit a project's main branch directly. You make a branch, do your work, and propose it back.
Try it
git branch feature-ideas # create a branch
git switch feature-ideas # move onto it
(On older Git versions: git checkout -b feature-ideas does both steps.)
You are now on the side branch. Make a change:
echo "Idea: add a contributors page" >> README.md
git add README.md
git commit -m "Draft contributors page idea"
Now switch back:
git switch main
Open README.md — the idea is gone from view. It is safe on its branch, not lost. Switch back with git switch feature-ideas and it returns.
Merge when ready
git switch main
git merge feature-ideas
The branch's commits are now part of main. Delete the finished branch:
git branch -d feature-ideas
The command you will use most
git branch
lists your branches and marks the current one. When contributing to a real project you will type git switch -c my-fix more than almost any other command — create a branch per fix, never work on main.
Next lesson: taking your repo to GitHub, where collaboration happens.