libgit2 returned: Refspec 'refs/heads/origin/HEAD' not found error in TortoiseGit

I got this error when starting up TortoiseGit:

Error dialog screenshot

  • git conflict keep original upstream
  • In Perforce, how can I switch between branches like Git?
  • How do I split a Git repository cleanly?
  • Error with gradlew: /usr/bin/env: bash: No such file or directory
  • Specify greater than or equal to Git tag in requirements.txt
  • Do I need to create a changelog file with Git?
  • Could not get all refs.
    libgit2 returned: Refspec ‘refs/heads/origin/HEAD’ not found

    While annoying, it does not prevent me from using TortoiseGit. However, I’d like to make it go away, because it is, well, annoying. How do I fix this?

  • github in ubuntu looking for the application?
  • How do I view all commits for a specific day?
  • Is there a Git Server Side Hook to put quota on repository sizes?
  • Codenvy git push command
  • How to ensure that Visual Studio Online Git Repository is private?
  • How to perform case insensitive diff in Git
  • One Solution collect form web for “libgit2 returned: Refspec 'refs/heads/origin/HEAD' not found error in TortoiseGit”

    Updated Answer

    So it turns out that, for the purpose of updating a local repo’s view of which branch <remote>/HEAD points to, you can have git automatically fetch that information from the remote and set it locally for you, instead of having to manually set it with git symbolic-ref like in my old solution above:

    git remote set-head <remote> --auto
    
    # Or shorter
    git remote set-head <remote> -a
    

    Note that this command doesn’t actually change what the default branch is on the remote repo itself. For that, you’ll probably need to use git symbolic-ref directly on the remote repo, if you have access to it.

    • Documentation for git remote set-head.

    Old Answer

    The problem that the error message refers to is that apparently libgit2 is trying to read the remote default branch pointed to by refs/remotes/origin/HEAD, but the remote branch doesn’t exist, thus the error.

    Using git branch -a, my local repo thinks that origin/develop is the remote default branch:

    remotes/origin/HEAD -> origin/develop
    

    At one point origin/develop was indeed the default branch in my origin repo on GitHub, but it isn’t any longer, the master branch is. The develop branch was deleted from origin since it was no longer needed.

    So I fixed this by manually updating the local reference origin/HEAD to point to the new default branch on origin:

    git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/master
    

    Or if I wanted to also add a message to the reflog for refs/remotes/origin/HEAD:

    git symbolic-ref -m "Update to new remote default branch" \
    refs/remotes/origin/HEAD refs/remotes/origin/master
    

    This resolved the issue. See also:

    1. Documentation for git symbolic-ref.
    2. Git: Correct way to change Active Branch in a bare repository?.
    3. How do I change a Git remote HEAD to point to something besides “master”.
    Git Baby is a git and github fan, let's start git clone.