--- 
canonical: 'https://mwop.net/blog/2026-12-06-identifying-earliest-tag-containing-commit.html'
title: 'Identifying the Earliest Tag Containing A Commit Using the Git CLI'
author: "[Matthew Weier O'Phinney](https://mwop.net)"
created: '2026-12-06T11:56:04-06:00'
updated: '2026-12-06T11:56:04-06:00'
tags:
  - cli
  - git
  - til

---
I'm reviewing some security reports for [Laminas](https:://getlaminas.org), and needed to identify the earliest branch to patch. The GitHub UI has become so incredibly slow, and requires so many different clicks to get at the information that I decided to see if I could do it from the command line. As such, I learned a couple of git commands I'd not used previously.





We pin to explicit PHP minor versions in our `composer.json` files. As such, I needed to determine when the line with the PHP dependency was changed such that it's minimum PHP version matches the current earliest supported PHP version (8.2 at the time of writing).

I identified the line number using grep and assigned it to a variable:

```bash
php_constraint_lineno=$(grep -E -n '"php":\s*".*?\|' composer.json | cut -d':' -f 1)
```

Once I had the line number, I could review the git history. The "-L" flag to `git log` allows you to provide a line range and a file:

```bash
git log -L "${php_constraint_lineno},${php_constraint_lineno}:composer.json"
```

This opens up a pager, and from there I can see the various changes to that line, along with commit hash, author, date, message, and diff.

The final piece was determining the first tag that has that commit:

```bash
git describe --contains <commit>
```

This is so much easier and faster!