/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
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 featureOutput · exit 128
fatal: a branch named 'feature' already exists
Fixes
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-currentOutput
feature feature
| Goal | Command |
|---|---|
| Use the existing branch | git switch feature |
| Create a new branch with another name | git switch -c feature-2 |
| Reset the branch to a start point and switch to it | git switch -C feature main |
| Reset the branch without switching | git branch -f feature main |
| Delete the old branch first | git 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
Related pages
errors · giterror: pathspec 'x' did not match any file(s) known to gitgit checkout prints this when the name is neither a branch nor a tracked file, often a remote branch not fetched yet. Fetch, check the name, or create it.errors · gitfatal: not a git repository (or any of the parent directories): .gitGit prints not a git repository when the current folder and none of its parents contain a .git directory. cd into the repository, run git init, or set GIT_DIR.commands · gitgit push origin --delete branch: delete a remote branchDelete a remote branch with git push origin --delete NAME, the local one with git branch -d NAME, and stale origin/NAME refs with git fetch --prune. Examples.