List Git branches by most recent commit date
Sort branches by tip commit date, format local and remote-tracking references, and build a useful stale-branch review list.
Use git branch --sort=-committerdate for a quick list, or git for-each-ref when you want custom columns. The minus sign sorts newest first.
The date belongs to the commit at the branch tip. It is not the branch's creation date, last checkout time, or last push time.
List local and remote-tracking branches
# Local branches, newest tip commit first
git branch --sort=-committerdate
# Local branches with dates
git for-each-ref --sort=-committerdate refs/heads/ \
--format='%(committerdate:short) %(refname:short)'
# Include the locally stored remote-tracking refs
git for-each-ref --sort=-committerdate refs/heads/ refs/remotes/ \
--format='%(committerdate:iso8601) %(refname:short) %(authorname)'
Use only refs/remotes/ if that is the list you want. Remote-tracking refs reflect your clone's last fetch; refresh the relevant remote first if freshness matters. The list can include a symbolic reference such as origin/HEAD, which is an alias rather than an independent remote branch.
Useful format fields
| Field | Meaning |
|---|---|
%(refname:short) |
Short reference name |
%(committerdate:iso8601) |
Tip commit's committer timestamp |
%(authorname) |
Tip commit's author name |
%(subject) |
First line of the tip commit message |
%(upstream:short) |
Configured upstream, if one exists |
Add %(committerdate:unix) when a script needs a numeric timestamp. Date comparisons should use timestamps rather than formatted display strings.
Review older branches before deleting them
To see the 20 oldest local branch tips:
git for-each-ref --sort=committerdate --count=20 refs/heads/ \
--format='%(committerdate:short) %(refname:short)'
This command does not filter for “older than 90 days.” A date cutoff requires a separate comparison against a timestamp.
Compare that list with branches merged into your intended integration branch:
git branch --merged main
Merged status and an old timestamp are review inputs, not a guarantee that a branch is disposable. Check branch purpose, ongoing work, and any release requirements before deleting it.
FAQ
How do author date and committer date differ?
The author date records the author's timestamp; the committer date records the timestamp associated with creating that commit object. Rebase or amend commonly preserves the former and updates the latter. Both can be overridden, so neither is a definitive record of human activity.
Can for-each-ref produce JSON?
It supports formatting and language-specific quoting options such as --python and --shell, but those do not directly create JSON. For structured output, emit well-defined fields, parse them with a script, and serialize with a JSON library. Avoid assuming that a separator is safe for arbitrary author names or subjects; a restricted field set such as ref names and numeric timestamps is easier to handle correctly.
How can I see the branch relationships?
git log --oneline --graph --all --date-order
This displays the commit graph. Use it to understand how branches diverged or merged, and for-each-ref for a sortable inventory of branch tips.
See git-for-each-ref, git-branch, and git-log.
Contact us
Join the community
to join the community
Try Guance
Start online and pay only for what you use.
Get startedChoose a Guance plan