13

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?

8
  • 1
    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. ? Commented Aug 7, 2019 at 7:53
  • (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? Commented Aug 7, 2019 at 9:34
  • @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? Commented Aug 7, 2019 at 9:42
  • @torek - (1) thanks, edited. (2) not, I cloned without the single_branch option. Commented Aug 7, 2019 at 9:43
  • 3
    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 stackoverflow.com/q/17714159/1256452 Commented Aug 7, 2019 at 9:51

3 Answers 3

9

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> 
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

1

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'

1 Comment

Thank you, this is the only solution that worked for me. In my case I think I made a mistake by doing a git pull --unshallow, which apparently prevented me from fetching from origin.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.