Git error "fatal: XXX is not a commit and a branch YYY cannot be created from it
Asked Answered
L

3

12

I am trying the checkout a branch from a remote repository into a local branch, and receive the above error.

For those who want to direct me to: Why is it not a commit and a branch cannot be created from it?, then I already tried fetching, and even git fetch --all

To be precise, I cannot checkout any branch on the Github repository, that is not the main branch that I'm tracking, let's call it dev.

So, I can do the following:

git checkout origin/dev -b my_own_dev_env

But I cannot checkout any other branch, not even

git checkout origin/master -b master

And in this case I receive

"fatal: 'origin/master' is not a commit and a branch 'master' cannot be created from it"

Edit: When cloning to a new directory, I can perform all git operations as usual. So I would like to know what could go wrong in a local copy that prevents git commands from working properly?

Longley answered 7/8, 2019 at 7:50 Comment(8)
We're going to need a lot more information. Lets start with, can you checkout your branch if you create a clean clone of the remote? Can you confirm that the branch exists on the remote? What's the configuration of origin, etc. ?Stephens
(1) What is that backslash doing in your quoted text? Typo in pasting the error message? (Use cut-and-paste to avoid typos.) (2) Did you use git clone --single-branch originally?Hug
@Liam, thanks, after cloning the repo again, I can checkout any branch I want. So still, I would like to work on the same local directory I was working on. What is preventing me from checking out branches?Longley
@Hug - (1) thanks, edited. (2) not, I cloned without the single_branch option.Longley
Hm, it's certainly acting like a single-branch clone, if git fetch does not create-or-update the appropriate remote-tracking names. What goes git config --get-all remote.origin.fetch produce?Hug
@torek, it produces: +refs/heads/dev:refs/remotes/origin/dev. I think I need *, so how can I revert configuration to * ?Longley
Aha. That's the setting for --single-branch. If you didn't use git clone --depth or git clone --single-branch, you must have set it some other way. Set this back to normal, and git fetch will make `git checkout work again. See https://mcmap.net/q/12540/-how-do-i-quot-undo-quot-a-single-branch-clone/1256452Hug
Incredible! Probably my PyCharm changed something in the background. I didn't issue the single_branch command in the command line. You are invited to write it as an answer and I'll mark it as the best one that could help.Longley
K
8

First check whether you have fetched all branches or not by executing following command.

git fetch --all

Check for existence of branch name in local

git branch -a

Execute command to track remote branch and create one in local

git checkout -t origin/<Branch Name>
Karlie answered 3/7, 2020 at 7:19 Comment(0)
F
2

You may have limited fetch configurations for your remote. See .git/config for your remote, it may have something like the following.

[remote "origin"]
    url = <url>
    fetch = +refs/heads/main:refs/remotes/origin/main

As such git fetch will only get main. To fix you can update it to

[remote "origin"]
    url = <url>
    fetch = +refs/heads/*:refs/remotes/origin/*
Ferial answered 4/5, 2023 at 13:22 Comment(0)
G
0

For me works to remove the origin and add it again. Then branch were added git remote remove origin

git remote add origin <url>

git fetch origin

git checkout -b 'feature/XXX' 'origin/feature/XXX'

Greenleaf answered 24/7 at 10:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.