How do I get the name of the current branch in libgit2?

Running git branch -a doesn't list HEAD. In libgit2, HEAD isn't considered a valid branch either. It's only a reference.

If you want to discover which reference is the current branch, then you should

  • Load the current HEAD reference (try the git_repository_head() convenience method)
  • Determine its type (using git_reference_type())
  • Depending on its type (GIT_REF_SYMBOLIC or GIT_REF_OID) retrieve one of the following
    • The name of the branch (using git_reference_symbolic_target())
    • The commit being pointed at (using git_reference_target())

Tags:

Libgit2