threads / discuss / 53779

Why won't git checkout a branch?

Subject: Why won't git checkout a branch?

## tl;dr

6 messages between Jul 1, 2020 and Jul 1, 2020.

replies: 5people: 3as markdown or json

Jeffrey Walton· Jul 1, 2020, 08:41 UTC · lore
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· Jul 1, 2020, 09:50 UTC · re: Jeffrey Walton · lore

Re: Why won't git checkout a branch?

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· Jul 1, 2020, 10:09 UTC · re: Andreas Schwab · lore

Re: Why won't git checkout a branch?

On Wed, Jul 1, 2020 at 5:50 AM Andreas Schwab <schwab@linux-m68k.org> wrote:
Show 10 quoted lines
>
> 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· Jul 1, 2020, 10:20 UTC · re: Jeffrey Walton · lore

Re: Why won't git checkout a branch?

On Jul 01 2020, Jeffrey Walton wrote:
Show 16 quoted lines
> 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· Jul 1, 2020, 10:45 UTC · re: Andreas Schwab · lore

Re: Why won't git checkout a branch?

On Wed, Jul 1, 2020 at 6:20 AM Andreas Schwab <schwab@linux-m68k.org> wrote:
Show 22 quoted lines
>
> 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· Jul 1, 2020, 15:13 UTC · re: Jeffrey Walton · lore

Re: Why won't git checkout a branch?

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

← back to recent threads