Dashboard

What Is a Git Branch? A Plain Guide for AI Agent Users

A Git branch is just a movable pointer, which is why creating one costs nothing. Here are the six commands to know and how to keep an AI agent safely on its own branch.

Steve Jefferson
Steve Jefferson
Developer Advocate
22 October 20261 min read

A Git branch is a lightweight, movable label that points at one commit, which is one saved snapshot of your project. The Pro Git book puts it this way: "A branch in Git is simply a lightweight movable pointer to one of these commits." Because a branch is only a label and not a copy of your files, creating one costs nothing, and throwing one away loses nothing you did not mean to lose. That is exactly why branches are the main safety tool when an AI agent edits your code.

What a Git branch is: a label, not a folder

Many people picture a branch as a duplicate of the project in another folder. It is not. Git records your history as a chain of commits. A branch is a name attached to one commit in that chain. When you make a new commit on the branch, the label moves forward to it.

Git also keeps a special pointer called HEAD, which the book describes as "a pointer to the local branch you're currently on." HEAD answers one question: where will my next commit go?

So two branches can share all their history and differ only in the last few commits. That is cheap, quick and easy to undo.

The main branch

Every repository has a first branch. The Pro Git book notes that the default name is master, and says it "is not a special branch. It is exactly like any other branch." Many projects and hosting sites now name theirs main. To see which branch you are on, run:

git branch --show-current

The convention is to keep the main branch in a working state and do new work somewhere else. Why that matters for agents is covered in stopping an AI coding agent from committing to main.

Six commands, in the order you will use them

  1. See your branches: git branch lists them and marks the current one.

  2. Create a branch: git branch ai-fix-login makes a new label on the commit you are on. The book is explicit that this does not move you: "The git branch command only _created_ a new branch."

  3. Switch to it: git switch ai-fix-login moves HEAD to that branch. This command arrived in Git 2.23, and the older git checkout ai-fix-login does the same job.

  4. Create and switch at once: git switch -c ai-fix-login is the shortcut you will use most.

  5. Go back: git switch - returns you to the branch you were on before.

  6. Bring the work in: switch to the main branch and run git merge ai-fix-login. On a hosting site you do the same with a pull request.

If the merge reports a conflict because two branches changed the same lines, handling merge conflicts with AI coding agents walks through what to do.

Why branches suit AI agents

An agent can make dozens of edits in a few minutes. If those land on your main branch, undoing them means untangling a mixed history. If they land on a branch you created for the task, you have three simple outcomes:

  • Good result: merge the branch.

  • Mixed result: merge part of it, or ask the agent to continue.

  • Bad result: delete the branch and start again. Your main branch never changed.

That last option is the point. A branch turns "the agent made a mess" from a repair job into a delete.

The rule to give the agent

Put this in the instructions you give your tool at the start of a task:

Before changing anything, create a new branch named for this task.
Make all commits on that branch. Do not commit to main.
Do not delete any branch. Tell me the branch name when you finish.

Then check the result before merging. Reviewing an AI agent's git diff before merging shows what to look at, and keeping an AI coding agent inside one feature branch covers what to do when it wanders. If you host on GitHub, branch protection for an AI coding agent turns the rule into something the system enforces. For the wider picture, Git versus GitHub explains which of these actions happen only on your machine.

Common branch mistakes

  • Working on the wrong branch. Run git branch --show-current before you start.

  • Uncommitted changes when switching. Git may refuse to switch, or carry changes across. Commit or stash first.

  • Never merging. A branch that is not merged does not exist for your live site.

  • Branches that live for weeks. The longer a branch stays separate, the harder it is to merge. Keep tasks small.

FAQ

Does creating a branch copy my files?

No. It creates a new label pointing at the commit you are on. Your files stay as they are.

What is the difference between git switch and git checkout?

Both can move you between branches. git switch was added in Git 2.23 to do only that, while git checkout does several jobs.

How do I delete a branch?

Run git branch -d name once it has been merged. Git will warn you if the branch holds work that has not been merged.

Should an AI agent work on its own branch?

Yes. It keeps the main branch safe and lets you discard the whole attempt in one step.

How did this land?

About the author

Steve Jefferson
Steve Jefferson

Developer Advocate

Steve builds something with Swarmz every week and writes up what worked, what broke, and what he'd do differently. Tutorials and hands-on guides are his lane.

Share

Get the next post in your inbox

One email a month. Product updates, engineering posts, and the best of Built with Swarmz.

I agree to receive emails about AI building tips and Swarmz product news. Unsubscribe any time.