tollef.web

`ls` by git history

October 27 2025 @ 11:13

for some reason, after 10 years of programming, i never thought it would be neat to list files by their last updated time through git vc, rather than last modified file.

this bash command allows a simple gls or gls N for the most N recent files.

neat!

gls() {
    local limit="${1:-}"
    local output

    output=$((git ls-files; git ls-files --others --exclude-standard) | while read file; do
        if [ -f "$file" ]; then
            timestamp=$(git log -1 --format=%ai -- "$file" 2>/dev/null || stat -f "%Sm" -t "%Y-%m-%d %H:%M:%S" "$file")
            echo "$timestamp $file"
        fi
    done | sort -r)

    if [ -n "$limit" ]; then
        echo "$output" | head -n "$limit"
    else
        echo "$output"
    fi
}