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:
%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:
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.
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 --.
For a directory:
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.
If the goal is to inspect a remote-tracking branch, fetch first so your local reference is current.
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.
Example:
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.
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=%cIfor 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.

