Script Lover
Script Lover

Reputation: 381

fatal: A branch named 'new_branch' already exists

I've created a branch with name of xyz_api But when trying to switch in git by following command:

git checkout -b xyz_api

it returns following error

fatal: A branch named 'xyz_api' already exists.

Upvotes: 6

Views: 40910

Answers (3)

A.Muqtadir
A.Muqtadir

Reputation: 71

My case was a little different. I had cloned a branch yesterday. The next day owner of that branch deleted it and created a new one. Now when I was trying to switch to the newly created branch I was getting the same error

fatal: A branch named 'xyz_api' already exists.

So, the issue was my local repository still had a reference to the old branch, even though the branch was deleted and recreated remotely. Hence, I had to delete the old local reference before I could pull and switch to the new branch.

To delete the local branch and its reference

git branch -D branch-name

Then checkout the new branch

git checkout -b branch-name origin/branch-name

Upvotes: 0

kosist
kosist

Reputation: 3057

Use just git checkout xyz_api command

Upvotes: 3

Mark
Mark

Reputation: 5239

According to Git checkout:

Specifying -b causes a new branch to be created as if git-branch[1] were called and then checked out.

If you have already created the branch you do not need to specify the -b parameter in your command. git checkout xyz_api is enough to switch to a branch.

Upvotes: 13

Related Questions