This question is the continuation of this question, As requested I have raised this as a separate question.
How to check whether a commit is present in a Branch in Azure Dev Ops?
Is there any efficient way to get the latest commit of a particular branch using this package?
The below is a C# demo, it should achieve your requirement:
Works Successfully on my side: