10

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?

Investing TS
  • 184
  • 1
  • 2
  • 13
  • 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. ? – Liam Aug 07 '19 at 07: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? – torek Aug 07 '19 at 09: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? – Investing TS Aug 07 '19 at 09:42
  • @torek - (1) thanks, edited. (2) not, I cloned without the ```single_branch``` option. – Investing TS Aug 07 '19 at 09:43
  • 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? – torek Aug 07 '19 at 09:47
  • @torek, it produces: ```+refs/heads/dev:refs/remotes/origin/dev```. I think I need ```*```, so how can I revert configuration to * ? – Investing TS Aug 07 '19 at 09:49
  • 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 https://stackoverflow.com/q/17714159/1256452 – torek Aug 07 '19 at 09:51
  • 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. – Investing TS Aug 07 '19 at 09:58

2 Answers2

7

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>
Sohan Kumar
  • 85
  • 1
  • 9
0

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