Skip to content

git branch

Creating and managing branches with git branch — isolating features and parallel development.

What you'll learn

  • How to create, list, and delete branches
  • Why branching is Git's killer feature
  • The difference between git branch and git checkout -b
  • Renaming and tracking branches

Concept

Why Branching Matters

Branching lets you diverge from the main line of development and work on something independently — a new feature, a bug fix, an experiment — without affecting the stable code. This is Git's most powerful feature.

git branch feature-login

This creates a new branch, but does not switch to it. To create and switch in one step, use git checkout -b or git switch -c.

Listing Branches

git branch              # list local branches (* marks current)
git branch -r           # list remote branches
git branch -a           # list all branches (local + remote)
git branch -v           # show last commit on each branch
git branch -vv          # show tracking info too

The asterisk (*) next to a branch name indicates the branch you are currently on.

Creating and Switching

# Create a branch (stays on current branch)
git branch feature-search

# Switch to it
git checkout feature-search
# or with the newer command:
git switch feature-search

# Create and switch in one step
git checkout -b feature-search
# or:
git switch -c feature-search

Deleting Branches

# Delete a fully-merged branch
git branch -d feature-login

# Force delete even if not merged (use with caution!)
git branch -D feature-login

-d refuses to delete a branch that has unmerged changes, protecting you from losing work. -D forces deletion regardless — only use it when you are sure.

Renaming a Branch

# Rename the current branch
git branch -m new-name

# Rename a specific branch
git branch -m old-name new-name

Branch Naming Conventions

Good branch names are descriptive and follow a pattern:

  • feature/user-authentication
  • bugfix/login-redirect
  • hotfix/security-patch
  • chore/update-dependencies
  • docs/api-reference

This makes it clear what each branch is for and enables automation (like auto-generating changelogs).

Lightweight Branches

Git branches are just pointers to a commit — they are incredibly lightweight. Creating a branch takes milliseconds and uses almost no disk space. Unlike SVN, where branching copies an entire directory, Git branches cost nothing. Create branches freely.

Example

              # List all branches — * marks the current branch
git branch
# * main
#   feature-login

# Create a new branch for a feature
git branch feature-search

# Switch to the new branch
git checkout feature-search
# or: git switch feature-search

# Create and switch in one step (most common)
git checkout -b feature-payment
# or: git switch -c feature-payment

# Make some changes on the feature branch
echo "payment logic" > payment.js
git add payment.js
git commit -m "feat: add payment module"

# Switch back to main
git checkout main

# List branches with last commit info
git branch -v

# Rename the current branch
git branch -m feature-payments

# Delete a merged branch
git checkout main
git branch -d feature-search

# Force delete an unmerged branch (caution!)
git branch -D experimental-branch

# List remote branches too
git branch -a
            

Covers listing, creating, switching, renaming, and deleting branches. The checkout -b (or switch -c) pattern for creating and switching in one step is the most commonly used command. Note the -d vs -D distinction for safe vs forced deletion.

Try it

Common mistakes

The mistake

Creating a branch but forgetting to switch to it before making changes

The fix

git branch <name> creates the branch but stays on the current one. Use git checkout -b <name> (or git switch -c <name>) to create AND switch in one step. Always check git branch to confirm which branch you are on before editing.

The mistake

Using -D (force delete) instead of -d and losing unmerged work

The fix

git branch -d refuses to delete branches with unmerged changes, protecting your work. Only use -D when you are absolutely sure the branch's work is not needed. If you accidentally deleted a branch, you can often recover it with git reflog.

Related Cheatsheets

Practice git branch

11 exercises

Practice git branch

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors