HansPeterGit 0.1.12
dotnet add package HansPeterGit --version 0.1.12
NuGet\Install-Package HansPeterGit -Version 0.1.12
<PackageReference Include="HansPeterGit" Version="0.1.12" />
paket add HansPeterGit --version 0.1.12
#r "nuget: HansPeterGit, 0.1.12"
// Install HansPeterGit as a Cake Addin #addin nuget:?package=HansPeterGit&version=0.1.12 // Install HansPeterGit as a Cake Tool #tool nuget:?package=HansPeterGit&version=0.1.12
HansPeterGit
GIT interface for .NET
This repository contains a cross-platform interface to the git CLI (aka git.exe/git).
The project aim is to get rid of the stale library libgit2sharp
and provide an easy interface to interact with the git cli.
Dependencies
An installed version of git should be accessible via path or can be defined via the GitOptions.PathToGit
property.
Install
Add the NuGet package HansPeterGit to any project supporting .NET Standard 2.0 or higher.
> dotnet add package HansPeterGit
Usage
Clone a repository
GitRepository.Clone() can download a remote repository to the local file system, same as the git clone command.
GitRepository.Clone("https://github.com/twenzel/HansPeterGit.git", @"D:\TestSample");
Create a local repository
The GitRepository.Init() method can create a new Git repository in the specified path, equivalent to the git init command.
var repos = new GitRepository(@"D:\TestSample");
repos.Init();
Get status
Using the GetStatus() method to retrieve the repositories current status.
var repos = new GitRepository(@"D:\TestSample");
var status = repos.GetStatus();
Console.WriteLine($"Current branch: {status.Branch}");
Console.WriteLine($"Current commit: {status.Commit}");
if (status.IsDirty)
{
var newfile = status.Added.First();
}
var fileInfo = status["src/newFile.json"];
Get branches
Using the GetBranches() method to retrieve the local branches of the current repository.
var repos = new GitRepository(@"D:\TestSample");
var status = repos.GetBranches();
Console.WriteLine("Branches:");
foreach (var branch in branches)
{
Console.WriteLine($"{branch.Name}");
}
To retrieve the remote branches use the GetBranches(bool) method with the value "true" ("false" will retrieve the local branches).
var repos = new GitRepository(@"D:\TestSample");
var status = repos.GetBranches(true);
Console.WriteLine("Remote branches:");
foreach (var branch in branches)
{
Console.WriteLine($"{branch.Name}");
}
To retrieve only branch names please use the GetBranchNames(bool) method.
Staging/Restore
To stage the current work tree use any of the Stage/Add/Restore methods.
repos.StageAll();
// Stage file
repos.Add("src/newFile.json");
// Discard changes in working directory
repos.Restore("src/newFile.json");
// Unstage file
repos.Unstage("src/newFile.json");
Commit changes
The Commit() method can be used to commit the current changes.
var commit = repos.Commit("Fix calculation bug");
// with dedicated author
repos.Commit("Fix calculation bug", new Author("Some name", "bugfixer@test.com"));
Push changes
To push changes from the local repository please use the Push() methods.
repos.Push();
// with dedicated remove name
repos.Push("origin", "main");
// with setting an upstream
repos.PushWithUpstream("origin", "main");
Others
There are already some other methods but not all commands are wrapped/implemented yet. Please feel free to create an issue or contribute.
For any other git command you can use the generic ExecuteCommand
method.
var result = repos.ExecuteCommand("revert");
Authentication
Sometimes the remote repository requires an authentication (e.g. GitHub, Azure DevOps). In order to clone or push the repository you can define an authentication.
var options = new GitOptions(workingDirectory);
options.Authentication = new BasicAuthentication("pat", "myuserpat");
var repository = GitRepository.Clone("https://dev.azure.com/yourOrgName/yourProject/_git/yourRepository", options);
Beside BasicAuthentication
an BearerAuthentication
is also already build-in. You can implement your own authentication by implementing the IAuthentication
interface.
The build-in authentication implementation uses the '-c http.extraheader' argument of the git cli to provide the credentials.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.1)
-
net6.0
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.