How To Create a Git Branch: A Simple Step‑by‑Step Guide
Creating a Git branch is one of the most useful skills you can learn if you work with code, documentation, or any files tracked in Git. Branches let you experiment, add features, or fix bugs without touching your main code until you’re ready.
This guide walks through what a Git branch is, how branching works, and the common ways to create and use branches, from the command line to graphical tools.
What Is a Git Branch?
A Git branch is basically a named pointer to a series of commits.
Think of your project history as a timeline:
- Each commit is a snapshot of your files at a point in time.
- A branch is a label (like main, develop, or feature/login) that points to the latest commit in one line of work.
When you:
- Create a new branch, Git makes a new label pointing to the current commit.
- Switch to that branch, your working files update to reflect that branch’s version.
- Commit on that branch, the branch pointer moves forward along the new line of history.
The big win: you can have multiple lines of work (branches) happening in parallel without interfering with each other.
Common use cases:
- Adding a new feature (feature/payment-flow)
- Fixing a bug (bugfix/typo-header)
- Trying a risky experiment you might later throw away
Basic Branch Commands You’ll Use
Here are the core Git commands related to branches:
List branches
git branchCreate a new branch (without switching)
git branch new-branch-nameCreate and switch in one step (most common)
git checkout -b new-branch-nameor on newer Git:
git switch -c new-branch-nameSwitch to an existing branch
git checkout branch-nameor:
git switch branch-nameDelete a local branch
git branch -d branch-name # safe delete (refuses if not merged) git branch -D branch-name # force deletePush a new local branch to a remote (e.g. GitHub, GitLab)
git push -u origin branch-name