46

How can my C# code run git commands when it detects changes in tracked file? I am writing a VisualStudio/C# console project for this purpose.

I am new to the the .NET environment and currently working on integrating automated GIT commits to a folder. I need to automatically commit any change/add/delete on a known folder and push that to a git remote. Any guidance appreciated. Thank you.

Here is what I have and the last one is the one I need some guidance with:

  1. Git repository initially set up on folder with proper ignore file (done).
  2. I am using C# FileSystemWatcher to catch any changes on said folder (done).
  3. Once my project detects a change it needs to commit and push those changes (pending).

Tentative commands the project needs to run:

git add -A
git commit "explanations_of_changes"
git push our_remote

NOTE: This code (with no user interaction) will be the only entity committing to this repo so I am not worried about conflicts and believe this flow will work.

6 Answers 6

66

I realize this is an old question but I wanted to add the solution I recently came across to help those in the future.

The PowerShell class provides an easy way to interact with git. This is part of the System.Management.Automation namespace in .NET. Note that System.Management.Automation.dll is available via NuGet.

string directory = ""; // directory of the git repository

using (PowerShell powershell = PowerShell.Create()) {
    // this changes from the user folder that PowerShell starts up with to your git repository
    powershell.AddScript($"cd {directory}");

    powershell.AddScript(@"git init");
    powershell.AddScript(@"git add *");
    powershell.AddScript(@"git commit -m 'git commit from PowerShell in C#'");
    powershell.AddScript(@"git push");

    Collection<PSObject> results = powershell.Invoke();
}

In my opinion this is cleaner and nicer than using the Process.Start() approach. You can modify this to your specfic needs by editing the scripts that are added to the powershell object.

As commented by @ArtemIllarionov, powershell.Invoke() does not return errors but the Streams property has output information. Specifically powerShell.Streams.Error for errors.

9
  • How would we do this with something like SourceTree which uses it's own git window?
    – Triynko
    Dec 3, 2018 at 21:18
  • 2
    Source tree is just a visual interface for git. You can run git commands on a repo and not use source tree and those changes will be reflected in source tree.
    – ivcubr
    Dec 3, 2018 at 22:18
  • I mean it doesn't use the standard git window, it uses its own version ming32.exe. It's a bash command window. Commands that I use everyday in that window simply will not work in powershell. For example "target_branch="dev"; git checkout $target_branch" (i.e. assigning and using variables exactly like that), will not work in powershell. So again, the question of how to invoke git from c# is not really answered.
    – Triynko
    Dec 20, 2018 at 6:53
  • 2
    Note: this solution uses System.Management.Automation for which you will need .Net Core 2.0 and up Mar 20, 2019 at 8:15
  • 1
    I tried this and the result of Invoke() contains only output from the last command. Any idea how to get all of the output?
    – Kappacake
    May 16, 2019 at 21:24
31

If you want to do it in C#, you can call the external git command by Process.Start when you detect file change

string gitCommand = "git";
string gitAddArgument = @"add -A";
string gitCommitArgument = @"commit ""explanations_of_changes""";
string gitPushArgument = @"push our_remote";

Process.Start(gitCommand, gitAddArgument);
Process.Start(gitCommand, gitCommitArgument);
Process.Start(gitCommand, gitPushArgument);

Not the best solution but it works in C#

2
  • I used the Process class and this sample to find a solution that worked well for my use case. Thank you.
    – Yuri
    Oct 3, 2014 at 23:23
  • Probably the best solution here, as we directly use git as the command, instead of relying on the cmd. The only adjustment I would make would be to specify the working directory, which can happen by using ProcessStartInfo and specifying the parameters accordingly, and calling Process.Start on that.
    – Rekkon
    Aug 1 at 11:51
17
using System.Diagnostics;
using System.Text;

//Console.WriteLine(CommandOutput("git status"));

public static string CommandOutput(string command,
                                    string workingDirectory = null)
{
    try
    {
        ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + command);

        procStartInfo.RedirectStandardError = procStartInfo.RedirectStandardInput = procStartInfo.RedirectStandardOutput = true;
        procStartInfo.UseShellExecute = false;
        procStartInfo.CreateNoWindow = true;
        if (null != workingDirectory)
        {
            procStartInfo.WorkingDirectory = workingDirectory;
        }

        Process proc = new Process();
        proc.StartInfo = procStartInfo;

        StringBuilder sb = new StringBuilder();
        proc.OutputDataReceived += delegate (object sender, DataReceivedEventArgs e)
        {
            sb.AppendLine(e.Data);
        };
        proc.ErrorDataReceived += delegate (object sender, DataReceivedEventArgs e)
        {
            sb.AppendLine(e.Data);
        };

        proc.Start();
        proc.BeginOutputReadLine();
        proc.BeginErrorReadLine();
        proc.WaitForExit();
        return sb.ToString();
    }
    catch (Exception objException)
    {
        return $"Error in command: {command}, {objException.Message}";
    }
}
2
  • I have been trying to take this approach, but can't figure out authentication if you are running in an environment that is not local and needs to auth.
    – RoboYak
    Sep 4, 2020 at 23:43
  • @Raj thanks so much for this script. It was just what I was looking for and with it I am able to receive all the output received from my git commands using a C# program. I personally used powershell instead of cmd and put the directory of my repository instead of /c and to test it quickly I input the command git diff and printed the output like this: Console.Write(CommandOutput("git diff",strMyRepoPath)); Dec 7, 2021 at 9:09
7

Try LibGit2Sharp, a native implementation of git for .NET:

https://github.com/libgit2/libgit2sharp/

2
  • 11
    Want to point out that LibGit2Sharp does not support SSH and has some other nuances. Spent a few hours trying to get LibGit2Sharp to work and now I have to dump it because of that. Just thought others should know before diving in that it may struggle to handle solutions requiring SSH as well as complex situations like multiple remotes. (It can technically handle them, but it's sure been a pain so far.) For simple stuff, this works well.
    – teynon
    Dec 1, 2020 at 20:29
  • LibGit2Sharp doesn't support shallow clones, that means you cannot clone just the latest snapshot without all the history. So depending on your projects history the cloned data might be quite big and that makes it not very suitable for all use cases where disk space or performance matter.
    – Developer
    Aug 31, 2021 at 6:17
3

One alternative would be to setup Grunt and TaskRunner with your project.

Grunt should be able to provide the automation of detecting changes to a folder(or folders) in your project and execute the appropriate git commands to commit it.

Task Runner allows you to initialize and run Grunt from within Visual Studio.

The Visual Studio team has indicated that Task Runner is going to become integrated into future releases of Visual Studio, so this could be a long term solution.

Note: It has been mentioned in the comments, but I feel it worth mentioning again that auto-commiting anytime a file is saved to the repository isn't best practice. You want functional / atomic code changes to get pushed in, not simple text changes. Auto-Commit at your own risk.

-7

The Package Manager Console is Powershell console. So you can run your git commands from there.

5
  • Thank you Voam. I am new to .NET so just to clarify. I need to have those commands hardcoded so they are run automatically when the files are changed.What you are saying is my code can use Package Manager Console in a way to run those commands runtime as needed?
    – Yuri
    Oct 2, 2014 at 21:25
  • I didn't see the requirement that the process of saving to git would be initiated by Visual Studio itself upon file changes.
    – voam
    Oct 2, 2014 at 21:54
  • Let me know if you have any other thoughts on how to accomplish this.
    – Yuri
    Oct 2, 2014 at 22:45
  • 3
    The OP specifically stated "How can my C# code run git commands", this is not something that can be achieved using the Package Manager Console Mar 20, 2019 at 8:17
  • The package manager console is not in side Yuri's C# code.
    – sanepete
    Apr 11, 2022 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.