How to get commit ID for a tag in Azure Devops git repository.

Thomas Murnane 81 Reputation points
2025-04-23T12:23:54.9466667+00:00

I am trying to automate a view of all the workitems that have been closed between 2 branch tags of a git repo. One of the main issues I'm having is for some reason, when I view the tags from the tags page, I can see the commit id of the tags:
User's image

However, I can't for the life of me figure out how to get that commit id from a tag via REST API call.
I'm getting a list of the tags with the following:

$tagsUrl = "https://dev.azure.com/$Organization/$SystemTeamProject/_apis/git/repositories/$repositoryId/refs?filter=tags/&api-version=7.1-preview.1"
$tagsResponse = Invoke-RestMethod -Uri $tagsUrl -Method Get -Headers $headers
$allTags = $tagsResponse.value | ForEach-Object { $_.name -replace "refs/tags/", "" }

All the AI models retrieve it the same way, but it returns the "objectId" of that tag, which does not match the commit id!
https://dev.azure.com/{org}/{proj}/_apis/git/repositories/{repo/refs?filter=tags/24.2.1.601-RELEASE-CANDIDATE&api-version=7.1-preview.1
User's image

That does not match the commit id.

The initial tags are created via a PowerShell script once we create our release branch:

        git tag -a $releaseInfo.ReleaseTag -m "Tagging release branch"
        git push -q origin $releaseInfo.ReleaseTag
Azure DevOps
{count} votes

Accepted answer
  1. Bodapati Harish 400 Reputation points Microsoft External Staff Moderator
    2025-04-24T06:07:15.5166667+00:00

    Hello Thomas Murnane,

    To get the commit ID, use the /annotatedtags endpoint with the tag’s objectId. below is simple PowerShell script to do it:

    
    $organization = "your-org"
    
    $project = "your-project"
    
    $repositoryId = "your-repo-id"
    
    $pat = "your-personal-access-token"
    
    $headers = @{ Authorization = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$pat")) }
    
    # Get the tags
    
    $tagsUrl = "https://dev.azure.com/$organization/$project/_apis/git/repositories/$repositoryId/refs?filter=tags/&api-version=7.1-preview.1"
    
    $tagsResponse = Invoke-RestMethod -Uri $tagsUrl -Method Get -Headers $headers
    
    foreach ($tag in $tagsResponse.value) {
    
        $tagName = $tag.name -replace "refs/tags/", ""
    
        $tagObjectId = $tag.objectId
    
        # Get the commit ID
    
        $annotatedTagUrl = "https://dev.azure.com/$organization/$project/_apis/git/repositories/$repositoryId/annotatedtags/$tagObjectId?api-version=7.1-preview.1"
    
        $annotatedTagResponse = Invoke-RestMethod -Uri $annotatedTagUrl -Method Get -Headers $headers
    
        $commitId = $annotatedTagResponse.taggedObject.objectId
    
        Write-Output "Tag: $tagName, Commit ID: $commitId"
    
    }
    

    This will give you the commit ID (like 418392bc for 24.2.1.601-RELEASE-CANDIDATE) that matches the UI.

    Hope this helps!

    If the answer is helpful, please click Accept Answer and kindly upvote it. If you have any further questions about this answer, please click Comment.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Thomas Murnane 81 Reputation points
    2025-04-24T11:12:10.6366667+00:00

    I had this in my browser before I saw your answer, and I posted it. When the page refreshed, I saw your answer. I couldn't see a way to delete my post, but at least this shows another way.
    I found that using the following API call, I was able to get the commit IDs from the results of this call:

    function Get-CommitId {
        param (
            [string]$baseTag,
            [string]$targetTag
        )
    
        $uri = "https://dev.azure.com/$($Global:Organization)/$($Global:SystemTeamProject)/_apis/git/repositories/$($Global:repoName)/diffs/commits" +
            "?baseVersion=$baseTag" +
            "&baseVersionType=tag" +
            "&targetVersion=$targetTag" +
            "&targetVersionType=tag" +
            "&api-version=7.1"
    
        $response = Invoke-RestMethod -Uri $uri -Headers $Global:headers -Method Get
    
        return @{
            BaseCommit   = $response.baseCommit
            TargetCommit = $response.targetCommit
        }
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.