The first couple of lines open a Git repository.
The git_repository type represents a handle to a repository with a cache in memory.
This is the simplest method, for when you know the exact path to a repository’s working directory or .git folder.
There’s also the git_repository_open_ext which includes options for searching, git_clone and friends for making a local clone of a remote repository, and git_repository_init for creating an entirely new repository.

The second chunk of code uses rev-parse syntax (see Branch References for more on this) to get the commit that HEAD eventually points to.
The type returned is a git_object pointer, which represents something that exists in the Git object database for a repository.
git_object is actually a “parent” type for several different kinds of objects; the memory layout for each of the “child” types is the same as for git_object, so you can safely cast to the right one.
In this case, git_object_type(commit) would return GIT_OBJ_COMMIT, so it’s safe to cast to a git_commit pointer.

The next chunk shows how to access the commit’s properties.
The last line here uses a git_oid type; this is Libgit2’s representation for a SHA-1 hash.

From this sample, a couple of patterns have started to emerge:

If you declare a pointer and pass a reference to it into a Libgit2 call, that call will probably return an integer error code.
A 0 value indicates success; anything less is an error.

If Libgit2 populates a pointer for you, you’re responsible for freeing it.

If Libgit2 returns a const pointer from a call, you don’t have to free it, but it will become invalid when the object it belongs to is freed.

Writing C is a bit painful.

That last one means it isn’t very probable that you’ll be writing C when using Libgit2.
Fortunately, there are a number of language-specific bindings available that make it fairly easy to work with Git repositories from your specific language and environment.
Let’s take a look at the above example written using the Ruby bindings for Libgit2, which are named Rugged, and can be found at https://github.com/libgit2/rugged.

As you can see, the code is much less cluttered.
Firstly, Rugged uses exceptions; it can raise things like ConfigError or ObjectError to signal error conditions.
Secondly, there’s no explicit freeing of resources, since Ruby is garbage-collected.
Let’s take a look at a slightly more complicated example: crafting a commit from scratch

Populate the index with the head commit’s tree, and add the new file at the path newfile.txt.

This creates a new tree in the ODB, and uses it for the new commit.

We use the same signature for both the author and committer fields.

The commit message.

When creating a commit, you have to specify the new commit’s parents.
This uses the tip of HEAD for the single parent.

Rugged (and Libgit2) can optionally update a reference when making a commit.

The return value is the SHA-1 hash of a new commit object, which you can then use to get a Commit object.

The Ruby code is nice and clean, but since Libgit2 is doing the heavy lifting, this code will run pretty fast, too.
If you’re not a rubyist, we touch on some other bindings in Other Bindings.

Advanced Functionality

Libgit2 has a couple of capabilities that are outside the scope of core Git.
One example is pluggability: Libgit2 allows you to provide custom “backends” for several types of operation, so you can store things in a different way than stock Git does.
Libgit2 allows custom backends for configuration, ref storage, and the object database, among other things.

Let’s take a look at how this works.
The code below is borrowed from the set of backend examples provided by the Libgit2 team (which can be found at https://github.com/libgit2/libgit2-backends).
Here’s how a custom backend for the object database is set up:

(Note that errors are captured, but not handled. We hope your code is better than ours.)

Initialize an empty object database (ODB) “frontend,” which will act as a container for the “backends” which are the ones doing the real work.

Initialize a custom ODB backend.

Add the backend to the frontend.

Open a repository, and set it to use our ODB to look up objects.

But what is this git_odb_backend_mine thing?
Well, that’s the constructor for your own ODB implementation, and you can do whatever you want in there, so long as you fill in the git_odb_backend structure properly.
Here’s what it could look like:

The subtlest constraint here is that my_backend_struct's first member must be a git_odb_backend structure; this ensures that the memory layout is what the Libgit2 code expects it to be.
The rest of it is arbitrary; this structure can be as large or small as you need it to be.

The initialization function allocates some memory for the structure, sets up the custom context, and then fills in the members of the parent structure that it supports.
Take a look at the include/git2/sys/odb_backend.h file in the Libgit2 source for a complete set of call signatures; your particular use case will help determine which of these you’ll want to support.

Other Bindings

Libgit2 has bindings for many languages.
Here we show a small example using a few of the more complete bindings pakages as of this writing; libraries exist for many other languages, including C++, Go, Node.js, Erlang, and the JVM, all in various stages of maturity.
The official collection of bindings can be found by browsing the repositories at https://github.com/libgit2.
The code we’ll write will return the commit message from the commit eventually pointed to by HEAD (sort of like git log -1).

LibGit2Sharp

If you’re writing a .NET or Mono application, LibGit2Sharp (https://github.com/libgit2/libgit2sharp) is what you’re looking for.
The bindings are written in C#, and great care has been taken to wrap the raw Libgit2 calls with native-feeling CLR APIs.
Here’s what our example program looks like:

new Repository(@"C:\path\to\repo").Head.Tip.Message;

For desktop Windows applications, there’s even a NuGet package that will help you get started quickly.

objective-git

If your application is running on an Apple platform, you’re likely using Objective-C as your implementation language.
Objective-Git (https://github.com/libgit2/objective-git) is the name of the Libgit2 bindings for that environment.
The example program looks like this:

Further Reading

Of course, a full treatment of Libgit2’s capabilities is outside the scope of this book.
If you want more information on Libgit2 itself, there’s API documentation at https://libgit2.github.com/libgit2, and a set of guides at https://libgit2.github.com/docs.
For the other bindings, check the bundled README and tests; there are often small tutorials and pointers to further reading there.