GitSharp/Examples

From eqqon

< GitSharp(Difference between revisions)
Jump to: navigation, search
m
(Clone)
 
(22 intermediate revisions not shown)
Line 1: Line 1:
== Playing around with git's objects ==
== Playing around with git's objects ==
=== Repository ===
=== Repository ===
-
Opening an existing git repository
+
<span class="S0">&nbsp; &nbsp; &nbsp; </span><span class="S2">//Opening an existing git repository</span><br />
-
{{code|var repo<nowiki>=</nowiki>new Repository("path/to/repo")}}
+
<span class="S0">&nbsp; &nbsp; &nbsp; </span>repo<span class="S0"> </span><span class="S10">=</span><span class="S0"> </span><span class="S5">new</span><span class="S0"> </span>Repository<span class="S10">(</span><span class="S6">"path/to/repo"</span><span class="S10">);</span><br />
 +
<br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span><span class="S2">// Now suppose you have created some new files and want to commit them</span><br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span>repo<span class="S10">.</span>Index<span class="S10">.</span>Add<span class="S10">(</span><span class="S6">"README"</span><span class="S10">,</span><span class="S0"> </span><span class="S6">"License.txt"</span><span class="S10">);</span><br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span>Commit<span class="S0"> </span>commit<span class="S0"> </span><span class="S10">=</span><span class="S0"> </span>repo<span class="S10">.</span>Commit<span class="S10">(</span><span class="S6">"My first commit with gitsharp"</span><span class="S10">,</span><span class="S0"> </span><span class="S5">new</span><span class="S0"> </span>Author<span class="S10">(</span><span class="S6">"henon"</span><span class="S10">,</span><span class="S0"> </span><span class="S6">"meinrad.recheis@gmail.com"</span><span class="S10">));</span><br />
 +
<br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span><span class="S2">// Easy, isn't it? Now let's have a look at the changes of this commit:</span><br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span><span class="S5">foreach</span><span class="S0"> </span><span class="S10">(</span>Change<span class="S0"> </span>change<span class="S0"> </span><span class="S5">in</span><span class="S0"> </span>commit<span class="S10">.</span>Changes<span class="S10">)</span><br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; </span>Console<span class="S10">.</span>WriteLine<span class="S10">(</span>change<span class="S10">.</span>Name<span class="S0"> </span><span class="S10">+</span><span class="S0"> </span><span class="S6">" "</span><span class="S0"> </span><span class="S10">+</span><span class="S0"> </span>change<span class="S10">.</span>ChangeType<span class="S10">);</span><br />
 +
<br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span><span class="S2">//Get the staged changes from the index</span><br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span>repo<span class="S10">.</span>Status<span class="S10">.</span>Added<span class="S10">.</span>Contains<span class="S10">(</span><span class="S6">"README"</span><span class="S10">);</span><br />
 +
<br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span><span class="S2">//Access and manipulate the configuration</span><br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span>repo<span class="S10">.</span>Config<span class="S10">[</span><span class="S6">"core.autocrlf"</span><span class="S10">]</span><span class="S0"> </span><span class="S10">=</span><span class="S0"> </span><span class="S6">"false"</span><span class="S10">;</span><br />
-
=== Branch ===
+
=== Commit ===
-
Get the current branch
+
<span class="S0">&nbsp; &nbsp; &nbsp; </span><span class="S2">// Get the message of the previous commit</span><br />
-
{{code|var branch<nowiki>=</nowiki>repo.CurrentBranch<br>
+
<span class="S0">&nbsp; &nbsp; &nbsp; </span><span class="S5">string</span><span class="S0"> </span>msg<span class="S0"> </span><span class="S10">=</span><span class="S0"> </span><span class="S5">new</span><span class="S0"> </span>Commit<span class="S10">(</span>repo<span class="S10">,</span><span class="S0"> </span><span class="S6">"HEAD^"</span><span class="S10">).</span>Message<span class="S10">;</span><br />
-
Console.WriteLine("Current branch is "+branch.Name);}}
+
<span class="S0">&nbsp; &nbsp; &nbsp; </span>Debug<span class="S10">.</span>Assert<span class="S10">(</span>msg<span class="S0"> </span><span class="S10">==</span><span class="S0"> </span>repo<span class="S10">.</span>CurrentBranch<span class="S10">.</span>CurrentCommit<span class="S10">.</span>Parent<span class="S10">.</span>Message<span class="S10">);</span><br />
 +
<br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span><span class="S2">//Print a list of changes between two commits c1 and c2:</span><br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span>Commit<span class="S0"> </span>c1<span class="S0"> </span><span class="S10">=</span><span class="S0"> </span>repo<span class="S10">.</span>Get<span class="S10">&lt;</span>Commit<span class="S10">&gt;(</span><span class="S0"> </span><span class="S6">"979829389f136bfabb5956c68d909e7bf3092a4e"</span><span class="S10">);</span><span class="S0"> </span><span class="S2">// &lt;-- note: short hashes are not yet supported</span><br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span>Commit<span class="S0"> </span>c2<span class="S0"> </span><span class="S10">=</span><span class="S0"> </span><span class="S5">new</span><span class="S0"> </span>Commit<span class="S10">(</span>repo<span class="S10">,</span><span class="S0"> </span><span class="S6">"4a7455c2f23e0f7356877d1813594041c56e2db9"</span><span class="S10">);</span><br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span><span class="S5">foreach</span><span class="S0"> </span><span class="S10">(</span>Change<span class="S0"> </span>change<span class="S0"> </span><span class="S5">in</span><span class="S0"> </span>c1<span class="S10">.</span>CompareAgainst<span class="S10">(</span>c2<span class="S10">))</span><br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; </span>Console<span class="S10">.</span>WriteLine<span class="S10">(</span>change<span class="S10">.</span>ChangeType<span class="S0"> </span><span class="S10">+</span><span class="S0"> </span><span class="S6">": "</span><span class="S0"> </span><span class="S10">+</span><span class="S0"> </span>change<span class="S10">.</span>Path<span class="S10">);</span><br />
 +
<br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span><span class="S2">//Print all previous commits of HEAD of the repository repo</span><br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span><span class="S5">foreach</span><span class="S0"> </span><span class="S10">(</span>Commit<span class="S0"> </span>commit<span class="S0"> </span><span class="S5">in</span><span class="S0"> </span>repo<span class="S10">.</span>Head<span class="S10">.</span>CurrentCommit<span class="S10">.</span>Ancestors<span class="S10">)</span><br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; </span>Console<span class="S10">.</span>WriteLine<span class="S10">(</span>commit<span class="S10">.</span>ShortHash<span class="S0"> </span><span class="S10">+</span><span class="S0"> </span><span class="S6">": "</span><span class="S0"> </span><span class="S10">+</span><span class="S0"> </span>commit<span class="S10">.</span>Message<span class="S0"> </span><span class="S10">+</span><span class="S0"> </span><span class="S6">", "</span><span class="S0"> </span><span class="S10">+</span><span class="S0"> </span>commit<span class="S10">.</span>Author<span class="S10">.</span>Name<span class="S0"> </span><span class="S10">+</span><span class="S0"> </span><span class="S6">", "</span><span class="S0"> </span><span class="S10">+</span><span class="S0"> </span>commit<span class="S10">.</span>AuthorDate<span class="S10">);</span><br />
-
Another way to get the current branch
+
=== Tree & Leaf ===
-
{{code|repo.Head}}
+
<span class="S0">&nbsp; &nbsp; &nbsp; </span><span class="S2">//Get the root tree of the most recent commit</span><br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span>var<span class="S0"> </span>tree<span class="S0"> </span><span class="S10">=</span><span class="S0"> </span>repo<span class="S10">.</span>Head<span class="S10">.</span>CurrentCommit<span class="S10">.</span>Tree<span class="S10">;</span><br />
 +
<br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span><span class="S2">//It has no Parent so IsRoot should be true</span><br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span>Debug<span class="S10">.</span>Assert<span class="S10">(</span>tree<span class="S10">.</span>Parent<span class="S0"> </span><span class="S10">==</span><span class="S0"> </span><span class="S5">null</span><span class="S10">);</span><br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span>Debug<span class="S10">.</span>Assert<span class="S10">(</span>tree<span class="S10">.</span>IsRoot<span class="S10">);</span><br />
 +
<br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span><span class="S2">//Now you can browse throught that tree by iterating over its child trees</span><br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span><span class="S5">foreach</span><span class="S0"> </span><span class="S10">(</span>Tree<span class="S0"> </span>subtree<span class="S0"> </span><span class="S5">in</span><span class="S0"> </span>tree<span class="S10">.</span>Trees<span class="S10">)</span><br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; </span>Console<span class="S10">.</span>WriteLine<span class="S10">(</span>subtree<span class="S10">.</span>Path<span class="S10">);</span><br />
 +
<br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span><span class="S2">//Or printing the names of the files it contains</span><br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span><span class="S5">foreach</span><span class="S0"> </span><span class="S10">(</span>Leaf<span class="S0"> </span>leaf<span class="S0"> </span><span class="S5">in</span><span class="S0"> </span>tree<span class="S10">.</span>Leaves<span class="S10">)</span><br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; &nbsp; </span>Console<span class="S10">.</span>WriteLine<span class="S10">(</span>leaf<span class="S10">.</span>Path<span class="S10">);</span><br />
-
Get master branch
+
=== Blob ===
-
{{code|var master <nowiki>=</nowiki> new Branch(repo, "master");}}
+
-
Get the abbreviated hash of the last commit on master
+
<span class="S0">&nbsp; &nbsp; &nbsp; </span><span class="S2">//A Leaf is a Blob and inherits from it a method to retrieve the data as a UTF8 encoded string:</span><br />
-
{{code|master.CurrentCommit.ShortHash}}
+
<span class="S0">&nbsp; &nbsp; &nbsp; </span><span class="S5">string</span><span class="S0"> </span>string_data<span class="S0"> </span><span class="S10">=</span><span class="S0"> </span><span class="S5">new</span><span class="S0"> </span>Blob<span class="S10">(</span>repo<span class="S10">,</span><span class="S0"> </span><span class="S6">"49322bb17d3acc9146f98c97d078513228bbf3c0"</span><span class="S10">).</span>Data<span class="S10">;</span><br />
 +
<br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span><span class="S2">// Blob also let's you access the raw data as byte array</span><br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span><span class="S5">byte</span><span class="S10">[]</span><span class="S0"> </span>byte_data<span class="S0"> </span><span class="S10">=</span><span class="S0"> </span><span class="S5">new</span><span class="S0"> </span>Blob<span class="S10">(</span>repo<span class="S10">,</span><span class="S0"> </span><span class="S6">"49322bb17d3acc9146f98c97d078513228bbf3c0"</span><span class="S10">).</span>RawData<span class="S10">;</span><br />
-
=== Ref ===
+
=== Branch ===
-
You can use Ref to resolve typical git references without needing to know their type. The following expression should be true if "version1.0.0" is a Tag and not a Branch.
+
<span class="S0">&nbsp; &nbsp; &nbsp; </span><span class="S2">//Get the current branch</span><br />
-
{{code|new Ref(repo, "version1.0.0").Target.IsTag}}
+
<span class="S0">&nbsp; &nbsp; &nbsp; </span>var<span class="S0"> </span>branch<span class="S0"> </span><span class="S10">=</span><span class="S0"> </span>repo<span class="S10">.</span>CurrentBranch<span class="S10">;</span><br />
-
 
+
<span class="S0">&nbsp; &nbsp; &nbsp; </span>Console<span class="S10">.</span>WriteLine<span class="S10">(</span><span class="S6">"Current branch is "</span><span class="S0"> </span><span class="S10">+</span><span class="S0"> </span>branch<span class="S10">.</span>Name<span class="S10">);</span><br />
-
Branch is a Ref, but not all Ref's necessarily need to be branches. For instance, we want to get the message of the previous commit:
+
<br />
-
{{code|(new Ref(repo, "HEAD^").Target as Commit).Message}}
+
<span class="S0">&nbsp; &nbsp; &nbsp; </span><span class="S2">//Another way to get the current branch</span><br />
-
 
+
<span class="S0">&nbsp; &nbsp; &nbsp; </span>Branch<span class="S0"> </span>head<span class="S0"> </span><span class="S10">=</span><span class="S0"> </span>repo<span class="S10">.</span>Head<span class="S10">;</span><br />
-
=== Commit ===
+
<br />
-
Actually the example above (to get the message of the previous commit) can be shortened like so:
+
<span class="S0">&nbsp; &nbsp; &nbsp; </span><span class="S2">// check if head == master</span><br />
-
{{code|new Commit(repo, "HEAD^").Message}}
+
<span class="S0">&nbsp; &nbsp; &nbsp; </span>Debug<span class="S10">.</span>Assert<span class="S10">(</span>head<span class="S10">.</span>Name<span class="S0"> </span><span class="S10">==</span><span class="S0"> </span><span class="S6">"master"</span><span class="S10">);</span><br />
-
 
+
<br />
-
Print a list of changes between two commits c1 and c2:
+
<span class="S0">&nbsp; &nbsp; &nbsp; </span><span class="S2">//Get master branch</span><br />
-
{{code|foreach(var change in c1.CompareAgainst(c2))<br>
+
<br />
-
&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(change.ChangeType+": "+change.Path);}}
+
<span class="S0">&nbsp; &nbsp; &nbsp; </span>var<span class="S0"> </span>master<span class="S0"> </span><span class="S10">=</span><span class="S0"> </span><span class="S5">new</span><span class="S0"> </span>Branch<span class="S10">(</span>repo<span class="S10">,</span><span class="S0"> </span><span class="S6">"master"</span><span class="S10">);</span><br />
-
 
+
<span class="S0">&nbsp; &nbsp; &nbsp; </span>Debug<span class="S10">.</span>Assert<span class="S10">(</span>master<span class="S0"> </span><span class="S10">==</span><span class="S0"> </span>repo<span class="S10">.</span>Get<span class="S10">&lt;</span>Branch<span class="S10">&gt;(</span><span class="S6">"master"</span><span class="S10">));</span><br />
-
Print all previous commits of HEAD of the repository repo
+
<br />
-
{{code|foreach(var commit in repo.Head.CurrentCommit.Ancestors)<br>
+
<span class="S0">&nbsp; &nbsp; &nbsp; </span><span class="S2">//Get the abbreviated hash of the last commit on master</span><br />
-
&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(commit.ShortHash+": "+commit.Message+", "+commit.Author.Name+", "+commit.AuthoredDate);}}
+
<span class="S0">&nbsp; &nbsp; &nbsp; </span>Console<span class="S10">.</span>WriteLine<span class="S10">(</span>master<span class="S10">.</span>CurrentCommit<span class="S10">.</span>ShortHash<span class="S10">);</span><br />
-
 
+
<br />
-
=== Tree & Leaf ===
+
<span class="S0">&nbsp; &nbsp; &nbsp; </span><span class="S2">//Create a new branch</span><br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span>var<span class="S0"> </span>b<span class="S0"> </span><span class="S10">=</span><span class="S0"> </span>GitSharp<span class="S10">.</span>Branch<span class="S10">.</span>Create<span class="S10">(</span>repo<span class="S10">,</span><span class="S0"> </span><span class="S6">"foo"</span><span class="S10">);</span><br />
 +
<br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span><span class="S2">// Switching to our new branch</span><br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span>b<span class="S10">.</span>Checkout<span class="S10">();</span><br />
 +
<br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span><span class="S2">//Check if foo is current branch</span><br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span>Debug<span class="S10">.</span>Assert<span class="S10">(</span>b<span class="S10">.</span>IsCurrent<span class="S10">);</span><br />
 +
<br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span><span class="S2">//Reset the branch to a previous commit (hard or soft or mixed)</span><br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span>master<span class="S10">.</span>Reset<span class="S10">(</span><span class="S6">"HEAD^"</span><span class="S10">,</span><span class="S0"> </span>ResetBehavior<span class="S10">.</span>Hard<span class="S10">);</span><br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span>master<span class="S10">.</span>Reset<span class="S10">(</span><span class="S6">"49322bb17d3acc9146f98c97d078513228bbf3c0"</span><span class="S10">,</span><span class="S0"> </span>ResetBehavior<span class="S10">.</span>Soft<span class="S10">);</span><br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span>master<span class="S10">.</span>Reset<span class="S10">(</span><span class="S6">"master"</span><span class="S10">,</span><span class="S0"> </span>ResetBehavior<span class="S10">.</span>Mixed<span class="S10">);</span><br />
== Using git commands ==
== Using git commands ==
=== Init ===
=== Init ===
-
Initializing a new repository in the current directory (if GID_DIR environment variable is not set)
+
<span class="S0">&nbsp; &nbsp; &nbsp; </span><span class="S2">//Initializing a new repository in the current directory (if GID_DIR environment variable is not set)</span><br />
-
{{code|Git.Commands.Init();}}
+
<span class="S0">&nbsp; &nbsp; &nbsp; </span>Git<span class="S10">.</span>Init<span class="S10">(</span><span class="S6">"."</span><span class="S10">);</span><br />
 +
<br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span><span class="S2">//Initializing a new repository in the specified location</span><br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span>Git<span class="S10">.</span>Init<span class="S10">(</span><span class="S6">"path/to/repo"</span><span class="S10">);</span><br />
 +
<br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span><span class="S2">//Initializing a new repository with options</span><br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span>var<span class="S0"> </span>cmd<span class="S0"> </span><span class="S10">=</span><span class="S0"> </span><span class="S5">new</span><span class="S0"> </span>InitCommand<span class="S0"> </span><span class="S10">{</span><span class="S0"> </span>GitDirectory<span class="S0"> </span><span class="S10">=</span><span class="S6">"path/to/repo"</span><span class="S10">,</span><span class="S0"> </span>Quiet<span class="S0"> </span><span class="S10">=</span><span class="S0"> </span><span class="S5">false</span><span class="S10">,</span><span class="S0"> </span>Bare<span class="S0"> </span><span class="S10">=</span><span class="S0"> </span><span class="S5">true</span><span class="S0"> </span><span class="S10">};</span><br />
 +
<span class="S0">&nbsp; &nbsp; &nbsp; </span>cmd<span class="S10">.</span>Execute<span class="S10">();</span><br />
-
Initializing a new repository in the specified location
+
== Clone ==
-
{{code|Git.Commands.Init("path/to/repo");}}
+
-
Initializing a new repository with options
+
<span class="S0">&nbsp; &nbsp; &nbsp; </span><span class="S2">//Clone a repository from a public repository via http</span><br />
-
{{code|<nowiki>
+
<span class="S0">&nbsp; &nbsp; &nbsp; </span>Git<span class="S10">.</span>Clone<span class="S10">(</span><span class="S6">"git://github.com/henon/GitSharp.git"</span><span class="S10">,</span><span class="S0"> </span><span class="S6">"path/to/local/copy"</span><span class="S10">);</span><br />
-
var cmd = new Git.InitCommand("path/to/repo") { Quiet=false, Bare=true };</nowiki><br>
+
<br />
-
cmd.Execute();
+
<span class="S0">&nbsp; &nbsp; &nbsp; </span><span class="S2">// Or using options</span><br />
-
}}
+
<span class="S0">&nbsp; &nbsp; &nbsp; </span>Git<span class="S10">.</span>Clone<span class="S10">(</span><span class="S5">new</span><span class="S0"> </span>CloneCommand<span class="S0"> </span><span class="S10">{</span><span class="S0"> </span>Source<span class="S0"> </span><span class="S10">=</span><span class="S0"> </span><span class="S6">"git://github.com/henon/GitSharp.git"</span><span class="S10">,</span><span class="S0"> </span>GitDirectory<span class="S0"> </span><span class="S10">=</span><span class="S0"> </span><span class="S6">"path/to/local/copy"</span><span class="S10">,</span><span class="S0"> </span>Quiet<span class="S0"> </span><span class="S10">=</span><span class="S0"> </span><span class="S5">false</span><span class="S10">,</span><span class="S0"> </span>Bare<span class="S0"> </span><span class="S10">=</span><span class="S0"> </span><span class="S5">true</span><span class="S0"> </span><span class="S10">});</span><br />
 +
<br />

Latest revision as of 10:58, 5 June 2010

Contents

Playing around with git's objects

Repository

      //Opening an existing git repository
      repo = new Repository("path/to/repo");

      // Now suppose you have created some new files and want to commit them
      repo.Index.Add("README", "License.txt");
      Commit commit = repo.Commit("My first commit with gitsharp", new Author("henon", "meinrad.recheis@gmail.com"));

      // Easy, isn't it? Now let's have a look at the changes of this commit:
      foreach (Change change in commit.Changes)
        Console.WriteLine(change.Name + " " + change.ChangeType);

      //Get the staged changes from the index
      repo.Status.Added.Contains("README");

      //Access and manipulate the configuration
      repo.Config["core.autocrlf"] = "false";

Commit

      // Get the message of the previous commit
      string msg = new Commit(repo, "HEAD^").Message;
      Debug.Assert(msg == repo.CurrentBranch.CurrentCommit.Parent.Message);

      //Print a list of changes between two commits c1 and c2:
      Commit c1 = repo.Get<Commit>( "979829389f136bfabb5956c68d909e7bf3092a4e"); // <-- note: short hashes are not yet supported
      Commit c2 = new Commit(repo, "4a7455c2f23e0f7356877d1813594041c56e2db9");
      foreach (Change change in c1.CompareAgainst(c2))
        Console.WriteLine(change.ChangeType + ": " + change.Path);

      //Print all previous commits of HEAD of the repository repo
      foreach (Commit commit in repo.Head.CurrentCommit.Ancestors)
        Console.WriteLine(commit.ShortHash + ": " + commit.Message + ", " + commit.Author.Name + ", " + commit.AuthorDate);

Tree & Leaf

      //Get the root tree of the most recent commit
      var tree = repo.Head.CurrentCommit.Tree;

      //It has no Parent so IsRoot should be true
      Debug.Assert(tree.Parent == null);
      Debug.Assert(tree.IsRoot);

      //Now you can browse throught that tree by iterating over its child trees
      foreach (Tree subtree in tree.Trees)
        Console.WriteLine(subtree.Path);

      //Or printing the names of the files it contains
      foreach (Leaf leaf in tree.Leaves)
        Console.WriteLine(leaf.Path);

Blob

      //A Leaf is a Blob and inherits from it a method to retrieve the data as a UTF8 encoded string:
      string string_data = new Blob(repo, "49322bb17d3acc9146f98c97d078513228bbf3c0").Data;

      // Blob also let's you access the raw data as byte array
      byte[] byte_data = new Blob(repo, "49322bb17d3acc9146f98c97d078513228bbf3c0").RawData;

Branch

      //Get the current branch
      var branch = repo.CurrentBranch;
      Console.WriteLine("Current branch is " + branch.Name);

      //Another way to get the current branch
      Branch head = repo.Head;

      // check if head == master
      Debug.Assert(head.Name == "master");

      //Get master branch

      var master = new Branch(repo, "master");
      Debug.Assert(master == repo.Get<Branch>("master"));

      //Get the abbreviated hash of the last commit on master
      Console.WriteLine(master.CurrentCommit.ShortHash);

      //Create a new branch
      var b = GitSharp.Branch.Create(repo, "foo");

      // Switching to our new branch
      b.Checkout();

      //Check if foo is current branch
      Debug.Assert(b.IsCurrent);

      //Reset the branch to a previous commit (hard or soft or mixed)
      master.Reset("HEAD^", ResetBehavior.Hard);
      master.Reset("49322bb17d3acc9146f98c97d078513228bbf3c0", ResetBehavior.Soft);
      master.Reset("master", ResetBehavior.Mixed);

Using git commands

Init

      //Initializing a new repository in the current directory (if GID_DIR environment variable is not set)
      Git.Init(".");

      //Initializing a new repository in the specified location
      Git.Init("path/to/repo");

      //Initializing a new repository with options
      var cmd = new InitCommand { GitDirectory ="path/to/repo", Quiet = false, Bare = true };
      cmd.Execute();

Clone

      //Clone a repository from a public repository via http
      Git.Clone("git://github.com/henon/GitSharp.git", "path/to/local/copy");

      // Or using options
      Git.Clone(new CloneCommand { Source = "git://github.com/henon/GitSharp.git", GitDirectory = "path/to/local/copy", Quiet = false, Bare = true });