Skip to content

/errors/git/a-branch-named-already-exists

fatal: a branch named 'feature' already exists

verified onenvironmentlocal · linux 6.18.44-fc-v140 x64 · GNU bash 5.2.21 · git version 2.43.0updated
tool
Git
exit code
128
versions
Git 2.x
os
linux, macos, windows

fatal: a branch named 'feature' already exists means a local branch with that name is already in the repository, so git branch feature and git switch -c feature refuse to create it. Move to it with git switch feature, or choose another name. The command exits with status 128.

Reproduce it

bash
git init -q -b main demo && cd demo
git -c user.name=demo -c user.email=demo@example.com commit -q --allow-empty -m "Initial commit"
git branch feature
git branch feature

Output · exit 128

fatal: a branch named 'feature' already exists

Fixes

bash
git init -q -b main demo && cd demo
git -c user.name=demo -c user.email=demo@example.com commit -q --allow-empty -m "Initial commit"
git branch feature
git switch -q feature
git branch --show-current
git switch -q main
git branch -f feature main
git switch -q -C feature main
git branch --show-current

Output

feature
feature
GoalCommand
Use the existing branchgit switch feature
Create a new branch with another namegit switch -c feature-2
Reset the branch to a start point and switch to itgit switch -C feature main
Reset the branch without switchinggit branch -f feature main
Delete the old branch firstgit branch -d feature

The branch may also exist only on the remote. In that case git switch feature creates a local branch that tracks origin/feature; list branches with git branch -a.

Sources