Chadrick Blog

git error when checkout remote branch

I am currently in some branch and want to checkout to a remote branch named dev1 which is not yet existent in local git. So I tried calling,

$ git fetch origin dev1
$ git checkout origin dev1

but it gave me an error.

error: pathspec 'BRANCH-NAME' did not match any file(s) known to git

BTW, fetching worked fine and did not complain. So I tried

$ git checkout -b dev1 origin/dev1

but got error:

error “Cannot update paths and switch to branch”

I checked if my local git was tracking remote dev1 branch by

$ git remote show origin

remote origin
 Fetch URL: git@github.com:someuser/somegit.git
 Push  URL: git@github.com:someuser/somegit-TODAH.git
 HEAD branch: master
 Remote branch:
 dev tracked
 Local branch configured for 'git pull':
 dev merges with remote dev
 Local refs configured for 'git push':
 dev         pushes to dev         (up to date)

From the output, we can see local git was not tracking remote dev1 branch.

solution

make local git track remote dev branch

$ git fetch origin dev1:dev1

this should add tracking remote_branch_name(dev1) to local git by the name of local_branch_name(dev1). Mind the confusion due to use same name in remote and local.

now, execute

$ git checkout dev1

and goal is achieved.