git checkout master does not switch branch - repository broken?

六月ゝ 毕业季﹏ 提交于 2019-12-01 02:26:38

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.

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

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

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!