Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8645393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:32:24+00:00 2026-06-12T12:32:24+00:00

In libgit2, how to resolve short ref name to get the full ref name

  • 0

In libgit2, how to resolve short ref name to get the full ref name or hash?
What libgit2 function need to be called?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:32:26+00:00Added an answer on June 12, 2026 at 12:32 pm

    libgit2 supports the full extended SHA-1 syntax through the use of the git_revparse_single() function. In order to retrieve the hash, just pass the retrieved object to the git_object_id()function.

    Leveraging git_revparse_single() will allow one to replicate following standard git command line usages:

    $ git show master
    $ git show heads/master
    $ git show e90810b
    ...
    

    Libgit2 code:

    git_repository* repo;
    git_object *object;
    int error;
    
    ... open existing repository ...
    
    /* Short named references
     * Note: Might be considered ambiguous if tags/master
     * and heads/master both exist
     */
    error = git_revparse_single(&obj, repo, "master"); 
    git_object_free(obj);
    
    /* Less ambiguous name */
    error = git_revparse_single(&obj, repo, "heads/master");
    git_object_free(obj);
    
    /* Short hash as well */
    error = git_revparse_single(&obj, repo, "e90810b"); 
    git_object_free(obj);
    
    /* Complex specs */
    error = git_revparse_single(&obj, repo, "master@{0}~1^1"); 
    git_object_free(obj);
    
    /* Tree entries */
    error = git_revparse_single(&obj, repo, "test/master@{1}:branch_file.txt"); 
    git_object_free(obj);
    

    For more information or examples about usage, you can peek at the unit tests.

    Note: There’s currently no built-in exposed way to retrieve a canonical reference name from a short reference name.

    Update

    The amazing @CarlosMartinNieto made it happen.

    Libgit2 now exposes git_reference_dwim which retrieves a reference by its short name (e.g. master, heads/master, …)

    • Header
    • Libgit 2 commit introducing this change
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use libgit2 to read the name of the current branch.
I am using rugged gem which uses libgit2 to operate on a git repository.
How to clone a repo (with libgit2 ) I want to do exactly what
I want to implement git log -1 fullpath/myfile with libgit2. I am fairly new
I got another libgit2 issue and will be very grateful for your help. I'm
I am trying to get a list of all object IDs in a git
So using LibGit2Sharp https://github.com/libgit2/libgit2sharp you can walk through the branches like this using (var
I want to install git with homebrew. I had MacPorts installed, which I removed
Is there a way to create a new clone of a git repo using
Does anyone know how to create a commit using the ruby bindings (the 'rugged'

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.