Before starting, you need your git client configured.
var credentials = new VssBasicCredential(userName: string.Empty, password: this.config.Key);
this.vssConnection = new VssConnection(new Uri(this.config.BaseUrl), credentials);
if (!string.IsNullOrWhiteSpace(this.config.Key))
{
this.gitClient = this.vssConnection.GetClient<GitHttpClient>(); // Creates a git client.
}
Install Azure DevOps SDK from nuget:
(In your csproj)
<!--Azure DevOps-->
<PackageReference Include="Microsoft.TeamFoundationServer.Client" Version="16.170.0" />
Code:
/// <summary>
/// Restore a deleted branch.
/// </summary>
/// <param name="branch">Branch name</param>
/// <returns>Restore task</returns>
public async Task<bool> RestoreBranch(string branch)
{
var searchCriteria = new GitPushSearchCriteria
{
RefName = $"refs/heads/{branch}",
IncludeRefUpdates = true
};
var pushes = await this.gitClient.GetPushesAsync(
project: this.config.ProjectName,
repositoryId: this.config.RepositoryId,
skip: 0,
top: 1,
searchCriteria: searchCriteria);
if (!pushes.Any() || !pushes.First().RefUpdates.Any())
{
return false;
}
var refUpdate = pushes.First().RefUpdates.First();
var objectId = refUpdate.OldObjectId;
var updates = new List<GitRefUpdate>
{
new GitRefUpdate
{
Name = $"refs/heads/{branch}",
NewObjectId = objectId,
OldObjectId = "0000000000000000000000000000000000000000" // Deleted object.
}
};
var result = await this.gitClient.UpdateRefsAsync(
refUpdates: updates,
repositoryId: this.config.RepositoryId,
project: this.config.ProjectName);
return result.Any() && result.First().Success;
}
After running that, you can run git fetch
locally. Now the delete branch gonna appear.
I just finished reading your blog post on how to restore a deleted branch from Azure DevOps using its SDK, and I must say, it was quite informative and well-written. I appreciate the detailed step-by-step instructions you provided, which made it easy to follow and understand.
The core idea of your blog post, which is to help readers restore a deleted branch using Azure DevOps SDK, is a valuable one. The use of code snippets and clear explanations of each step is a significant strength of your post. I particularly liked the fact that you provided the necessary prerequisites (i.e., having the git client configured) and the installation of Azure DevOps SDK from NuGet. This ensures that readers have everything they need before diving into the actual process.
However, there are a few areas where the post could be improved. Firstly, it would be helpful if you could provide a brief introduction to Azure DevOps and its SDK, as well as why one might need to restore a deleted branch. This would give readers who are not familiar with the topic a better understanding of the context and the importance of your tutorial.
Secondly, in the code snippet, you use the
this.config
object, but you do not explain what it is or how to set it up. It would be beneficial to provide a brief explanation of theconfig
object and its properties, such asBaseUrl
,Key
,ProjectName
, andRepositoryId
. This would help readers understand the code better and ensure they can successfully follow your tutorial.Lastly, it would be helpful to include some error handling in the code snippet. For example, you could provide guidance on what to do if the
RestoreBranch
method returnsfalse
or if there are any exceptions thrown during the process. This would help readers troubleshoot any issues they might encounter while following your tutorial.Overall, your blog post is informative and valuable, and with a few improvements, it could be even more helpful to readers. Keep up the great work!