threads / discuss / 53857

Can't checkout branch

Subject: Can't checkout branch

## tl;dr

6 messages between Jul 14, 2020 and Jul 15, 2020.

replies: 5people: 4as markdown or json

Larry Martell· Jul 14, 2020, 16:59 UTC · lore

Been using git for a long time, first time I have seen this. I am unable to checkout a branch.

$ git status # On branch master nothing to commit, working directory clean

$ git fetch --all
Fetching origin
 * [new branch]      deploy     -> origin/deploy
$ git checkout deploy
$ git branch
* master
$ git checkout origin/deploy
Note: checking out 'origin/deploy'.
You are in 'detached HEAD' state.

I tried deleting the local repo and recloning, but I got the same results. I see the branch on github. I can checkout other branches, just not this one.

What could be going on?
Chris Torek· Jul 14, 2020, 18:17 UTC · re: Larry Martell · lore

Re: Can't checkout branch

On Tue, Jul 14, 2020 at 10:04 AM Larry Martell <larry.martell@gmail.com> wrote: [snippage]

Show 18 quoted lines
> $ git fetch --all
> Fetching origin
>  * [new branch]      deploy     -> origin/deploy
>
> $ git checkout deploy
> $ git branch
> * master
>
> $ git checkout origin/deploy
> Note: checking out 'origin/deploy'.
>
> You are in 'detached HEAD' state.
>
> I tried deleting the local repo and recloning, but I got the same
> results. I see the branch on github. I can checkout other branches,
> just not this one.
>
> What could be going on?

This happens in Git versions prior to 2.23 (what Git version are you using?) when there is a *file or directory* named `deploy`.

What is happening is that since there is no *branch* named deploy (yet), you can't check it out (yet). So `git checkout deploy` initially fails. The checkout command then goes on to try two alternatives:

 1. Treat this as `git checkout -- deploy`, i.e.,
    extract the file or subtree named `deploy` from
    the index (discarding changes to that file or
    subtree).
 2. Treat this as `git checkout -b deploy origin/deploy`
    or `git checkout -t origin/deploy`, i.e., create the
    branch `deploy` from `origin/deploy`.

If *both* of these alternatives work, pre-2.23 Git *assumes* you meant alternative number 1. In 2.23 or later, `git checkout` tells you that this is ambiguous and makes you pick which one you want.

To work around this in all versions of Git, you can just be more explicit. For instance:

    git checkout deploy --
forces Git to treat it as a branch name.
Chris
Junio C Hamano· Jul 14, 2020, 19:04 UTC · re: Chris Torek · lore

Re: Can't checkout branch

Chris Torek <chris.torek@gmail.com> writes:
Show 15 quoted lines
>  2. Treat this as `git checkout -b deploy origin/deploy`
>     or `git checkout -t origin/deploy`, i.e., create the
>     branch `deploy` from `origin/deploy`.
>
> If *both* of these alternatives work, pre-2.23 Git *assumes* you
> meant alternative number 1.  In 2.23 or later, `git checkout`
> tells you that this is ambiguous and makes you pick which one you
> want.
>
> To work around this in all versions of Git, you can just be
> more explicit.  For instance:
>
>     git checkout deploy --
>
> forces Git to treat it as a branch name.

I actually think "git checkout -t -b deploy origin/deploy" is a better way to be explicit to avoid confusion. You do not create a branch every few seconds anyway---it is more important to instill in user's head what really does go on by encouraging them to give these command line arguments explicitly, instead of teaching new people to rely on "git guesses most of the time in your favor so here is the lazy command line"---that will prevent true understanding and if it left unremedied long enough, will result in a confused user who is no longer a newbie.

Having said that, yes "git checkout deploy --" is a way to still rely on git guessing most of the time in your favor to stay lazy, and it should work, but there may be unseen other factors to break the DWIM, just like Larry didn't think of an existing file deploy in the working tree to be interfering.

Thanks.
Larry Martell· Jul 14, 2020, 20:29 UTC · re: Chris Torek · lore

Re: Can't checkout branch

On Tue, Jul 14, 2020 at 2:18 PM Chris Torek <chris.torek@gmail.com> wrote:
Show 50 quoted lines
>
> On Tue, Jul 14, 2020 at 10:04 AM Larry Martell <larry.martell@gmail.com> wrote:
> [snippage]
> > $ git fetch --all
> > Fetching origin
> >  * [new branch]      deploy     -> origin/deploy
> >
> > $ git checkout deploy
> > $ git branch
> > * master
> >
> > $ git checkout origin/deploy
> > Note: checking out 'origin/deploy'.
> >
> > You are in 'detached HEAD' state.
> >
> > I tried deleting the local repo and recloning, but I got the same
> > results. I see the branch on github. I can checkout other branches,
> > just not this one.
> >
> > What could be going on?
>
> This happens in Git versions prior to 2.23 (what Git version are
> you using?) when there is a *file or directory* named `deploy`.
>
> What is happening is that since there is no *branch* named
> deploy (yet), you can't check it out (yet).  So `git checkout
> deploy` initially fails.  The checkout command then goes on
> to try two alternatives:
>
>  1. Treat this as `git checkout -- deploy`, i.e.,
>     extract the file or subtree named `deploy` from
>     the index (discarding changes to that file or
>     subtree).
>
>  2. Treat this as `git checkout -b deploy origin/deploy`
>     or `git checkout -t origin/deploy`, i.e., create the
>     branch `deploy` from `origin/deploy`.
>
> If *both* of these alternatives work, pre-2.23 Git *assumes* you
> meant alternative number 1.  In 2.23 or later, `git checkout`
> tells you that this is ambiguous and makes you pick which one you
> want.
>
> To work around this in all versions of Git, you can just be
> more explicit.  For instance:
>
>     git checkout deploy --
>
> forces Git to treat it as a branch name.
Thanks for the reply - that was indeed the issue - I had a dir named deploy.
Andreas Schwab· Jul 15, 2020, 07:00 UTC · re: Chris Torek · lore

Re: Can't checkout branch

On Jul 14 2020, Chris Torek wrote:
Show 6 quoted lines
> To work around this in all versions of Git, you can just be
> more explicit.  For instance:
>
>     git checkout deploy --
>
> forces Git to treat it as a branch name.
For recent versions of git, you can also use `git switch deploy'.
Andreas.
-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."
Junio C Hamano· Jul 15, 2020, 14:50 UTC · re: Andreas Schwab · lore

Re: Can't checkout branch

Andreas Schwab <schwab@linux-m68k.org> writes:
Show 10 quoted lines
> On Jul 14 2020, Chris Torek wrote:
>
>> To work around this in all versions of Git, you can just be
>> more explicit.  For instance:
>>
>>     git checkout deploy --
>>
>> forces Git to treat it as a branch name.
>
> For recent versions of git, you can also use `git switch deploy'.
Yes, that is the best piece of advice in the thread so far.
Thanks.

← back to recent threads