Skip to main content

Hands-On Projects

Project 2: Building a GitHub First Commit Finder in Go

Learn how to build a Go command-line tool that fetches the first commit of any GitHub repository using the GitHub API, pagination, and regular expressions.

With the GitHub API, we can do a lot of things that are not easy to achieve manually, and this is one of those important tasks we will tackle with a command-line tool.

In this lesson, we will try to do something like this: if you use GitHub, especially in large projects, you will find a lot of commits and open issues.

Sometimes you need to go back and look at the first commit of a specific project, and that task becomes a bit harder as the project grows and more developers contribute to it.

What is a Commit

A commit in GitHub is a recorded snapshot of modifications made to a file or directory. Think of it as the operation where you send some modifications and store them on a remote server like GitHub.

Every commit in GitHub contains some necessary information like the author's name, the date when it was made, a message that explains the change made, and also contains a SHA-1 hash, which is a unique identifier for that specific commit.

Every project starts with a first commit, and it is common for its message to be "first commit" or "initial commit".

That is the message or description that most developers give to the first commit in a project (it is not necessary to name it like that, it is just a widely followed convention).

In large projects hosted on GitHub, where there are a lot of commits and more than one developer contributes and makes changes, the real challenge is to get the first commit, who made it, and when.

That is why the tool that we will build will solve this problem and give us all this information in one click.

Create the Project

In this project, we will use the package that we explained in Lesson 3 to make the output more colorful and beautiful.

So, for this, create a project and call it whatever name of your choice you want, then bootstrap the go.mod file using the command go mod init.

Next, use the command go get github.com/fatih/color to add the package to your project so that you can use it inside that project.

mkdir first-commit
cd first-commit
go mod init first-commit
go get github.com/fatih/color
Creating a Go CLI Tool with Colored Output

Now, create the main file where we will write our code, for example, commit.go or any name of your choice; it doesn't matter.

touch commit.go

Open it in your editor and start with the package declaration and imports at the top, as explained in Lesson 3.

package main

import (
	"fmt"
	"log"
	"os"
	"strings"
	"github.com/fatih/color"
)

func main() {
	// We'll add our code here
}

Working with Repositories in GitHub