Investing TS
Investing TS

Reputation: 314

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

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?

Upvotes: 12

Views: 23185

Answers (3)

Igor Nosovskyi
Igor Nosovskyi

Reputation: 124

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'

Upvotes: 0

James PiFL Tumber
James PiFL Tumber

Reputation: 33

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/*

Upvotes: 2

Sohan Kumar
Sohan Kumar

Reputation: 105

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>

Upvotes: 9

Related Questions