Please enable JavaScript to use CodeHS

Agentic Engineering & AI Software Topics

Git branches

Branches let you work on a copy of your project without disturbing the main line. You can experiment, fix a bug, or build a feature in isolation, then merge your work back when it is ready.

Git ~12 min High School / College Prep
The Big Idea

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.

Workflows

Creating and switching branches

The commands are small but powerful:

  • git branch feature — create a new branch called feature at the current commit.
  • git switch feature — move HEAD to the feature branch. 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.

Pattern

A typical branch workflow

Most professional work follows this pattern:

  1. Start from the latest main branch.
  2. Create a feature branch: git switch -c feature/login.
  3. Make commits on the feature branch.
  4. Switch back to main and merge the feature in.
  5. 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.

Code

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.

Interactive

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.

Ready. Create a branch to start.
Your Turn

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.

Check Yourself

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.