How to Use Octokit Commit Method

Flatiron School / 10 November 2012

The following is a guest post by Ericka Ward and originally appeared on her blog. Ericka is currently a student a The Flatiron School. You can learn more about her here, or follow her on twitter here.

Octokit is a Ruby wrapper that parses data from the Github API. This is awesome because it makes the data much more accessible and easier to work with.

Octokit has many methods that you can use to access information. I’m going to show you how to use the commits method.

In order to use Octokit, make sure to first gem install Octokit and run bundle install. Also make sure to require 'octokit'.

The commits method will show you the commits for one repo. The documentation for the commits is below:

Let’s try this! My github username is ejw1. I have a repository called ericka_playlister. If I want to see the commits for my ericka_playlister repository I would do:

This also works:

I personally like the second syntax better because it is more expressive.

The commits method returns an array of nested hashes. Each element in the array is a nested hash that represents a commit object. The nested hash has many keys, including message, date, email, url and more.

So let’s play with this a little. I want to create an array that includes the commit messages for each commit made on github user DavidBakerTv’s mixtapeapp repository. Shout out David! Here’s how I would do that:

The list_commits method will return the array of commit objects that are nested hashes. I use the map method to iterate through that array and create a new modified array with the commit messages. I call the commit method in order to access the commit hash within the commit object. Finally, I call the message method to access the commit messages.

Let’s look at the return:

Whenever using the commit method, it is best to play around in irb or rails console with your array. The array of nested hashes can be tricky.

Another helpful link:

How to Install a Custom Theme on Octopress Previous Post Setting Up a Staging Environment to Use OmniAuth Facebook Next Post