git for-each-ref --sort=-committerdate
This command prints all refs in the repository (like branches and tags) and arranges them so the ones with the newest commits appear at the top, which is useful for quickly seeing the most recently updated work in your repo.
git for-each-ref iterates over all references in .git/refs and formats them according to a default or custom template, while the --sort=-committerdate flag tells Git to sort those refs by their committer date in descending order (the leading - means “reverse” the normal ascending order). Without a custom --format, this command shows default information about each ref; adding something like --format='%(refname:short) %(committerdate) %(authorname)' makes it print just the short ref name, the commit date, and the author. You can change the sort criteria by replacing committerdate with other fields such as authordate, creatordate, or refname, and you can remove the - (e.g., --sort=committerdate) to sort from oldest to newest instead.
You can also restrict what this command lists by passing a ref prefix, for example git for-each-ref refs/heads to show only local branches or git for-each-ref refs/tags to show only tags, and you can combine multiple --sort options like --sort=-committerdate --sort=refname to break ties by name. This makes it handy for generating custom reports, such as showing active branches, finding stale refs, or scripting cleanup tasks when combined with shell tools like head, tail, grep, and xargs.
Closely related variations include adding a simple format like git for-each-ref --sort=-committerdate --format='%(refname:short)' refs/heads to get a recency-ordered branch list, or using git branch --sort=-committerdate which internally uses similar mechanics but is specialized for branches and has a more user-friendly default output. For tags, a similar command is git for-each-ref --sort=-creatordate --format='%(refname:short) %(creatordate)' refs/tags, and for a higher-level view of recent work on branches, git branch --sort=-committerdate --format='%(refname:short) %(committerdate:relative)' gives a concise summary of how fresh each branch is.
Examples:
git for-each-ref --sort=-committerdate --format='%(refname:short) %(committerdate:relative)' refs/headsgit for-each-ref --sort=-creatordate --format='%(refname:short) %(creatordate)' refs/tags