Genadinik
Genadinik

Reputation: 18649

Fatal error when trying to check out another branch using git

I was trying to take a look at another branch by a developer and when I did this command:

git checkout -b master origin/new_branch_name 

I got this error message:

fatal: Cannot update paths and switch to branch 'master' at the same time. Did you intend to checkout 'origin/new_branch_name' which can not be resolved as commit? 

Dd I miss some step? Would anyone know what I did wrong? Was I supposed to create the repository locally first?

Upvotes: 2

Views: 3455

Answers (1)

twalberg
twalberg

Reputation: 62449

This error message indicates that the reference point from which you are trying to create a new branch does not exist in your local repository (origin/new_branch_name in your stated example). The solution to this is to run git fetch origin to make sure that those branches that exist your origin remote repository have been created locally as well (if you've messed with the default configuration for your origin remote, you may need to do git fetch origin refs/heads/*:refs/remotes/origin/* instead).

Additionally, since the argument to -b is intended to be the name of the new local branch being created, git checkout -b master ... is probably not correct, as you most likely already have a local master branch. Most of the time, it makes sense to name the local branch after the remote branch to avoid confusion, so the command you want to run after git fetch origin is git checkout -b new_branch_name origin/new_branch_name. (You could shorten that to simply git checkout new_branch_name, because the default behavior of git checkout <branch> if <branch> doesn't exist is to look for origin/<branch> and to create a local branch with the same name pointed to the remote branch).

Upvotes: 5

Related Questions