Thread (3 messages) 3 messages, 3 authors, 2026-03-06

Re: Feature request: support listing worktrees sorted by creation time

From: Norbert Kiesel <hidden>
Date: 2026-03-05 23:37:09

Wrote a Zsh script that uses the "directory create time" but when a
worktree is moved then that will be updated. So would still like to
have `git worktree add` store the current timestamp somewhere.
#!/bin/zsh
# List git worktrees with creation timestamps, sorted oldest to newest

wt_path="" branch=""
typeset -a ts_arr wt_arr br_arr

flush() {
    [[ -z $wt_path ]] && return
    local epoch=$(stat -f '%B' "$wt_path" 2>/dev/null)
    ts_arr+=("${epoch:-0}")
    wt_arr+=("$wt_path")
    br_arr+=("${branch:-(detached)}")
    wt_path="" branch=""
}

while IFS= read -r line; do
    case $line in
        worktree\ *)  flush; wt_path=${line#worktree } ;;
        branch\ *)    branch=${line#branch refs/heads/} ;;
        detached)     branch="(detached)" ;;
        "")           flush ;;
    esac
done < <(git worktree list --porcelain)
flush

# Find max branch width for column alignment
integer max_br=0
for br in "${br_arr[@]}"; do
    (( ${#br} > max_br )) && max_br=${#br}
done

# Print first entry (main worktree) always first
printf "%s  %-${max_br}s  %s\n" "$(date -r "${ts_arr[1]}"
+"%Y-%m-%d")" "${br_arr[1]}" "${wt_arr[1]}"

# Collect remaining entries prefixed with epoch for numeric sort
typeset -a rest_lines
for (( i=2; i<=${#ts_arr}; i++ )); do
    rest_lines+=("${ts_arr[$i]}  $(printf "%-${max_br}s  %s"
"${br_arr[$i]}" "${wt_arr[$i]}")")
done

# Sort numerically by epoch, then replace epoch with formatted date
(( ${#rest_lines} > 0 )) && print -l "${(on)rest_lines[@]}" | while
IFS= read -r line; do
    epoch=${line%% *}
    printf "%s  %s\n" "$(date -r "$epoch" +"%Y-%m-%d")" "${line#*  }"
done
On Thu, Mar 5, 2026 at 7:50 AM Junio C Hamano [off-list ref] wrote:
Norbert Kiesel [off-list ref] writes:
quoted
I have multiple repos with more than 20 worktrees, and sometimes
forget the name of a recently added worktree. Therefore it would
really be nice if I could use something like ‘git worktree list
—created’ to list them by their creation timestamp. Is that something
that makes sense to you as well? I could also create a pull request
for this if you would like it.
I do not think we have any _record_ of when each of these worktrees
was created, so this is not a realistic request.

The output from "git worktree list" may be more than 20 lines, but
isn't your terminal taller than 20 lines ;-)?

Since very early days of Git, we have created .git/description file
that is not used very much (I think it is shown in gitweb).  Perhaps
worktree should have an equivalent in per-worktree part of their
.git/ directory and "git worktree list --verbose" can use its
contents in addition to the additional pieces information it already
shows, or something like that, perhaps?
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help