akhilc
akhilc

Reputation: 181

Git not able to switch branches, except master

I'm new to git and was trying to work on a forked and cloned repo. In my PC I can see branches.

c:\>git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev
  remotes/origin/master
  remotes/upstream/dev
  remotes/upstream/master

And,

C:\>git branch -r
  origin/HEAD -> origin/master
  origin/dev
  origin/master
  upstream/dev
  upstream/master

But when I do

c:\> git checkout master
Already on 'master'
Your branch is up to date with 'origin/master'.

c:\> git checkout dev
error: pathspec 'dev' did not match any file(s) known to git.

Could anyone tell me why am I not able to switch to dev branch? I googled a bit, updated git (now 2.18.0.windows.1). Still this issue persists.

EDIT: It got resolved when I was randomly trying things in desktop app. I opened windows git desktop software. Clicked the option "Add local repositroy" and selected the folder where I had cloned the repo. Then I clicked the drop down branches and I could see dev there. When I selected it, it switched to that branch and command prompt issue was also resolved. Now I can switch to either master or dev. I don't know what happened internally when I did that, but it's working now.

Upvotes: 0

Views: 442

Answers (2)

Thomas
Thomas

Reputation: 181795

From man git-checkout (git help checkout):

If <branch> is not found but there does exist a tracking branch in exactly one remote (call it <remote>) with a matching name, treat as equivalent to

 $ git checkout -b <branch> --track <remote>/<branch>

So, often, git checkout dev would just have worked and done the right thing. What makes it not work in your case is that you have two remotes, origin and upstream, both containing a dev branch, so Git doesn't know which one to check out.

In this case, you'll have to use the longer form instead:

git checkout -b dev --track origin/dev

The -b dev creates a local branch called dev so you don't end up in "detached HEAD" state. The --track origin/dev sets it up as a tracking branch so you can push and pull it properly.

Actually, --track gives you a sane default for -b, so you can shorten this to:

git checkout --track origin/dev

Upvotes: 4

Jude Niroshan
Jude Niroshan

Reputation: 4460

You don't have a local branch named as dev. You need to fetch the branches in your origin to your local (your machine) .

You may need to try something like this:

git fetch origin
git checkout dev

Upvotes: 1

Related Questions