schrom
schrom

Reputation: 1641

git checkout master does not switch branch - repository broken?

tl;dr: git checkout master does not switch to master branch, gives no error or any output at all and I have no clue why. Any other branch works fine.

I have a git repository that consists of the branches development and master. I do a fresh clone of the repository, checking out branch development as default.

$ git clone <REPO-URL> --branch development
$ git branch -a
* development
origin/HEAD -> origin/development
origin/development
origin/master
$ git show-ref 
656c781c2affc26792f857baf8e232de07101535 refs/heads/development
656c781c2affc26792f857baf8e232de07101535 refs/remotes/origin/HEAD
656c781c2affc26792f857baf8e232de07101535 refs/remotes/origin/development
cfee1a1761642453edf5d001565f23b50243ff09 refs/remotes/origin/master

The ref master is pointing to is correct, that commit does exists and is the latest commit of my master branch.

So far everything looks normal, but when I try to switch to master this is happening:

$ git checkout master
$ git branch 
* development

No message from checkout, no error, nothing, and the branch is not switched.

Things I have tried so far:

Any further ideas? What am I missing? Is my repository broken? How can I fix this?

Upvotes: 23

Views: 22833

Answers (5)

Swapnil Bhojane
Swapnil Bhojane

Reputation: 1

Actually I fixed this issue by first I clone the expected branch as like

git clone -b <name of your dev branch> <http url of git repo>

then you can switch to master by

git checkout master

also you can switch to dev branch

git checkout <dev branch name>

so this work for me

Upvotes: 0

BobbyA
BobbyA

Reputation: 2260

The accepted answer (renaming the folder) works. If you don't want to do that, here's a work-around that I tested on git version 2.14.1.windows.1.

Delete the offending folder.

perform git branch <the branch you had trouble switching to>

perform git branch <your original branch>

perform git checkout -- . to undo the deletion.

Now you can switch to and from your desired branch without issue, even with a with a folder in the repo that's named the same as a branch. You'll notice that the branch appears in the branch list if you perform git branch

Upvotes: 0

Nick
Nick

Reputation: 713

If your repo has a folder or file name the same as a branch name then you will need to: git checkout xyz -- with the extra -- at the end. It tells git to use a branch or commit instead of trying to use a folder/file name.

Found the answer on another stackoverflow post: Git change branch when file of same name is present

Upvotes: 33

fhopf
fhopf

Reputation: 301

I have seen something similar when there is a folder master in the source tree. Unfortunately I didn't find a way to tell git to interpret the value as a branch. Renaming the folder fixed the problem for me.

Upvotes: 20

Bustikiller
Bustikiller

Reputation: 2498

The local master branch in your repository is not different from any other local branch in your repo. You cloned your repository to your development branch, which is the only local branch you have. Thus, if you try to checkout to your local master branch, git says it does not exist.

If you want to have both development and master local branch initially the same, you can do one of these things:

Download your repo to master branch, and create a local development branch:

git clone <repo_url>
git checkout -b development

or download your repo to development branch, and create a local master branch:

git clone <REPO-URL> --branch development
git checkout -b master

Upvotes: 4

Related Questions