TracksPractical Coding FoundationsVersion ControlWhat Is a Branch?(8 of 12)

What Is a Branch?

A branch is a parallel line of development in your project. It lets you work on new features, fix bugs, or experiment — all without touching your main code. When you're happy with your changes, you can merge them back. If the experiment fails, you can abandon the branch without any damage.

Why Branches Matter

Imagine you're adding a new feature to a working application. Without branches, every change you make affects the main code immediately. If something breaks, your whole project is broken until you fix it.

With branches, you create a separate workspace. Your main code stays stable while you experiment. It's like saving your game before attempting a difficult level — you can always go back.

The Default Branch

When you initialize a repository, Git creates a default branch, usually called main (older projects might use master). This is typically where your stable, working code lives.

Viewing Branches

See all branches in your repository:

The asterisk shows which branch you're currently on.

Creating a Branch

Create a new branch with:

This creates the branch but doesn't switch to it. You're still on main.

Switching Branches

Move to your new branch with checkout:

Now any commits you make will be on feature-login, not main.

Create and Switch in One Step

The most common workflow is creating a branch and immediately switching to it:

The -b flag means "create this branch, then switch to it."

Branch Naming

Use descriptive names that explain what the branch is for:

  • feature-user-auth — adding user authentication
  • fix-login-crash — fixing a specific bug
  • experiment-new-layout — trying something that might not work

Avoid spaces and special characters. Hyphens work well as separators.

Branches Are Cheap

Creating branches in Git is fast and uses almost no extra storage. Don't hesitate to create branches for any significant piece of work. It's much easier to merge a clean branch than to untangle mixed-up commits on main.

See More

Further Reading

You need to be signed in to leave a comment and join the discussion