Skip to content

Instantly share code, notes, and snippets.

@chester89
Created May 19, 2016 13:02
Show Gist options
  • Save chester89/eca596bd431e78ed8b9112a52ef6975e to your computer and use it in GitHub Desktop.
Save chester89/eca596bd431e78ed8b9112a52ef6975e to your computer and use it in GitHub Desktop.
Working with Git from .NET
static void GitCommit(String message)
{
var processPath = @"C:\Program Files (x86)\Git\bin\git.exe";
var filePath = @"D:\whatever\git-tests\text.txt";
File.AppendAllText(filePath, DateTime.UtcNow.ToString("s") + Environment.NewLine);
using (var process = new Process()
{
StartInfo = new ProcessStartInfo(processPath, string.Format(" commit -am \"{0}\"", message))
{
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
WorkingDirectory = Path.GetDirectoryName(filePath)
}
})
{
process.OutputDataReceived += (sender, args) =>
{
Console.WriteLine("received output from Git process [pid={0}]: {1}", (sender as Process).Id, args.Data);
};
process.ErrorDataReceived += (sender, args) =>
{
Console.WriteLine("received error from Git process [pid={0}]: {1}", (sender as Process).Id, args.Data);
};
process.Start();
Console.WriteLine("Started git");
process.BeginOutputReadLine();
//string stderr_str = process.StandardError.ReadToEnd();
//string stdout_str = process.StandardOutput.ReadToEnd();
process.WaitForExit();
}
}
//usage
GitCommit("commiting from .NET program");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment