Git Branching: Create, Switch, and Manage Branches

Boot.dev Blog » DevOps » Git Branching: Create, Switch, and Manage Branches
ThePrimeagen
ThePrimeagen Ex-Netflix engineer, NeoVim ricer, and Git rebaser

Last published March 31, 2026

Table of Contents

A Git branch allows you to keep track of different changes separately. For example, you can create a new branch to experiment with changing a color scheme without affecting your primary branch. If you like the changes, you merge (or rebase) the branch back into main. If you don’t, you delete it.

All the content from our Boot.dev courses are available for free here on the blog. This one is the “Branching” chapter of Learn Git. If you want to try the far more immersive version of the course, do check it out!

What Is a Branch Under the Hood?

A branch is just a named pointer to a specific commit. The commit that the branch points to is called the tip of the branch.

commits and branches

Because a branch is just a pointer to a commit, they’re lightweight and “cheap” resource-wise to create. When you create 10 branches, you’re not creating 10 copies of your project on your hard drive.

You can check which branch you’re currently on by running:

git branch

How Do You Visualize Branches?

Throughout our discussions of Git, you’ll see text diagrams like this:

A - B - C  main

This represents a branch called main with 3 commits. C is the most recent commit (the tip), B is the previous commit, and A is the commit before that. Multiple branches look like this:

       G - H    lanes_branch
      /
A - B - C - D   main
  \
   E - F        primes_branch

All three branches share commits A and B. After that they diverge. main continues to C and D, while lanes_branch splits off from B and has its own commits G and H.

You can get a similar view from Git itself with:

git log --oneline --graph --all

What Is the Default Branch?

Git’s built-in default branch is master. GitHub (somewhat, okay I’m old…) recently changed its default to main. As a general rule, I recommend using main as your default branch if you work primarily with GitHub.

You can change the global default:

git config set --global init.defaultBranch main

But that only affects new repositories. To rename an existing branch:

git branch -m master main

How Do You Create a New Branch?

Two ways:

git branch my_new_branch

This creates a new branch but doesn’t switch to it. I rarely use this command because usually I want to create a branch and switch to it immediately:

git switch -c my_new_branch

The git switch command allows you to switch branches. Including the -c flag tells Git to create a new branch and switch to it.

When you create a new branch, it uses the current commit you are on as the branch base. So if you’re on main with commits A, B, and C, your new branch starts pointing at C too:

branch diagram same commits

How Do You Switch Between Branches?

Use git switch to change branches:

git switch main

There’s an older command that does the same thing: git checkout. git switch is newer and designed to be more intuitive. It’s recommended to use git switch over git checkout for simply switching branches:

git switch main

git checkout main

How Do Commits Work on a Branch?

When you commit on a branch, only that branch’s pointer moves forward. The other branches stay where they are.

          D    add_classics
         /
A - B - C      main

The add_classics branch has all the commits from main, plus a new commit D. The main branch hasn’t moved — it still points at C.

What Are Useful git log Flags?

git log has some flags that make the output much easier to read:

--oneline shows a compact view. I use this one all the time:

git log --oneline

--decorate controls how branch names are displayed. It can be short (the default), full (shows the full ref name), or no (no decoration). As of Git 2.12.2, --decorate is applied automatically.

git log --oneline --decorate=full

Where Are Branches Stored on Disk?

Git stores all its information in the .git subdirectory, including branches. The “heads” (or “tips”) of branches are stored in .git/refs/heads. If you cat one of the files in that directory, you’ll see the commit hash that the branch points to.

cat .git/refs/heads/main

If you want to go deeper into how Git stores objects, see Git Internals. For the next step — actually combining branches — Learn Git 2 covers merging, rebasing, and conflict resolution.

Frequently Asked Questions

What is a Git branch?

A branch is just a named pointer to a specific commit. The commit that the branch points to is called the tip of the branch. Branches are lightweight and cheap to create because they are not copies of your project.


What is the difference between git switch and git checkout?

git switch is a newer command meant to be more intuitive for switching branches. git checkout is older and does the same thing but also has other uses. It is recommended to use git switch for simply switching branches.


How do I create a new branch and switch to it?

Use git switch -c my_new_branch. The -c flag tells Git to create the branch and switch to it immediately.


What is the default branch in Git?

Git's built-in default is master. GitHub changed its default to main. You can set your preferred default with git config set --global init.defaultBranch main.


Where are branches stored on disk?

Branch tips are stored as files in .git/refs/heads. Each file contains the commit hash that the branch points to.