How to fix fatal: A branch named 'feature' already exists in Git

GitBEGINNERLOWUpdated Dec 6, 2025ยท 264 views

The 'fatal: A branch named already exists' error occurs when you try to create a branch with a name that already exists in your local repository. Switch to the existing branch, use a different name, or reset the branch to a new starting point.

Quick fix

Fixes fatal: A branch named 'feature' already exists

# Modern Git (2.23+)
git switch feature

# Or using checkout
git checkout feature
  1. Switch to the existing branch instead
  2. List existing branches to confirm
  3. Use a different branch name
  4. Delete the existing branch first
git โ€” error
# Modern Git (2.23+)
git switch feature
# Or using checkout
git checkout feature
fatal: A branch named 'feature' already exists

What this error means

The "fatal: A branch named 'X' already exists" error in Git is a local name collision. It happens when you attempt to create a new branch using `git checkout -b` or `git branch`, but a branch with that exact name already exists in your local repository. Git branch names must be unique within a repository. When you run `git checkout -b feature`, Git tries to create a new branch called "feature" and then switch to it. If "feature" already exists locally, Git refuses to overwrite it and shows this fatal error. This error is strictly about local branches. The branch may or may not exist on the remote - this error only triggers when there's a conflict with your local branch names. Common scenarios include trying to create a branch you've already worked on, or attempting to track a remote branch when you already have a local branch with the same name.

Typical symptoms

  • Git command fails with 'fatal: A branch named ... already exists'
  • Running git checkout -b <branch-name> produces the error
  • Cannot create a new branch with a name you've used before
  • Trying to check out a remote branch fails because a local branch has the same name
  • CI/CD pipeline fails when attempting to create a branch that already exists
  • Git worktree add fails due to branch name collision

Common causes

  1. You already created a local branch with that name in a previous session
  2. A branch with that name exists from earlier work but was never deleted
  3. Attempting to use git checkout -b when you meant to switch to an existing branch
  4. A stale local branch remains after the remote branch was deleted and recreated
  5. Branch was created in another worktree or by another tool
  6. Automated scripts creating branches without checking if they exist first

How to fix "fatal: A branch named 'feature' already exists"

1Switch to the existing branch instead

If you simply want to work on the branch that already exists, switch to it without the -b flag:

bash
# Modern Git (2.23+)
git switch feature

# Or using checkout
git checkout feature

The -b flag tells Git to create a new branch. If the branch already exists, you don't need it - just switch directly.

2List existing branches to confirm

Check what branches exist locally to understand the situation:

bash
# List local branches
git branch

# List all branches (local and remote)
git branch -a

# List branches with last commit info
git branch -v

Look for your branch name in the output. If it's there, you can switch to it or decide whether to delete it.

3Use a different branch name

If you need a new branch and want to keep the existing one, use a different name:

bash
# Use a more specific name
git checkout -b feature-v2
git checkout -b feature-login-refactor
git checkout -b feature-2024-12

# Or append a number/date
git checkout -b feature-$(date +%Y%m%d)

Consider adopting a naming convention that includes ticket numbers or dates to avoid collisions.

4Delete the existing branch first

If you don't need the existing branch, delete it before creating the new one:

bash
# Safe delete (only if merged)
git branch -d feature

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

# Then create your new branch
git checkout -b feature

Warning: -D (force delete) will permanently remove the branch and any unmerged commits. Make sure you don't need those commits first.

5Reset the branch to a new starting point with -B flag

Use the uppercase -B flag to reset an existing branch to a new starting point:

bash
# Reset 'feature' to current HEAD and switch to it
git checkout -B feature

# Reset 'feature' to a specific commit/branch
git checkout -B feature main
git checkout -B feature origin/main

# Modern equivalent with git switch
git switch -C feature
git switch -C feature main

The -B flag creates the branch if it doesn't exist, or resets it if it does. This is useful when you want to start fresh from a different point.

6Track a remote branch with the same name

If you want to track a remote branch that has the same name as an existing local branch:

bash
# Option 1: Delete local and checkout fresh from remote
git branch -D feature
git checkout feature  # Will auto-track origin/feature

# Option 2: Reset local branch to match remote
git checkout feature
git reset --hard origin/feature

# Option 3: Use -B to reset and track
git checkout -B feature origin/feature

This is common when a remote branch was deleted and recreated, but your local branch still references the old version.

Advanced notes

### Understanding Branch Creation Commands

Git has multiple ways to create branches, each with different behavior:

| Command | Behavior |
|---------|----------|
| git branch <name> | Creates branch, stays on current branch |
| git checkout -b <name> | Creates branch and switches to it |
| git checkout -B <name> | Creates or resets branch and switches |
| git switch -c <name> | Creates branch and switches (modern) |
| git switch -C <name> | Creates or resets branch and switches |

### Creating an Alias for "Switch or Create"

You can create a Git alias that switches to a branch if it exists, or creates it if it doesn't:

bash
git config --global alias.switchoc '!f() { git switch "$1" 2>/dev/null || git switch -c "$1"; }; f'

# Usage
git switchoc feature  # Switches if exists, creates if not

### Branch Name Conflicts with Worktrees

If you use Git worktrees, a branch can only be checked out in one worktree at a time:

bash
# List worktrees to find where branch is checked out
git worktree list

# Remove a worktree if needed
git worktree remove /path/to/worktree

### Handling Stale Remote Tracking Branches

If remote branches were deleted but local tracking refs remain:

bash
# Prune stale remote tracking branches
git fetch --prune

# Or set it to always prune
git config --global fetch.prune true

### In CI/CD Pipelines

To safely handle branch creation in scripts:

bash
# Check if branch exists before creating
if git show-ref --verify --quiet refs/heads/feature; then
  git checkout feature
else
  git checkout -b feature
fi

# Or use -B to always reset
git checkout -B feature origin/main

Frequently asked questions

What does "fatal: A branch named 'feature' already exists" mean?
The "fatal: A branch named 'X' already exists" error in Git is a local name collision. It happens when you attempt to create a new branch using git checkout -b or git branch, but a branch with that exact name already exists in your local repository. Git branch names must be unique within a repository.
How do I fix "fatal: A branch named 'feature' already exists"?
The 'fatal: A branch named already exists' error occurs when you try to create a branch with a name that already exists in your local repository. Switch to the existing branch, use a different name, or reset the branch to a new starting point.
What causes "fatal: A branch named 'feature' already exists"?
You already created a local branch with that name in a previous session A branch with that name exists from earlier work but was never deleted Attempting to use git checkout -b when you meant to switch to an existing branch
Is the "fatal: A branch named 'feature' already exists" error serious?
It is a low-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading