Back
GitoryxGitoryx

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

On this page

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

  1. 1List branches: git branch
  2. 2Create and switch: git switch -c feature/my-feature
  3. 3Switch to existing: git switch main
  4. 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 main instead 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

  1. Step 1: List your branches

    See all local branches. The * marks your current branch.

    Terminal
    git branch# * main#   feature/login#   bugfix/header-typo

    Tip: Use git branch -a to list remote tracking branches too.

  2. Step 2: Create a new branch

    This creates the branch but does NOT switch to it.

    Terminal
    git branch feature/user-auth
  3. Step 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-auth
  4. Step 4: Switch to an existing branch

    Move to a branch that already exists.

    Terminal
    git switch feature/user-auth# or:git checkout feature/user-auth

    Careful: 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
  5. 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-name
  6. Step 6: Delete a branch

    Clean up branches that have been merged. Use -d for a safe delete (fails if unmerged), -D to 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-color cost nothing.
Not pulling before creating a branch
If your local main is behind the remote, your new branch starts from outdated code.
Fix: Run git pull origin main before 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> (or git 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 with remotes/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.
All Git tutorials →
Gitoryx

Stop fighting Git. Start shipping.

Download the fast, native Git client that developers actually enjoy using.

v1.1.0 · macOS 12+ (Apple Silicon or Intel) · Windows 10+ · Linux x86_64