23

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?

Nils_M
  • 1,062
  • 10
  • 24
schrom
  • 1,372
  • 1
  • 22
  • 36
  • So you want `git checkout master` to create a local tracking branch as in `git checkout -b master --track origin/master`? You do not want to create a local master branch which is different from the remotes master branch, right? You're absolutely right, that your command should work, provided there is exactly one remote with a branch with matching name. I really don't know how there is no error message at all... – Nils_M Dec 15 '15 at 11:50

4 Answers4

33

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

Nick
  • 676
  • 9
  • 22
  • This is what happened to me when I work with Magento. There is a folder called "dev" and we are also using a branch called "dev". So `git checkout dev` is actually checking out the content of `dev` folder not the dev branch. Thanks :) – user1105491 Jan 08 '18 at 12:33
20

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.

fhopf
  • 301
  • 2
  • 5
  • 5
    Ran into this too and didn't realize there were directories that matched the name of the target branches in my current working directory. If you switch directories, e.g. cd into a subdirectory, you'll be able to switch branches as well no problem. – Phil May 19 '17 at 17:31
  • 1
    No need to rename the folder. Just append a `--` to the command as pointed out in the answer by Nick. – josch Nov 07 '18 at 15:51
  • this here. this was the problem. – Jeremy Leipzig Dec 14 '22 at 00:24
4

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
Bustikiller
  • 2,423
  • 2
  • 16
  • 34
  • I think, from his line `git checkout -b master --track origin/master` which he says produces the target result, that he wants to create a tracking branch. Your code gives completely equal branches (as you point out). You also don't explain how there is no error message at all. – Nils_M Dec 15 '15 at 11:53
0

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

BobbyA
  • 2,090
  • 23
  • 41