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

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

问题描述

当我的 C# 代码检测到跟踪文件中的更改时,它如何运行 git 命令?为此,我正在编写 VisualStudio/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.

我是 .NET 环境的新手,目前致力于将自动化 GIT 提交集成到文件夹中.我需要在已知文件夹上自动提交任何更改/添加/删除,并将其推送到 git 远程.任何指导表示赞赏.谢谢.

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 存储库最初设置在具有适当忽略文件的文件夹上(已完成).
  2. 我正在使用 C# FileSystemWatcher 来捕捉对所述文件夹的任何更改(完成).
  3. 一旦我的项目检测到更改,它就需要提交并推送这些更改(待处理).

项目需要运行的暂定命令:

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.

推荐答案

如果你想用C#来做,可以在检测到文件变化时通过Process.Start调用外部git命令

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);

不是最好的解决方案,但它适用于 C#

Not the best solution but it works in C#

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

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