Git Branch: Create, Switch, and Manage Branches
Branches are the foundation of every Git workflow. This tutorial covers creating and switching branches, understanding HEAD, deleting stale branches, and working with remote branches, all with practical examples.
Beginner9 min readUpdated
What is a Git branch?
A Git branch is a lightweight, movable pointer to a specific commit. Creating a branch doesn't copy files: it just creates a new pointer that advances independently as you commit. The default branch is usually called main or master.
The short version
- 1List branches:
git branch - 2Create and switch:
git switch -c feature/my-feature - 3Switch to existing:
git switch main - 4Delete merged branch:
git branch -d feature/my-feature
Why it matters
You've been committing directly to main and now two features are tangled together. Or you're afraid to experiment because any mistake will affect the working code. Branches solve both of these problems.
Branches are the core of any Git workflow. They let you develop features, fix bugs, and experiment in complete isolation, without touching the stable codebase. Every pull request starts with a branch.
Where it usually goes wrong
- Working directly on
maininstead of a feature branch - Forgetting to switch branches before starting new work
- Leaving dozens of stale merged branches that clutter the repo
- Confusing local branches with remote tracking branches
Git branch, step by step
Step 1: List your branches
See all local branches. The
*marks your current branch.Terminalgit branch# * main# feature/login# bugfix/header-typoTip: Use
git branch -ato list remote tracking branches too.Step 2: Create a new branch
This creates the branch but does NOT switch to it.
Terminalgit branch feature/user-authStep 3: Create a branch and switch to it immediately
This is the most common pattern: create and switch in one command.
Terminal# Modern Git (2.23+)git switch -c feature/user-auth# Classic syntax (still works everywhere)git checkout -b feature/user-authStep 4: Switch to an existing branch
Move to a branch that already exists.
Terminalgit switch feature/user-auth# or:git checkout feature/user-authCareful: Make sure to commit or stash your current changes before switching, since Git may refuse to switch if there are conflicts with the target branch.
In Gitoryx: All branches shown in the sidebar with their latest commitHow it works Step 5: Rename a branch
Rename your current branch or a different local branch.
Terminal# Rename the current branchgit branch -m new-name# Rename a specific branchgit branch -m old-name new-nameStep 6: Delete a branch
Clean up branches that have been merged. Use
-dfor a safe delete (fails if unmerged),-Dto force.Terminal# Safe delete (only if merged)git branch -d feature/user-auth# Force deletegit branch -D feature/user-auth# Delete a remote branchgit push origin --delete feature/user-auth
Mistakes to avoid with git branch
- Committing to `main` directly
- This makes it impossible to open a proper pull request and risks pushing unstable code to the shared branch.
- Fix: Always create a new branch for any change, even a one-line fix. Branch names like
fix/button-colorcost nothing. - Not pulling before creating a branch
- If your local
mainis behind the remote, your new branch starts from outdated code. - Fix: Run
git pull origin mainbefore branching to make sure you're starting from the latest code. - Accumulating stale branches
- After merging a PR, the feature branch lingers indefinitely. Over time the repo fills with dozens of dead branches.
- Fix: Delete branches after merging. GitHub and GitLab can auto-delete branches when a PR is merged.
Git branch in Gitoryx
Gitoryx shows all your branches in the sidebar and the commit graph simultaneously. Creating, switching, and deleting branches is a single right-click.
- All branches shown in the sidebar with their latest commit
- Create or switch branches with a single right-click in the graph
- See diverged branches at a glance with the visual commit graph
- Delete merged branches in bulk from the branch manager
Free plan · no account required · macOS, Windows and Linux
Git branch: frequently asked questions
- What is the difference between `git branch` and `git checkout -b`?
git branch <name>creates a branch but stays on the current branch.git checkout -b <name>(orgit switch -c <name>) creates a branch and immediately switches to it. The latter is what you usually want.- How do I see all branches including remote ones?
- Run
git branch -a. Local branches appear in white; remote tracking branches appear in red (or prefixed withremotes/origin/). - What happens when I delete a branch?
- Deleting a branch only removes the pointer: the commits themselves remain in the repository until Git garbage-collects them. As long as the branch was merged, the history is preserved in the target branch.
- Can I work on multiple branches at the same time?
- Not in the same working directory. You can only have one branch checked out at a time. However, Git worktrees let you check out multiple branches simultaneously in separate folders.
Related reading
- Git Branching Strategy: A Complete GuideTutorial · 14 min
- How to Resolve Merge Conflicts in GitTutorial · 12 min