git
commit date
git repository
version control
git command

How do I get last commit date from git repository?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Getting the last commit date from Git sounds straightforward, but you need to decide what "last" means first. You might want the newest commit on the current HEAD, the latest change to one file, or the most recent commit on a remote branch. Git can answer all of those questions, but only if you scope the command correctly.

Read the Latest Commit Date on the Current Branch

If you want the newest commit reachable from your current HEAD, the simplest command is:

bash
git log -1 --format=%cI

%cI prints the committer date in strict ISO 8601 format, which is a good default because it is unambiguous and easy to parse in scripts.

If you want the date alongside other useful information:

bash
git log -1 --date=iso-strict --format="%h %cI %s"

That prints the short hash, the commit date, and the subject line in one output line.

Know the Difference Between Author Date and Commit Date

Git stores both an author date and a commit date. They are often the same, but not always.

bash
git log -1 --format="author=%aI commit=%cI"

The author date reflects when the change was originally written. The commit date reflects when the current commit object was created. After a rebase, amend, or cherry-pick, those timestamps can differ.

If you are measuring repository freshness, committer date is usually the right choice. If you are studying when the work was originally authored, author date may be more meaningful.

Get the Last Commit Date for a Specific File or Directory

Often you do not care about the whole repository. You care about one file or one component inside a monorepo. In that case, pass a path after --.

bash
git log -1 --format=%cI -- src/main.py

For a directory:

bash
git log -1 --format="%cI %h" -- docs/

This is much more useful than a repository-wide timestamp when different parts of the codebase move at different speeds.

Query Another Branch or a Remote Reference

Without an explicit ref, git log -1 only inspects your current HEAD. If you need the date from another branch, name it directly.

bash
git log -1 --format=%cI main
git log -1 --format=%cI origin/main

If the goal is to inspect a remote-tracking branch, fetch first so your local reference is current.

bash
git fetch origin
git log -1 --format=%cI origin/main

Without that fetch step, the command may technically work while still returning stale information.

Use Unix Time for Simple Comparisons

If you are comparing ages in shell scripts, epoch seconds are often easier than formatted timestamps.

bash
git log -1 --format=%ct

Example:

bash
last_commit=$(git log -1 --format=%ct)
now=$(date +%s)
echo $((now - last_commit))

This avoids timezone and date-format parsing issues when you only need arithmetic.

Read the Value Programmatically

If your application or automation needs the last commit date inside code, ask Git for a stable format and parse that.

python
1import subprocess
2from datetime import datetime
3
4raw = subprocess.check_output(
5    ["git", "log", "-1", "--format=%cI"],
6    text=True,
7).strip()
8
9last_commit = datetime.fromisoformat(raw.replace("Z", "+00:00"))
10print(last_commit.isoformat())

That is more robust than parsing a human-friendly date format from plain git log.

Match the Command to the Real Question

A lot of confusion comes from using the right command for the wrong scope. In CI, you might need the last commit on origin/main, not the detached HEAD checked out for a job. In a monorepo dashboard, you might need the last change for services/api, not the last change anywhere in the repository.

The command is short, but the meaning depends entirely on the ref or path you query.

Common Pitfalls

The biggest pitfall is assuming git log -1 means "latest commit on the remote default branch." It only means the latest commit reachable from the current HEAD unless you specify another ref.

Another common issue is mixing up author date and commit date. Rebases and amended commits can make those timestamps differ in ways that matter for reporting.

People also parse human-readable dates when %cI or %ct would be far easier to automate safely.

Summary

  • Use git log -1 --format=%cI for a stable last-commit timestamp.
  • Choose author date or commit date intentionally based on what you are measuring.
  • Scope the command to a path when you care about a file or directory instead of the whole repository.
  • Fetch before reading remote-tracking branch dates so the reference is up to date.
  • Prefer ISO 8601 or Unix epoch output for scripts and automation.

Course illustration
Course illustration

All Rights Reserved.