77

How can I pull (maybe push too) some folder from GitHub?

I mean I need API for .NET to access within C#, not GUI for git.

wonea
  • 4,783
  • 17
  • 86
  • 139
cnd
  • 32,616
  • 62
  • 183
  • 313
  • 3
    @code4life – and anyone else interested in library/tool recommendations – checkout [Software Recommendations Stack Exchange](http://softwarerecs.stackexchange.com/). – Kenny Evitt May 03 '15 at 17:18

3 Answers3

59

As James Manning mentioned in a comment in the currently accepted answer, the library libgit2sharp is an actively supported project providing a .NET API for Git.

Kenny Evitt
  • 9,291
  • 5
  • 65
  • 93
  • Unfortunately though, as at Oct 2013, Pull isn't fully supported as Pull = Fetch + Merge and Merge has yet to be written. Will be awesome when it's complete though! :-) – Russell Giddings Oct 16 '13 at 16:28
  • 3
    **Merge** is now exposed by LibGit2Sharp ;-) (see **[#608](https://github.com/libgit2/libgit2sharp/pull/608)**) – nulltoken Jan 28 '14 at 16:29
  • Unfortunately LibGit2Sharp doesn't include SSH support in the Windows builds. This is a pretty big miss, and although the community seems to have discussed it alot, there is still no final fix. – Doug Feb 10 '18 at 08:11
  • @Doug [This question](https://stackoverflow.com/questions/47463207/which-ssh-private-key-format-for-libgit2-libgit2sharp), and its answers, seem to indicate that SSH *is* supported. – Kenny Evitt Feb 12 '18 at 14:26
  • 2
    @KennyEvitt LibGit2Sharp and LibGit2Sharp-SSH are two different libraries - the latter is a fork of LibGit2Sharp, lacks official support, has broken Nuget packages and is generally an "off the grid" library. When working with low level un-managed code, unless you've got experience wrapping Native functions, you're asking for trouble going off grid (memory leaks, poor exception handling). – Doug Feb 12 '18 at 22:54
  • @Doug Great catch; I missed that the question was about the fork. – Kenny Evitt Feb 13 '18 at 14:21
  • 4
    As of 1/2020 lib2git STILL [does not handle long paths on windows](https://github.com/libgit2/libgit2/issues/3053). If this is a problem you'll need to just shell out to git for windows. – hcoverlambda Jan 16 '20 at 20:06
  • This library doesn't seem to have been [actively developed since abut 2016](https://github.com/libgit2/libgit2sharp/graphs/contributors) – Liam Dec 14 '20 at 16:02
  • 1
    @Liam That doesn't seem to be strictly true. Certainly development is much less frequent and involves smaller changes, but that's pretty typical for 'mature' libraries or programs. Personally, that kind of thing no longer bothers me. I use 'old' code a lot just fine and regularly run into 'new' code that doesn't work at all for me. Usually, the most important criteria is whether *I* run into issues that likely won't be fixed. There *are* a lot of open issues in the GitHub project tho. – Kenny Evitt Dec 14 '20 at 16:13
  • Being as GIT is still being developed this almost certainly doesn't support any of the newer features of GIT – Liam Dec 14 '20 at 16:18
  • 1
    Libgit2sharp is in a lot of places a TON more complex than just passing git commands elsewhere. The simplest way to safely do "git reset --hard" in libgit2sharp is like 20 lines – Hobbamok Jun 30 '22 at 08:21
  • 1
    @Hobbamok That's true but I'd guess it's still worth doing safely 'in code' versus calling the Git CLI. I'd bet that most people aren't 'safely'/'correctly' calling/handling/maintaining the Git CLI via shell commands from their code either. Just having to parse Git CLI output – as text – is a big enough pain that a nice library _could_ be (very) worth using instead. – Kenny Evitt Jun 30 '22 at 15:41
56

What I have done is however to write a simple class libray to call git commands by running child process.

First, create a ProcessStartInfo for some configuration.

ProcessStartInfo gitInfo = new ProcessStartInfo();
gitInfo.CreateNoWindow = true;
gitInfo.RedirectStandardError = true;
gitInfo.RedirectStandardOutput = true;
gitInfo.FileName = YOUR_GIT_INSTALLED_DIRECTORY + @"\bin\git.exe";

Then create a Process to actually run the command.

Process gitProcess = new Process();
gitInfo.Arguments = YOUR_GIT_COMMAND; // such as "fetch orign"
gitInfo.WorkingDirectory = YOUR_GIT_REPOSITORY_PATH;

gitProcess.StartInfo = gitInfo;
gitProcess.Start();

string stderr_str = gitProcess.StandardError.ReadToEnd();  // pick up STDERR
string stdout_str = gitProcess.StandardOutput.ReadToEnd(); // pick up STDOUT

gitProcess.WaitForExit();
gitProcess.Close();

It is then up to you to call whatever command now.

Pok
  • 1,521
  • 1
  • 13
  • 20
  • 15
    I had to add the following line in order for the compiler to be happy with this class: gitInfo.UseShellExecute = false; – J Wynia Jan 03 '13 at 18:56
  • 5
    Wouldn't this run into trouble with ssh, since there's no ssh-agent to provide keys for pulling and pushing? – Chris Moschini Aug 16 '13 at 23:39
  • 2
    See [this answer](http://stackoverflow.com/a/4587739/173497) to the question [How to capture Shell command output in C#?](http://stackoverflow.com/questions/4587415/how-to-capture-shell-command-output-in-c) for details on how to capture both standard-error and standard-output generally; this could fail apparently if the output to standard-error is larger than the relevant buffer. – Kenny Evitt Aug 20 '13 at 19:26
  • 9
    Don't read out and error stream synchronously (lines before WaitForExit). It will deadlock if both have any data. – Aleksander Stankiewicz Aug 20 '15 at 14:41
  • This is a great answer and worked really well for me, note that on .Net Core 1.0 the Close function is not supported for Process – Starjumper Tech SL Mar 25 '19 at 09:03
  • I think you add onto this a little bit and use a shell package like Medallion to make this super easy. – Robert Smith Apr 19 '21 at 16:43
31

I just found this: http://www.eqqon.com/index.php/GitSharp

GitSharp is an implementation of Git for the Dot.Net Framework and Mono. It is aimed to be fully compatible to the original Git and shall be a light weight library for cool applications that are based on Git as their object database or are reading or manipulating repositories in some way...

gnat
  • 6,213
  • 108
  • 53
  • 73
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • 37
    As mentioned on that page, they've stopped development since libgit2sharp looks more promising - https://github.com/libgit2/libgit2sharp/ – James Manning Nov 12 '12 at 17:53
  • 1
    Unfortunately though, as at Oct 2013, Pull isn't fully supported in libgit2sharp as Pull = Fetch + Merge and Merge has yet to be written. Will be awesome when it's complete though! :-) – Russell Giddings Oct 16 '13 at 16:36
  • 4
    @RussellGiddings Things have evolved a bit since: Merge, Pull, Revert,... are now exposed ;-) – nulltoken May 17 '14 at 06:37
  • 2
    These days, libgit2sharp is the better option - see answer below. – Jon Rea Aug 31 '16 at 04:36