Search code examples
git

Why can't I checkout a remote branch from git?


I have a serious issue with git. My client runs a repository on GitHub. I am working on the main branch and want to checkout a release branch.

The issue: my git-cli can't check this branch out.

I ran:

git branch -a

Result:

* main
  remotes/origin/feature/commander
  remotes/origin/release/0.88
  remotes/origin/release/0.89
  remotes/origin/release/0.89

and so on

So i want to checkout the release-branch 0.89 using several attempts that I found here on StackOverflow and in git-tutorials:

git checkout release/0.89

error: pathspec 'release/0.89' did not match any file(s) known to git

git checkout remotes/release/0.89

error: pathspec 'remotes/release/0.89' did not match any file(s) known to git

I found also something like this, that I usually use to create a new remote branch:

git checkout -b release/0.89 origin/release/0.89

this gives an error:

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

I am using git version 2.34.1 on Ubuntu 22.

What do I wrong?


Solution

  • Okay, so first, it looks like you've wrongly created a local branch with the name remotes/origin/release/0.89. So your first move should be to delete it:

    git branch -D remotes/origin/release/0.89
    

    If that works, then git branch -a will only list one remotes/origin/release/0.89.

    In that case, you can now do this correctly, which is to say:

    git switch release/0.89
    

    That will create a local branch, release/0.89, based off of the remote release/0.89 (and Git will tell you so).