从C#函数运行git命令 [英] Run git commands from a C# function

查看:1082
本文介绍了从C#函数运行git命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的C#代码检测到跟踪文件中的更改时,如何运行git命令?我正在为此编写一个VisualStudio / C#控制台项目。



我对.NET环境很陌生,目前正致力于将自动GIT提交集成到一个文件夹。我需要自动提交任何更改/添加/删除已知文件夹,并将其推送到git远程。任何指导赞赏。谢谢。



这是我的最后一个,我需要一些指导:


  1. 使用适当的忽略文件(完成)最初在文件夹上设置Git存储库。
  2. 我使用C#FileSystemWatcher捕获对所述文件夹(完成)的任何更改。



  3. $ b
  4. 一旦我的项目检测到更改,就需要提交并推送这些更改p>项目需要运行的暂定命令:

      git add -A 
    git commitexplanations_of_changes
    git push our_remote

    注意:此代码(无用户交互)将是唯一承诺这个回购,所以我不担心冲突,并相信这个流程将起作用。

如果你想在C#中做到这一点,你可以通过Process.Start调用外部git命令,当你检测到文件改变的时候

  string gitCommand =git; 
字符串gitAddArgument = @add -A;
string gitCommitArgument = @commitexplanationations_of_changes
string gitPushArgument = @push our_remote

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

不是最好的解决方案,但它可以在C#中使用


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.

解决方案

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#

这篇关于从C#函数运行git命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆