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.
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.
If you simply want to work on the branch that already exists, switch to it without the -b flag:
# Modern Git (2.23+)
git switch feature
# Or using checkout
git checkout featureThe -b flag tells Git to create a new branch. If the branch already exists, you don't need it - just switch directly.
Check what branches exist locally to understand the situation:
# List local branches
git branch
# List all branches (local and remote)
git branch -a
# List branches with last commit info
git branch -vLook for your branch name in the output. If it's there, you can switch to it or decide whether to delete it.
If you need a new branch and want to keep the existing one, use a different name:
# 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.
If you don't need the existing branch, delete it before creating the new one:
# 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 featureWarning: -D (force delete) will permanently remove the branch and any unmerged commits. Make sure you don't need those commits first.
Use the uppercase -B flag to reset an existing branch to a new starting point:
# 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 mainThe -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.
If you want to track a remote branch that has the same name as an existing local branch:
# 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/featureThis is common when a remote branch was deleted and recreated, but your local branch still references the old version.
### 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:
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:
# 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:
# 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:
# 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/mainwarning: BOM detected in file, this may cause issues
UTF-8 Byte Order Mark (BOM) detected in file
fatal: Server does not support --shallow-exclude
Server does not support --shallow-exclude
warning: filtering out blobs larger than limit
Git partial clone filtering large blobs warning
fatal: Server does not support --shallow-since
Server does not support --shallow-since in Git
kex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server