How to list local git branches by order of last visited date

I am looking for a way to list git branches in the order of @{-1}, @{-2}, @{-3} etc references. git branch --sort allows "authordate" and "committerdate" but can't find something like "visiteddate".


Solution 1:

I don't know how to find the "date of the last visit". There is however a way to list branches, in the (reverse) chronological order they were visited :


git finds out @{-1}, @{-2}, @{-3} etc by inspecting the reflog, and keeping lines lookings like checkout: moving from aaa to bbb.

You can grep your way out of the same behavior :

git reflog | grep -o "checkout: moving from .* to " |\
    sed -e 's/checkout: moving from //' -e 's/ to $//' | head -20

comments :

# inspect reflog :
git reflog |\

# keep messages matching 'checkout: ...'
# using -o to keep only this specific portion of the message
grep -o "checkout: moving from .* to " |\

# remove parts which are not a branch (or commit, or anything) name :
sed -e 's/checkout: moving from //' -e 's/ to $//'  |\

# keep only last 20 entries
head -20