Why branches matter
Imagine you are writing a story. You want to try a new ending, but you do not want to overwrite the main draft. The clean solution is to make a copy of the document and edit the copy. If the new ending is good, you copy it back. If it is not, you throw the copy away and the original is untouched.
A branch in Git is that copy. It is a lightweight movable pointer to a commit. When you make a new commit on a branch, the pointer moves forward. The original branch stays where it was.
Branches are essential for teams and for coding agents because they let many people work at the same time without stepping on each other.
Creating and switching branches
The commands are small but powerful:
git branch feature— create a new branch calledfeatureat the current commit.git switch feature— moveHEADto thefeaturebranch. New commits now go there.git checkout feature— the older name for the same action; many tutorials and scripts still use it.git branch— list local branches and show which one you are on.
HEAD is Git's name for "where you are right now." It usually points to the tip of a branch. When you switch branches, HEAD moves with you.
Shortcut: git switch -c feature creates and switches to a new branch in one step.
A typical branch workflow
Most professional work follows this pattern:
- Start from the latest
mainbranch. - Create a feature branch:
git switch -c feature/login. - Make commits on the feature branch.
- Switch back to
mainand merge the feature in. - Delete the feature branch when it is no longer needed.
This is called the feature-branch workflow. It keeps the main branch stable while experiments happen on separate branches.
See a feature-branch diff
Below, the left side is main and the right side is the same file after a feature branch adds a welcome message and a helper function.
Try the branch graph
Click the buttons below to create a branch, switch to it, and make commits. Watch how the graph changes and how HEAD moves.
Check yourself
Why is it safer to build a new feature on its own branch instead of committing directly to main?
Your answer:
Sample answer: A feature branch keeps the main branch stable. If the feature breaks or the experiment does not work out, main is unaffected and other people can keep shipping.
Quick quiz
1. What does git branch feature do?
2. What does HEAD point to?
3. Which command both creates and switches to a branch?
Keep learning
You now know how to create and switch branches. Next, learn how to merge those branches back together and what happens when two branches change the same place.
Part of the “Agentic Engineering & AI Software Topics” series.