GitSharp/Examples
From eqqon
Contents |
Playing around with git's objects
Repository
Opening an existing git repository
var repo=new Repository("path/to/repo")
Branch
Get the current branch
var branch=repo.CurrentBranch
Console.WriteLine("Current branch is "+branch.Name
Another way to get the current branch
repo.Head
Get master branch
var master = new Branch(repo, "master");
Get the abbreviated hash of the last commit on master
Console.WriteLine(master.CurrentCommit.ShortHash);
Ref
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.
new Ref(repo, "version1.0.0").Target.IsTag
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:
Console.WriteLine((new Ref(repo, "HEAD^").Target as Commit).Message);
Commit
Actually the example above (to get the message of the previous commit) can be shortened like so:
Console.WriteLine((new Commit(repo, "HEAD^").Message);
Using git commands
Init
Initializing a new repository in the current directory (if GID_DIR environment variable is not set)
Git.Commands.Init();
Initializing a new repository in the specified location
Git.Commands.Init("path/to/repo");
Initializing a new repository with options
var cmd = new Git.InitCommand("path/to/repo") { Quiet=false, Bare=true };
cmd.Execute();