Git is the preferred source code version control tool for many programmers, and I've recently switched from TFVC to Git. I am going to show you how to operate Git with .NET Core.
Why am I doing this
First of all, we do have a lot of good Git clients, such as GitHub for Windows, VS/VS Code, git GUI, and so on, all quite mature, there is no need for us to reinvent this wheel in own. NET/C#. But on the server, you might need to manage a Git repository, have your own business logic, the Git repository needs to integrate with other system parts, and even want to write your own GitHub with ASP.NET, so it's useful to manipulate Git with. NET Core.
LibGit2Sharp
We don't need to study the principles of Git from scratch, .NET community already has a library that operates Git: LibGit2Sharp. It supports both the .NET Framework and .NET Core, and that's what we need to use!
We can also find this package on NuGet: https://www.nuget.org/packages/LibGit2Sharp
First of all, let's install it into our .NET Core project.
NuGet Package Manager (Visual Studio)
Install-Package LibGit2Sharp
.NET Core CLI
dotnet add package LibGit2Sharp
Now we have the fundamental library for everything we need.
Clone a repository
Repository.Clone() can download a remote repository to the local file system, same as git clone command.
Repository.Clone("https://github.com/EdiWang/EnvSetup.git", @"D:\EnvSetup");
There are overrides that can set advanced options.
Create a local repository
The Repository.Init() method can create a new Git repository in the specified path, equivalent to the git init command.
Repository.Init(@"D:\GitRocks");
It creates a ".git" hidden folder inside "D:\GitRocks".
Open a local Git repository
The LibGit2Sharp.Repository type represents a Git repository that can be loaded in memory or from a local path, that is, a directory that contains the ".git" folder. As my own blog project D:\GitHub\Moonglade
Because it implements the IDisposable interface, it is recommended that you use the using statement to wrap the operation on Repository to facilitate the release of resources.
Opening the local Git repository is simple, passing the path to Repository's constructor:
using (var repo = new Repository(@"D:\GitHub\Moonglade"))
{
}
Get Branch
The Repository.Branches property contains all the branch information for the current repository. For example, we want to output what local and remote branches are available in the current repository:
using (var repo = new Repository(@"D:\GitHub\Moonglade"))
{
var branches = repo.Branches;
foreach (var b in branches)
{
Console.WriteLine(b.FriendlyName);
}
}
Of course, in addition to the name of the branch, it also includes other information such as Commits under the branch.
Get Commits
We can get the full commit history via Branch.Commits or Repository.Commits
foreach (var commit in repo.Commits)
{
Console.WriteLine(
$"{commit.Id.ToString().Substring(0, 7)} " +
$"{commit.Author.When.ToLocalTime()} " +
$"{commit.MessageShort} " +
$"{commit.Author.Name}");
}
To find a specific commit, use Repository.Lookup<Commit>()
var commit = repo.Lookup<Commit>("9fddbbf");
Console.WriteLine($"Commit Full ID: {commit.Id}");
Console.WriteLine($"Message: {commit.MessageShort}");
Console.WriteLine($"Author: {commit.Author.Name}");
Console.WriteLine($"Time: {commit.Author.When.ToLocalTime()}");
To get the latest commit, use Repository.Head.Tip
var commit = repo.Head.Tip;
Console.WriteLine($"Commit Full ID: {commit.Id}");
Console.WriteLine($"Message: {commit.MessageShort}");
Console.WriteLine($"Author: {commit.Author.Name}");
Console.WriteLine($"Time: {commit.Author.When.ToLocalTime()}");
Get Tags
Just like branches, tags can be accessed via Repository.Tags
foreach (var item in repo.Tags)
{
Console.WriteLine($"{item.FriendlyName} - {item.Target.Id}");
}
Other Operations
The above example demonstrates the most commonly used Git repository information retrieval operations, there are many other operations, such as read and write ignore files via Repository.Ignore, write Commit, compare changes, and so on, you can explore on your own 😀
Reference: http://www.woodwardweb.com/git/getting_started_2.html
Anduin
That was really cool! So I can use my latest git commit ID to check for updates and auto-generate update log.
lindexi
Good. But can it add commit?
WRiver
Thank You! You cure my headache ^^
Sffreeds
It doesn't support SSH Auth with .net core.
waka
Thank You so much, very good article
Jim
Thanks for sharing!
Do you happen to know if one can clone the repository with SSH authentication? I've found very little documentation regarding that.
scott moses
I could not get LibGit2Sharp working with a remote due to lack of SSH