# Why won't git checkout a branch?

6 messages from 2020-07-01 to 2020-07-01. Participants: Jeffrey Walton, Andreas Schwab, Chris Torek.
Thread: https://gitlist.dev/t/53779

## Jeffrey Walton, 2020-07-01 08:41

Subject: Why won't git checkout a branch?
Message-ID: <CAH8yC8m1=0n8zzoW--g_XLebJFqFSXpBpTr+1S58z7snLTt-0Q@mail.gmail.com>
URL: https://gitlist.dev/e/CAH8yC8m1%3D0n8zzoW--g_XLebJFqFSXpBpTr%2B1S58z7snLTt-0Q%40mail.gmail.com

```
Why can't I checkout a branch? What's broken this time?

$ rm -rf libb2
$ git clone https://github.com/noloader/libb2
Cloning into 'libb2'...
remote: Enumerating objects: 39, done.
remote: Counting objects: 100% (39/39), done.
remote: Compressing objects: 100% (31/31), done.
remote: Total 291 (delta 14), reused 23 (delta 8), pack-reused 252
Receiving objects: 100% (291/291), 431.67 KiB | 4.23 MiB/s, done.
Resolving deltas: 100% (162/162), done.
$ cd libb2
$ git fetch --all
Fetching origin
$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/autotools
  remotes/origin/fix-bigendian
  remotes/origin/m4
  remotes/origin/master
  remotes/origin/overhaul
$ git checkout m4
$ git branch
* master

```

## Andreas Schwab, 2020-07-01 09:50

Subject: Re: Why won't git checkout a branch?
Message-ID: <87tuyrd0ng.fsf@igel.home>
URL: https://gitlist.dev/e/87tuyrd0ng.fsf%40igel.home
In-Reply-To: <CAH8yC8m1=0n8zzoW--g_XLebJFqFSXpBpTr+1S58z7snLTt-0Q@mail.gmail.com>

```
On Jul 01 2020, Jeffrey Walton wrote:

> $ git checkout m4

Try a newer git version:

$ git checkout m4
fatal: 'm4' could be both a local file and a tracking branch.
Please use -- (and optionally --no-guess) to disambiguate

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."

```

## Jeffrey Walton, 2020-07-01 10:09

Subject: Re: Why won't git checkout a branch?
Message-ID: <CAH8yC8kxY5mV0p-98W0uDN7PLxmNR4LkNcz9vbKsq21aTd-qQg@mail.gmail.com>
URL: https://gitlist.dev/e/CAH8yC8kxY5mV0p-98W0uDN7PLxmNR4LkNcz9vbKsq21aTd-qQg%40mail.gmail.com
In-Reply-To: <87tuyrd0ng.fsf@igel.home>

```
On Wed, Jul 1, 2020 at 5:50 AM Andreas Schwab <schwab@linux-m68k.org> wrote:
>
> On Jul 01 2020, Jeffrey Walton wrote:
>
> > $ git checkout m4
>
> Try a newer git version:
>
> $ git checkout m4
> fatal: 'm4' could be both a local file and a tracking branch.
> Please use -- (and optionally --no-guess) to disambiguate

Ack, thanks.

This does not look normal, either. I don't want to create a new
branch. I want to work on the existing branch.

$ git checkout origin/m4
Note: checking out 'origin/m4'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at 5c3967f Merge branch 'm4' of
https://github.com/noloader/libb2 into m4

Jeff

```

## Andreas Schwab, 2020-07-01 10:20

Subject: Re: Why won't git checkout a branch?
Message-ID: <87lfk3czae.fsf@igel.home>
URL: https://gitlist.dev/e/87lfk3czae.fsf%40igel.home
In-Reply-To: <CAH8yC8kxY5mV0p-98W0uDN7PLxmNR4LkNcz9vbKsq21aTd-qQg@mail.gmail.com>

```
On Jul 01 2020, Jeffrey Walton wrote:

> On Wed, Jul 1, 2020 at 5:50 AM Andreas Schwab <schwab@linux-m68k.org> wrote:
>>
>> On Jul 01 2020, Jeffrey Walton wrote:
>>
>> > $ git checkout m4
>>
>> Try a newer git version:
>>
>> $ git checkout m4
>> fatal: 'm4' could be both a local file and a tracking branch.
>> Please use -- (and optionally --no-guess) to disambiguate
>
> Ack, thanks.
>
> This does not look normal, either. I don't want to create a new
> branch. I want to work on the existing branch.

To disambiguate, use `git checkout m4 --' if you want the branch, and
`git checkout -- m4' if you want the local file.

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."

```

## Jeffrey Walton, 2020-07-01 10:45

Subject: Re: Why won't git checkout a branch?
Message-ID: <CAH8yC8=SgS1=mpSM_7j8FQzreVPfE5U_tnsCz9ixZoBHLUuM7A@mail.gmail.com>
URL: https://gitlist.dev/e/CAH8yC8%3DSgS1%3DmpSM_7j8FQzreVPfE5U_tnsCz9ixZoBHLUuM7A%40mail.gmail.com
In-Reply-To: <87lfk3czae.fsf@igel.home>

```
On Wed, Jul 1, 2020 at 6:20 AM Andreas Schwab <schwab@linux-m68k.org> wrote:
>
> On Jul 01 2020, Jeffrey Walton wrote:
>
> > On Wed, Jul 1, 2020 at 5:50 AM Andreas Schwab <schwab@linux-m68k.org> wrote:
> >>
> >> On Jul 01 2020, Jeffrey Walton wrote:
> >>
> >> > $ git checkout m4
> >>
> >> Try a newer git version:
> >>
> >> $ git checkout m4
> >> fatal: 'm4' could be both a local file and a tracking branch.
> >> Please use -- (and optionally --no-guess) to disambiguate
> >
> > Ack, thanks.
> >
> > This does not look normal, either. I don't want to create a new
> > branch. I want to work on the existing branch.
>
> To disambiguate, use `git checkout m4 --' if you want the branch, and
> `git checkout -- m4' if you want the local file.

Perfect, thanks.

So the way to checkout that always works is to append --. Is that correct?

If so, I will start using it everywhere.

Jeff

```

## Chris Torek, 2020-07-01 15:13

Subject: Re: Why won't git checkout a branch?
Message-ID: <CAPx1Gvc+pWe6UwRREHMkUCViYjzQgfg1jfbWy2pyFQCjhwV+Bg@mail.gmail.com>
URL: https://gitlist.dev/e/CAPx1Gvc%2BpWe6UwRREHMkUCViYjzQgfg1jfbWy2pyFQCjhwV%2BBg%40mail.gmail.com
In-Reply-To: <CAH8yC8kxY5mV0p-98W0uDN7PLxmNR4LkNcz9vbKsq21aTd-qQg@mail.gmail.com>

```
On Wed, Jul 1, 2020 at 3:10 AM Jeffrey Walton <noloader@gmail.com> wrote:
> This does not look normal, either. I don't want to create a new
> branch. I want to work on the existing branch.

Technically, you *have to* create a new branch here: you do
not have an `m4` branch (yet). Your local repository's branches
are independent of the branches in the repository you cloned.

(You could work in detached HEAD state to avoid the technical
requirement here but that's not practical for normal humans. :-) )

The suffix-`--` trick works in old versions of Git, creating the `m4`
branch from the `origin/m4` name.  If you upgrade to Git 2.23 or
later and use `git switch`, I think you will find that it's generally
more user friendly.

Chris

```
