What are branches?
A branch in Git is a lightweight pointer to a specific commit. When you create a branch, Git does not copy any files — it simply creates a new pointer. This makes branching nearly instantaneous, even in large repositories. Think of branches as parallel timelines: the main branch is your stable timeline, and feature branches are alternate timelines where you build and test without affecting production code.
Every Git repository starts with a single branch (usually main or master). From there, the branching model you adopt determines how your team collaborates. The rest of this page covers every command you need to master that workflow.
Create and switch branches
Three commands let you create and navigate branches. git branch manages branches, git switch moves between them, and the legacy git checkout does both (and more).
git branch
git branch is the core command for managing branches. Run it without arguments to list local branches, add a name to create one, or use -d to delete a branch that has been merged. It never switches your working directory — it only manages pointers.
git switch
Introduced in Git 2.23, git switch is the dedicated command for changing branches. Use git switch -c name to create and switch in one step. It is clearer than checkout because it does one thing: move between branches.
git checkout (legacy)
Before Git 2.23, git checkout was the only way to switch branches. It still works and you will see it in older tutorials, but it also restores files and detaches HEAD — making it easy to misuse. For branch switching, prefer git switch.
Merge strategies
Once your feature branch is ready, you need to integrate it back. Git offers three main strategies: merge for a faithful history, rebase for a linear history, and cherry-pick for grabbing individual commits.
git merge
git merge integrates one branch into another. If the branches have diverged, Git creates a merge commit with two parents — preserving the complete history. If no divergence exists, Git performs a fast-forward merge (no extra commit needed).
git rebase
git rebase takes every commit on your branch and replays it on top of another branch. The result is a perfectly linear history with no merge commits. It is powerful but rewrites commit hashes — so never rebase commits that have been pushed to a shared branch.
git cherry-pick
git cherry-pick copies a single commit from one branch to another. It does not merge or rebase — it creates a new commit with the same changes. Use it when you need a specific fix without pulling in an entire branch.
Merge vs Rebase: which to choose?
This is one of the most common Git debates. Use the decision cards below to pick the right strategy, then check the comparison table further down for a side-by-side overview.
You want a complete history
The branch has been pushed or shared with others. You want every contributor's work to remain visible in the log. Merge commits act as milestones that show when features were integrated.
You want a clean, linear history
The branch is local and has not been pushed. You want a straight line of commits without merge nodes. Rebase keeps the log easy to read and makes git log --oneline a pleasure.
You need a single commit
A critical fix is on another branch and you only need that one commit. Cherry-pick avoids pulling in unrelated work. Ideal for hotfixes and backports.
The branch is shared
If you have already pushed your branch and others are working on it, rebasing rewrites history and creates diverging commits. This leads to confusion and duplicate work. Use merge instead.
Handle merge conflicts
A merge conflict occurs when two branches modify the same line of the same file. Git pauses and marks the file with <<<<<<<, =======, >>>>>>> markers. Open the file, choose the version you want (or combine both), remove the markers, then stage and commit. For a detailed walkthrough, see our guide on resolving merge conflicts.
Branch naming conventions
Consistent branch names make your repository easier to navigate. Most teams use a prefix/description pattern. Here are the most common prefixes:
| Prefix | Purpose | Example |
|---|---|---|
| feature/ | New feature or enhancement | feature/user-auth |
| bugfix/ | Bug fix (non-urgent) | bugfix/login-redirect |
| hotfix/ | Urgent production fix | hotfix/security-patch |
| release/ | Release preparation | release/v2.1.0 |
| chore/ | Maintenance or tooling | chore/update-deps |
Use lowercase letters, hyphens instead of spaces, and keep names short but descriptive. A good branch name tells your teammates what you are working on at a glance.