2

I have noticed a strange behaviour while using git.

Let's say I work on this repository, just an example of a repository with several branches.

I will just show the sequence of commands with relative answers, then briefly explain what I don't understand:

>git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

>git branch
* master

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

>git checkout Develop
Switched to a new branch 'Develop'
Branch 'Develop' set up to track remote branch 'Develop' from 'origin'.

>git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

>git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

>git branch
  Develop
* master

>git checkout develop
Switched to branch 'develop'

>git branch
  Develop
  master

>git status
On branch develop
nothing to commit, working tree clean

I basically checkout a remote branch but make a mistake in the name case. If I have no local branch that tracks the remote (so if I have no branch equal to the one I want to checkout with a different casing), it fails, otherwise I end up in this stange state where I am on a non existent branch ( it's not the one uppercase, as git branch does not mark it with a star). What is it happening?

EDIT: To address the duplicate flag and the comments: I know it is a problem of branches being case sensitive, it is not that. My question is more specifically: Why the second wrong checkout does not fail but takes me to this strange situation?

The output being requested in comment is:

>git branch -av
  Develop                4fc788f pdf added3
  master                 4fc788f pdf added3
  remotes/origin/Develop 4fc788f pdf added3
  remotes/origin/HEAD    -> origin/master
  remotes/origin/master  4fc788f pdf added3

>git --version
git version 2.17.1.windows.2
bracco23
  • 2,181
  • 10
  • 28
  • 1
    Are you using Git on Windows (where filenames are case-insensitive)? – Jonathon Reinhart Jan 16 '19 at 11:50
  • 3
    Possible duplicate of [Git branch name - case sensitive or insensitive?](https://stackoverflow.com/questions/38493543/git-branch-name-case-sensitive-or-insensitive) – Jonathon Reinhart Jan 16 '19 at 11:51
  • Yes, It Is on Windows – bracco23 Jan 16 '19 at 11:55
  • Please show result of git branch -av – Christoph Jan 16 '19 at 11:57
  • On Windows you can even run `git` as `GIT` (all uppercase) and this is not a Git issue. The same happens on macOS. The filesystem finds the (same) Git executable when you call it either as `git` or `GIT` or `Git`. The same thing happens when `git checkout develop` is looking for the `.git/refs/heads/develop` file because Git keeps the references (branches, tags) in files. The local `Develop` branch is kept in `.git/refs/heads/Develop`, the local `foo/bar` branch is kept in `.git/refs/heads/foo/bar`, the remote `origin/Develop` branch is kept in `.git/refs/remotes/origin/Develop` and so on. – axiac Jan 16 '19 at 15:39

1 Answers1

1

This is a case sensitivity problem. When you first try to check out the develop branch:

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

There is no local branch named develop. Due to case sensitivity issues in the way branch information is stored, a local branch named Develop might be identified as matching the branch name develop, if it was stored as a loose reference. However, at this point, neither exist so it's moot.

>git checkout Develop
Switched to a new branch 'Develop'
Branch 'Develop' set up to track remote branch 'Develop' from 'origin'.

There is no local branch named Develop. Nor is there one named develop, as we learned in the prior command. However, there is a remote branch origin/Develop. So git assumes that you want to create that, so it creates a branch Develop that tracks origin/Develop.

So when you next run:

>git checkout develop
Switched to branch 'develop'

Again, git looks for a local branch named develop. You still do not have one. However, you do have a Develop branch, and since git has used a loose reference to store that information on a case insensitive filesystem, it's found that information.

Regrettably, git has remained confused about this situation: when you asked for the develop branch, it located the information about Develop. But despite that, git's branch names are case sensitive. And since there's not actually a branch named develop (lower case), you're now in a strange state where you're not actually on any branch that git knows about.

Edward Thomson
  • 74,857
  • 14
  • 158
  • 187
  • Thanks for the answer, but a few details still bug me. If so, why isn't `git status` showing me that I am on `Develop` and it shows like if I was on no branch? Sounds like if I was going to commit something now it would be lost. Also, why isn't Git taking into account casing to avoid the problem? – bracco23 Jan 16 '19 at 15:37
  • You're *not* on Develop. You tried to check out a branch called "develop", Git's branch storage mechanisms are broken on case insensitive filesystems, and has put you into this state. As for why Git hasn't fixed this, you'd need to ask the authors, who have been ignoring this issue for well over a decade. – Edward Thomson Jan 16 '19 at 16:10
  • Good enough for me. If you feel like, update the answer with this new informations, thank you. – bracco23 Jan 16 '19 at 16:19