git: fatal: Cannot switch branch to a non-commit '12382'

23.6k views Asked by At

Someone else on my team created a new git branch, committed and pushed to the usual remote that we work with. When I try to check out this branch, I get this:

% git checkout 12382
fatal: Cannot switch branch to a non-commit '12382'

I have not had trouble checking out other branches from this repository; tried checking another one out right after this (one that I did not have a local copy of), and it worked fine.

I tried building a server with this branch on our Go pipeline, it worked fine - which means the server was successful in checking out that branch.

Tried this to check the status of things:

% git remote show origin
* remote origin
  Fetch URL: [email protected]:mycompany/myrepository.git
  Push  URL: [email protected]:mycompany/myrepository.git
  HEAD branch: stage
  Remote branches:
    10112                     tracked
    10198                     tracked
    10678                     tracked
...
    12382                     tracked    <<<---
...
  Local branches configured for 'git pull':
...
  Local refs configured for 'git push':
...

Could anyone suggest how to fix this? What went wrong?

5

There are 5 answers

10
knittl On BEST ANSWER

Git is confused, because 12382 looks like a commit hash. Use the fully qualified name to checkout the branch:

git checkout refs/heads/12382 --

or, if it's a remote branch:

git checkout refs/remotes/origin/12382 --
1
zaphodb On

@knittl: thanks that worked, had to do the following additional steps:

% git checkout refs/remotes/origin/12382
Note: checking out 'refs/remotes/origin/12382'.

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 2d834e4... 

% git branch | grep 12382
* (detached from origin/12382)

% git checkout -b 12382
Switched to a new branch '12382'

% git status
On branch 12382
nothing to commit, working directory clean

% git push --set-upstream origin 12382
Branch 12382 set up to track remote branch 12382 from origin.
Everything up-to-date
0
Christian On
% git switch -t origin/12382
Branch '12382' set up to track remote branch '12382' from 'origin'.
Switched to a new branch '12382'

This seems to have worked for me. I used to use git checkout but I might start using git switch now, to change branches.

In my case, someone used the bug tracking system ticket number as the branch name without an alphabetic prefix.

git switch is since git 2.23.

0
mfaani On

The question is an edge case and it's already been answered.

I'll answer the error on a more general level:


To be able to switch/checkout to something in your source tree it must be of type:

  • commit : git checkout: 90392aeda17d730d472493bc5a36237407c80979 or perhaps just do the first 7 digits ``git checkout: 90392ae`
  • tag git checkout V2.0.3
  • branch (remote branches too) git checkout newLogin
  • HEAD git checkout HEAD^1
  • Hash, short Hash.

So if you are switching to something that is none of them, like you mistyped your branch name, git would give you this error.

Cannot switch branch to a non-commit means the think you are trying to checkout to something that isn't tree-ish

0
Harel.Sh On

although old issue i didn't find god answer on the net so adding below which worked for me: git checkout --track origin/