threads / discuss / 35010

"git checkout foo" is getting confused by folder named "foo"

Subject: "git checkout foo" is getting confused by folder named "foo"

## tl;dr

11 messages between Sep 24, 2013 and Sep 26, 2013.

replies: 10people: 5as markdown or json

Jona Christopher Sahnwaldt· Sep 24, 2013, 21:07 UTC · lore
Hi,
maybe this has already been reported, but I didn't find it in the mail archive.

If I understand correctly, after I clone a repo, I should be able to switch to branch foo just by running

git checkout foo

This doesn't seem to work if a folder called "foo" exists in the root of the repo.

I got the same behavior with git 1.8.3.2 on a Mac and git 1.7.9.5 on Linux.
Steps to reproduce:

git clone https://github.com/dbpedia/extraction-framework.git cd extraction-framework/

First the happy path - there is a remote branch "live-dev", but no folder "extraction-framework/live-dev":

git checkout live-dev
Response:

Branch live-dev set up to track remote branch live-dev from origin. Switched to a new branch 'live-dev'

Fine! Now the unhappy path - there is a branch "wiktionary", but also a folder "extraction-framework/wiktionary":

git checkout wiktionary

Nothing - no response, no changes to working tree. .git/index seems to be modified though.

Slightly different - cd to some folder, try checkout again:

cd mappings git checkout wiktionary

Response:
error: pathspec 'wiktionary' did not match any file(s) known to git.

My workaround is that when I switch to a branch for the first time, I have to call

git checkout -t -b wiktionary --track origin/wiktionary
Response:

Branch wiktionary set up to track remote branch wiktionary from origin. Switched to a new branch 'wiktionary'

Looks good. After that, I can switch back and forth between branches just by git checkout wiktionary / git checkout master.

Cheers, Christopher

David Aguilar· Sep 25, 2013, 02:51 UTC · re: Jona Christopher Sahnwaldt · lore

Re: "git checkout foo" is getting confused by folder named "foo"

On Tue, Sep 24, 2013 at 2:07 PM, Jona Christopher Sahnwaldt <jc@sahnwaldt.de> wrote:

Show 11 quoted lines
> Hi,
>
> maybe this has already been reported, but I didn't find it in the mail archive.
>
> If I understand correctly, after I clone a repo, I should be able to
> switch to branch foo just by running
>
> git checkout foo
>
> This doesn't seem to work if a folder called "foo" exists in the root
> of the repo.
git checkout foo --
The double-dash at the end disambiguates between refs and paths.

You can use that trick on any command that accepts refspec (branches, tags, etc) and pathspec (path patterns).

-- 
David
Keshav Kini· Sep 25, 2013, 03:13 UTC · re: David Aguilar · lore

Re: "git checkout foo" is getting confused by folder named "foo"

David Aguilar <davvid@gmail.com> writes:
Show 20 quoted lines
> On Tue, Sep 24, 2013 at 2:07 PM, Jona Christopher Sahnwaldt
> <jc@sahnwaldt.de> wrote:
>> Hi,
>>
>> maybe this has already been reported, but I didn't find it in the mail archive.
>>
>> If I understand correctly, after I clone a repo, I should be able to
>> switch to branch foo just by running
>>
>> git checkout foo
>>
>> This doesn't seem to work if a folder called "foo" exists in the root
>> of the repo.
>
> git checkout foo --
>
> The double-dash at the end disambiguates between refs and paths.
>
> You can use that trick on any command that accepts refspec (branches,
> tags, etc) and pathspec (path patterns).

I was going to reply with similar advice, but I actually tried it on the example repo and it didn't work. Apparently it doesn't interoperate properly with the functionality that guesses when you're trying to check out a remote branch and creates an equivalently named local branch.

    [2] fs@erdos /tmp $ git clone https://github.com/dbpedia/extraction-framework.git
    Cloning into 'extraction-framework'...
    remote: Counting objects: 33513, done.
    remote: Compressing objects: 100% (6633/6633), done.
    remote: Total 33513 (delta 19000), reused 32922 (delta 18436)
    Receiving objects: 100% (33513/33513), 23.48 MiB | 747.00 KiB/s, done.
    Resolving deltas: 100% (19000/19000), done.
    Checking connectivity... done
    [2] fs@erdos /tmp $ cd extraction-framework/
    [2] fs@erdos /tmp/extraction-framework $ git checkout live-dev --
    fatal: invalid reference: live-dev
    [2] fs@erdos /tmp/extraction-framework $ git checkout live-dev
    Branch live-dev set up to track remote branch live-dev from origin.
    Switched to a new branch 'live-dev'
    [2] fs@erdos /tmp/extraction-framework $ git checkout master
    Switched to branch 'master'
    [2] fs@erdos /tmp/extraction-framework $ git checkout wiktionary
    [2] fs@erdos /tmp/extraction-framework $ git branch
      live-dev
    * master
    [2] fs@erdos /tmp/extraction-framework $ git checkout wiktionary --
    fatal: invalid reference: wiktionary
-Keshav
Duy Nguyen· Sep 25, 2013, 03:33 UTC · re: Keshav Kini · lore

Re: "git checkout foo" is getting confused by folder named "foo"

On Wed, Sep 25, 2013 at 10:13 AM, Keshav Kini <keshav.kini@gmail.com> wrote:
>     [2] fs@erdos /tmp/extraction-framework $ git checkout wiktionary --
>     fatal: invalid reference: wiktionary

It may work if we demote this from fatal to error warning so the dwim logic in checkout has a chance to try differently, I think.

-- 
Duy
Jona Christopher Sahnwaldt· Sep 25, 2013, 08:58 UTC · re: David Aguilar · lore

Re: "git checkout foo" is getting confused by folder named "foo"

On 25 September 2013 04:51, David Aguilar <davvid@gmail.com> wrote:
Show 15 quoted lines
> On Tue, Sep 24, 2013 at 2:07 PM, Jona Christopher Sahnwaldt
> <jc@sahnwaldt.de> wrote:
>> Hi,
>>
>> maybe this has already been reported, but I didn't find it in the mail archive.
>>
>> If I understand correctly, after I clone a repo, I should be able to
>> switch to branch foo just by running
>>
>> git checkout foo
>>
>> This doesn't seem to work if a folder called "foo" exists in the root
>> of the repo.
>
> git checkout foo --

Thanks for the suggestion, but it doesn't work for me. With both 1.7.9.5 and 1.8.3.2, I get this:

$ git checkout wiktionary -- fatal: invalid reference: wiktionary

When I try the full branch name:
$ git checkout origin/wiktionary --
Note: checking out 'origin/wiktionary'.
You are in 'detached HEAD' state. You can [...]
:-(
Christopher
Show 7 quoted lines
>
> The double-dash at the end disambiguates between refs and paths.
>
> You can use that trick on any command that accepts refspec (branches,
> tags, etc) and pathspec (path patterns).
> --
> David
Matthieu Moy· Sep 25, 2013, 13:09 UTC · re: Jona Christopher Sahnwaldt · lore

Re: "git checkout foo" is getting confused by folder named "foo"

Jona Christopher Sahnwaldt <jc@sahnwaldt.de> writes:
Show 22 quoted lines
> On 25 September 2013 04:51, David Aguilar <davvid@gmail.com> wrote:
>> On Tue, Sep 24, 2013 at 2:07 PM, Jona Christopher Sahnwaldt
>> <jc@sahnwaldt.de> wrote:
>>> Hi,
>>>
>>> maybe this has already been reported, but I didn't find it in the mail archive.
>>>
>>> If I understand correctly, after I clone a repo, I should be able to
>>> switch to branch foo just by running
>>>
>>> git checkout foo
>>>
>>> This doesn't seem to work if a folder called "foo" exists in the root
>>> of the repo.
>>
>> git checkout foo --
>
> Thanks for the suggestion, but it doesn't work for me. With both
> 1.7.9.5 and 1.8.3.2, I get this:
>
> $ git checkout wiktionary --
> fatal: invalid reference: wiktionary

OK, what happens is that "git checkout wiktionary" is actually a shorthand for "git checkout -b wiktionary --track origin/wiktionary".

In other words, it does not only "checkout" the branch, but it creates a local branch with the right name, and checks it out.

The -- disables this shorthand. I'd consider this as a bug. I've just sent a patch to try to fix this.

Show 5 quoted lines
> When I try the full branch name:
>
> $ git checkout origin/wiktionary --
> Note: checking out 'origin/wiktionary'.
> You are in 'detached HEAD' state. You can [...]

This actually checks out the right commit, but does not create a local branch. That's not a very desirable solution.

In short, this should do the trick:
  git checkout -b wiktionary --track origin/wiktionary
-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/
Jona Christopher Sahnwaldt· Sep 25, 2013, 16:24 UTC · re: Matthieu Moy · lore

Re: "git checkout foo" is getting confused by folder named "foo"

Hi everyone,

tl;dr: The short form "git checkout foo" is a mess. There's simply too much "DWIM" magic going on. There are no comprehensible rules how it decides if "foo" is a pathspec or a refspec.

On 25 September 2013 15:09, Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> wrote:
Show 27 quoted lines
> Jona Christopher Sahnwaldt <jc@sahnwaldt.de> writes:
>
>> On 25 September 2013 04:51, David Aguilar <davvid@gmail.com> wrote:
>>> On Tue, Sep 24, 2013 at 2:07 PM, Jona Christopher Sahnwaldt
>>> <jc@sahnwaldt.de> wrote:
>>>> Hi,
>>>>
>>>> maybe this has already been reported, but I didn't find it in the mail archive.
>>>>
>>>> If I understand correctly, after I clone a repo, I should be able to
>>>> switch to branch foo just by running
>>>>
>>>> git checkout foo
>>>>
>>>> This doesn't seem to work if a folder called "foo" exists in the root
>>>> of the repo.
>>>
>>> git checkout foo --
>>
>> Thanks for the suggestion, but it doesn't work for me. With both
>> 1.7.9.5 and 1.8.3.2, I get this:
>>
>> $ git checkout wiktionary --
>> fatal: invalid reference: wiktionary
>
> OK, what happens is that "git checkout wiktionary" is actually a
> shorthand for "git checkout -b wiktionary --track origin/wiktionary".

No, it isn't. More precisely: if branch "foo" is not yet in .git/config, but there is a branch "origin/foo" and no file/folder "foo" in the root of the project, then "git checkout foo" is short for "git checkout -b foo -t origin/foo" (which in turn can be shortened to "git checkout -t origin/foo", if I understand correctly).

Below are the rather arcane rules that I found in 1.7.9.5. They apply if the branch "foo" is not yet configured in .git/config. If branch "foo" is in .git/config I guess "git checkout foo" will always check out branch "foo", no matter if there's a file called "foo" somewhere.

I gave up testing them in 1.8.3.2, but I assume its behavior the same.
Let's consider several scenarios:
1. there is a branch "origin/foo", but also a *tracked* file/folder
"foo" in the *root* folder of the project:

a. when I'm in the root folder, "git checkout foo" silently resets the working tree file/folder "foo" to its staged / HEAD version (I think) and prints no response.

b. when I'm in a sub-folder that contains a tracked file/folder called "foo", "git checkout foo" silently resets the working tree file/folder "foo" to its staged / HEAD version and prints no response

c. when I'm in a sub-folder that cotains *no* *tracked* file/folder called "foo", "git checkout foo" does nothing and prints "error: pathspec 'foo' did not match any file(s) known to git."

2. there is a branch "origin/foo", but also an *untracked* file/folder
"foo" in the *root* folder of the project:

a. when I'm in the root folder, "git checkout foo" does nothing and prints "error: pathspec 'foo' did not match any file(s) known to git."

b. when I'm in a sub-folder containing a *tracked* file/folder "foo", "git checkout foo" silently resets the working tree file/folder "foo" to its staged / HEAD version and prints no response

c. when I'm in a sub-folder that contains *no* *tracked* file/folder called "foo", "git checkout foo" does nothing and prints "error: pathspec 'foo' did not match any file(s) known to git."

3. there is a branch "origin/foo", but *no*  file/folder "foo" in the
*root* folder of the project:

a. if I'm in the root folder (no tracked or untracked file/folder "foo"), "git checkout foo" switches to branch "foo" which tracks "origin/foo":

b. if I'm in a sub-folder with a (tracked or untracked) file/folder "foo", "git checkout foo" switches to branch "foo" which tracks "origin/foo":

4. there is *no* branch "origin/foo"

a. when I'm in root or sub-folder that contains a *tracked* file/folder called "foo", "git checkout foo" silently resets the working tree file/folder "foo" to its staged / HEAD version and prints no response

b. when I'm in root or sub-folder that cotains *no* *tracked* file/folder called "foo", "git checkout foo" does nothing and prints "error: pathspec 'foo' did not match any file(s) known to git."

All right, I guess there are more cases, but let's leave it at that...
>
> In other words, it does not only "checkout" the branch, but it creates a
> local branch with the right name, and checks it out.
No, in my case, it doesn't, because there is a folder called "wiktionary".
Show 16 quoted lines
>
> The -- disables this shorthand. I'd consider this as a bug. I've just
> sent a patch to try to fix this.
>
>> When I try the full branch name:
>>
>> $ git checkout origin/wiktionary --
>> Note: checking out 'origin/wiktionary'.
>> You are in 'detached HEAD' state. You can [...]
>
> This actually checks out the right commit, but does not create a local
> branch. That's not a very desirable solution.
>
> In short, this should do the trick:
>
>   git checkout -b wiktionary --track origin/wiktionary

Yes, I know, as I said in my original mail. By the way, I later found that I can shorten this to "git checkout -t origin/wiktionary".

JC
>
> --
> Matthieu Moy
> http://www-verimag.imag.fr/~moy/
Matthieu Moy· Sep 25, 2013, 19:12 UTC · re: Jona Christopher Sahnwaldt · lore

Re: "git checkout foo" is getting confused by folder named "foo"

Jona Christopher Sahnwaldt <jc@sahnwaldt.de> writes:
Show 5 quoted lines
> Hi everyone,
>
> tl;dr: The short form "git checkout foo" is a mess. There's simply too
> much "DWIM" magic going on. There are no comprehensible rules how it
> decides if "foo" is a pathspec or a refspec.
There is a very simple rule:

What's on the left hand side of -- are refs, what's on the right hand side are paths.

When you don't use --, then Git tries to guess, and fails whenever there's an ambiguity.

>> OK, what happens is that "git checkout wiktionary" is actually a
>> shorthand for "git checkout -b wiktionary --track origin/wiktionary".
>
> No, it isn't.

What I meant was that the short form advised by people were _meant_ to be a shorthand.

> Let's consider several scenarios:

I don't get your point. Is the overly long list a way of complaining? Are you suggesting a change? What do you think about the change I'm proposing?

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/
Jona Christopher Sahnwaldt· Sep 25, 2013, 19:36 UTC · re: Matthieu Moy · lore

Re: "git checkout foo" is getting confused by folder named "foo"

On 25 September 2013 21:12, Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> wrote:
Show 15 quoted lines
> Jona Christopher Sahnwaldt <jc@sahnwaldt.de> writes:
>
>> Hi everyone,
>>
>> tl;dr: The short form "git checkout foo" is a mess. There's simply too
>> much "DWIM" magic going on. There are no comprehensible rules how it
>> decides if "foo" is a pathspec or a refspec.
>
> There is a very simple rule:
>
> What's on the left hand side of -- are refs, what's on the right hand
> side are paths.
>
> When you don't use --, then Git tries to guess, and fails whenever
> there's an ambiguity.

That's the case I'm concerned with. And I think the guessing confuses users in many cases. It certainly has confused me.

Show 12 quoted lines
>
>>> OK, what happens is that "git checkout wiktionary" is actually a
>>> shorthand for "git checkout -b wiktionary --track origin/wiktionary".
>>
>> No, it isn't.
>
> What I meant was that the short form advised by people were _meant_ to
> be a shorthand.
>
>> Let's consider several scenarios:
>
> I don't get your point. Is the overly long list a way of complaining?

It's a way of showing that human beings can't understand git's guesswork. :-) It was also a (failed) attempt to understand the rules of this heuristic. And an attempt to show the developers that the rules have gotten out of hand.

> Are you suggesting a change?

Yes, I think the rules for the "short form" (the guessing when there's no --) should be made simpler, or maybe the guessing should be dropped altogether. I don't know. I don't know git well enough to be able to be more specific. I just find the current behavior very confusing.

> What do you think about the change I'm proposing?

I don't know. It looks like it's not really addressing my specific problem, because as far as I understand it only applies when there is a --. But again, I don't know git well enough.

Anyway, thanks for your work. I'm sorry I can't provide more useful input. This "short form" of checkout is just a small feature. I guess I'm bikeshedding here.

Cheers, JC

>
> --
> Matthieu Moy
> http://www-verimag.imag.fr/~moy/
Matthieu Moy· Sep 25, 2013, 20:01 UTC · re: Jona Christopher Sahnwaldt · lore

Re: "git checkout foo" is getting confused by folder named "foo"

Jona Christopher Sahnwaldt <jc@sahnwaldt.de> writes:
> Yes, I think the rules for the "short form" (the guessing when there's
> no --) should be made simpler, or maybe the guessing should be dropped
> altogether. I don't know. I don't know git well enough to be able to
> be more specific. I just find the current behavior very confusing.

It can hardly be "simpler" (in the sense "behavior that can be described with fewer words"), but it could be tightened to be safer.

When a remote branch $foo exists, a local branch $foo does not, and a file $foo does, then

  git checkout $foo

rather likely means "I want to use git checkout's DWIM and create local branch $foo", but it currently means to Git "checkout file foo from the index". It would make sense to die here, and require the use of --.

No time to write a patch for this. Any volunteer?
-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/
Jona Christopher Sahnwaldt· Sep 26, 2013, 21:25 UTC · re: Matthieu Moy · lore

Re: "git checkout foo" is getting confused by folder named "foo"

On 25 September 2013 22:01, Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> wrote:
Show 18 quoted lines
> Jona Christopher Sahnwaldt <jc@sahnwaldt.de> writes:
>
>> Yes, I think the rules for the "short form" (the guessing when there's
>> no --) should be made simpler, or maybe the guessing should be dropped
>> altogether. I don't know. I don't know git well enough to be able to
>> be more specific. I just find the current behavior very confusing.
>
> It can hardly be "simpler" (in the sense "behavior that can be described
> with fewer words"), but it could be tightened to be safer.
>
> When a remote branch $foo exists, a local branch $foo does not, and a
> file $foo does, then
>
>   git checkout $foo
>
> rather likely means "I want to use git checkout's DWIM and create local
> branch $foo", but it currently means to Git "checkout file foo from the
> index". It would make sense to die here, and require the use of --.

That sounds good. A rule like "when A is true, B is false, and C is true, then X" is probably too complex to be useful. It's probably better to give up and say "sorry, I DKWYM (don't know what you mean)". :-)

There are a few more ideas, opinions, discussions about all this at http://stackoverflow.com/questions/18833617/why-does-git-checkout-remote-branchname-not-create-new-tracking-branch especially in the comments.

Cheers, JC

Show 6 quoted lines
>
> No time to write a patch for this. Any volunteer?
>
> --
> Matthieu Moy
> http://www-verimag.imag.fr/~moy/

← back to recent threads