d
WE ARE EXPERTS IN TECHNOLOGY

Let’s Work Together

n

StatusNeo

Git Logging 101 – Viewing the Commit History

Modern Software Development starts with collaboration with version control. In the technology landscape, Git has gained immense popularity due to its distributed nature and powerful use of branching. As the project size and the number of collaborators increases in size, keeping track of the Git history and collaborating effectively becomes more complex. Having a good hand in digging the logs of your project history is a must-have skill set for every developer.

The best thing about the advent of open source is you don’t really need to be a part of a complex project with hundreds or thousands of collaborators to start learning. Let’s learn Git Logging by practicing on Spotify’s ultra-hot open-source DevEx framework, “Backstage”

Backstage by Spotify

The enormity of Backstage:

  • 33k ~ commits to master
  • More than 2 years’ worth of history
  • 850+ contributors

The Fundamentals of Git Logging

Git Log is an extremely powerful tool that can be used to view the entire history of the distributed VCS repository you are working with.

Let’s fire the basic command without any arguments first:

$ git log

Git Logging Basics

The command returns the commit history in reverse-chronological order with the following metadata rich in verbosity:

  • Unique commit SHA-1 checksum
  • Commit author name and email
  • Commit message
  • Timestamp of the commit

The output on the CLI is for the entire history which you can scroll down to – press q to return to the terminal.

Limit Git Log to previous N commits

$ git log -n [number]

The log output seems verbose and it is pretty difficult to find relevant information unless you focus on filtering data properly. Let’s start with the aesthetics of the log output

Prettifying the Git Logging Output

Collapse commit history and summarize the log in a single line.

$ git log --pretty=oneline

The log output is heavily polluted with 40-character-long commit SHA – let’s abbreviate it.

$ git log --pretty=oneline --abbrev-commit

You can shorten the previous command as:

$ git log --oneline

Graphically visualizing the Git Logging History

$ git log --graph

Customizing Log Output – The log format can be easily overridden with the pretty flag:

$ git log --pretty=format:'%h - %an, %ad : %s'

The arguments used here signify:

  • %h – shorthand commit SHA
  • %an – Author name
  • %ad – Author commit date
  • %s – Commit Message

Let’s put this pretty version of the logging format in the graphical visualization.

$ git log --pretty=format:'%h: %an checked-in at %ad : %s' --graph

Filtering History by Contributors

View the commit-graph for your project for all contributors

$ git log --pretty=format:'%an' --graph

Filter the log history for a specific user handle

$ git log --author 'NishkarshRaj

$ git log --author 'Nishkarsh Raj' --oneline

Searching History by Pattern matching the Commit Message

The best way to search for particular changes is by pattern matching the Git Commit message (Assuming you have a team that uses meaningful commit messages or you enforce patterns for commit messages!)

$ git log --grep [string 1] --grep [string 2] ... [--all-match]

Note that if the --all-match the flag is passed – the results will be matching all grep statements else it will showcase results even if a single grep statement matches.

Let’s take a look at some examples:

  • Use Case 1: See the list of contributors that were added to the list of Public Adopters of Backstage – for this, we need to view all commits that have the keyword “Adopter” in the commit message.

git log --grep 'Adopter' --pretty=format:'%ae added %s'

  • Use Case 2: See the list of contributors that posted content related to “Spotify”.

git log --grep 'Spotify' --pretty=format:'%ae added %s'

Use case 3: Find the commit message and author who added Spotify as a Public Adopter of Backstage.

git log --grep 'Spotify' --grep 'adopter' --pretty=format:'%an added %s at %ad' --all-match

This is extremely powerful showcase of the Git Logging command - we saw the commit message required (a solo commit in 32k commits done 2 years prior) in less than a second.

Viewing Commit History for a specific file/directory

This is an important use case when we don’t bother about the entire project at hand but only care about changes to a specific file.

$ git log -- [path-to-file]

Example: $ git log –online — ADOPTERS.md

Git Pickaxe Function

The Git Pickaxe fuction is the most powerful command for Git Logging. It helps to identify Git Commits that made changes to a particular function or match file content

In my experience, I have never seen a stronger Git Logging command than the Pickaxe function. I’ve rarely seen anyone documenting it either, consequently, I felt the need to highlight this in the post.

$ git log -S GitlabRepoPicker --pretty=format:'%h by %an (%ae) modified %ar'

Let’s also take a look at the history of the New Relic Dashboard plugin contributed by StatusNeo.

$ git log -S @backstage/plugin-new-relic-dashboard --pretty=format:'%h by %an (%ae) modified %ar'

That’s it for the post! I would like to highlight that this is not an exhaustive list of Git Logging capabilities. Thus, I would recommend going through the Man Page for a deep dive.

I love Git Logging and hope this post will make it interesting and fun for you as well. Nevertheless, grateful for your time. Cheers,

Principal DevOps Evangelist and Consultant at StatusNeo

Add Comment