git checkout master does not switch branch - repos

2020-03-08 08:22发布

问题:

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:

  • Creating a second branch master2 from the same commit => checkout works fine.
  • Deleting and re-creating branch master both local and on origin => again no checkout possible
  • git checkout -b master --track origin/master => works, but I think it should not be necessary to use this command as this is what git checkout master should do automatically
  • Committing and pushing to master works, but does not change the checkout problem
  • I tried several git versions (1.9, 2.2) and machines (linux, windows), problem occurs everywhere.

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

回答1:

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.



回答2:

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



回答3:

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


回答4:

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