Live streamJoin us for "State of code review 2024" on July 18thRegister today

How to create a new branch in Git

Kenny DuMez
Kenny DuMez
Graphite software engineer


This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.

A branch in Git allows you to diverge from the main line of development and work on new features or fixes without affecting the main codebase. This guide provides a step-by-step approach to creating new branches in Git, covering everything from basic commands to more specific scenarios like branching from the main branch.

In Git, a branch represents an independent line of development. Branches serve as an abstraction for the edit/stage/commit process. You can think of them as a way to request a brand new working directory, staging area, and project history. New commits are recorded in the history of the current branch, which results in a fork in the history of the project.

Under the hood each branch is a lightweight, movable pointer to one of these commits. When you create a new branch, Git simply creates a new pointer; it doesn't duplicate any files or directories. Instead, Git's commit graph structure allows branches to share common history up to their point of divergence, where each branch can then record its own subsequent commits independently, effectively creating forks in the project's history without duplicating the underlying data.

First, it's a good practice to know which branch you're currently on. You can do this by using the git status command, which will also tell you if your working directory is clean.

git status

If there are uncommitted changes, decide if you want to commit them to your current branch or leave them to commit on the new branch.

To create a new branch, use the git branch command followed by the name of the new branch. For example, to create a branch named feature-x, you would run:

git branch feature-x

This command creates a new branch but does not switch to it automatically.

After creating the new branch, you need to switch to it to start working on that branch. Use the git checkout command to switch branches:

git checkout feature-x

Alternatively, you can create and switch to the new branch in one step with:

git checkout -b feature-x

If you need to create a new branch from a specific branch, such as the main branch, first switch to that branch, then create your new branch:

git checkout main
git checkout -b feature-from-main

This sequence ensures that your new branch starts from the main branch, inheriting its current state.

  • Name branches clearly: Use descriptive names for your branches (e.g., feature-add-login, bugfix-header-crash), so it's clear what work is being done on each branch. See this guide on Git branch naming conventions for more information.
  • Keep branches short-lived: Merge branches back into the main branch as soon as the feature or fix is complete to avoid a more complex merge later.
  • Regularly pull changes: If your main branch is active, regularly pull changes from main to your feature branches to keep them up-to-date and minimize merge conflicts.
  • git branch <branch-name>: Creates a new branch.
  • git checkout <branch-name>: Switches to the specified branch.
  • git checkout -b <branch-name>: Creates a new branch and switches to it.
  • git checkout main: Switches to the main branch.

For further reading on creating new branches in Git see the official Git documentation.

Stay unblocked. Ship faster.
Experience the new developer workflow - create, review, and merge code continuously. Get started with one command.
Get started

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2