GitCreating Branches (git branch)

Creating Branches (git branch)

Creating a branch in Git is one of the fastest, lowest-risk operations you will ever do. There are several commands that can create branches; they all produce the same result — a movable pointer to a commit — but each has a slightly different ergonomic feel.

The classic way: git branch

Create without switching

Bash
git branch feature-login
# A new branch is created at the current commit
# You are STILL on the previous branch — git branch doesn't switch
The modern way: git switch -c

Create AND switch in one command

Bash
git switch -c feature-login
# Equivalent to:
# git branch feature-login
# git switch feature-login

-c (for create) is what you’ll use 95% of the time. Most of the time you create a branch because you want to start working on it.

The classic combo: git checkout -b

Same idea, older spelling

Bash
git checkout -b feature-login
# Predates 'git switch' — does the exact same thing
Use git switch if you have it
`git switch` was added in Git 2.23 (2019) specifically to separate “change branch” from `git checkout`’s many other meanings. Modern tutorials and IDEs prefer `git switch`. Either spelling works.
Branching from somewhere else

Start a branch at a specific commit or branch

Bash
# Branch off another branch (not the one you're on)
git switch -c hotfix/v1.2 main

# Branch off a specific commit
git switch -c old-investigation 1f9ab2c

# Branch off a tag
git switch -c v2.0-fixes v2.0

# Branch off a remote branch
git switch -c local-feature origin/feature-x
What happens when you create a branch
  • Git writes a new file in .git/refs/heads/ named after the branch, containing the starting commit hash.

  • No files are copied. Your working directory does not change. (Unless you also switch.)

  • The new branch points to the same commit as wherever you were branching off.

  • Until you commit, both branches are pointing at the same commit — they only diverge once one of them moves.

Confirming the branch was created

Bash
git branch
#   feature-login
# * main             ← * marks the currently checked-out branch

git switch feature-login
git branch
# * feature-login
#   main
Choosing a good branch name
  • Use a short, descriptive name: fix-login-typo, not fix.

  • Use hyphens or slashes between words; spaces are not allowed.

  • Many teams use prefixes like feature/, fix/, hotfix/, chore/, refactor/.

  • Keep names URL-safe — branches show up in PR URLs.

  • See the “Branch Naming Conventions” page for fuller guidance.

Creating a branch from a stash

Recover stashed work onto a new branch

Bash
git stash branch feature-from-stash
# Creates a new branch at the commit where you stashed
# Applies the stash and drops it
Branch from a remote

Track a remote branch locally

Bash
git fetch origin                           # update remote-tracking refs
git switch feature-x                       # Git auto-creates local branch tracking origin/feature-x
# Equivalent explicit form:
git switch -c feature-x --track origin/feature-x
Throwaway branches

Branches are cheap enough that you should create them for the smallest experiments. There is zero penalty for branches you never push.

Bash
# I want to try something risky
git switch -c try-something-wild
# ... experiment ...

# Did it work?
# YES: keep it, maybe rebase or rename
git switch main
git merge try-something-wild

# NO: throw it away
git switch main
git branch -D try-something-wild
Creating without affecting your current branch

Make a branch but stay where you are

Bash
git branch new-name HEAD~3
# Created at HEAD~3, but you're still on your current branch
Verifying the starting point

Bash
git log --oneline --decorate -5
# 1f9ab2c (HEAD -> main, feature-login) Latest commit
# d4b1e0c Earlier commit
# c204c1d ...
Tip
Make the rhythm git switch -c <branch> at the start of every new piece of work. It costs nothing and gives you a clean lane to work in.