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".
Asked
Active
Viewed 1,305 times
5

astgtciv
- 720
- 5
- 15
-
Define "visited". – jub0bs Jan 26 '17 at 11:18
-
2Does this answer your question? [How can I get a list of Git branches that I've recently checked out?](https://stackoverflow.com/questions/25095061/how-can-i-get-a-list-of-git-branches-that-ive-recently-checked-out) – Bergi Sep 24 '20 at 13:49
1 Answers
5
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

LeGEC
- 46,477
- 5
- 57
- 104
-
2excellent, thank you for the direction, below is what I was looking for: `git reflog | sed -n 's/.*checkout: moving from .* to \(.*\)/\1/p' | awk '!x[$0]++'`. Added as git alias! Explanation: As above, sed filters out "moving to [X]" from all the reflog lines. Since some branches will appear more than once in this list, the awk makes the entries unique, but without having to sorting the list as you would have to with `uniq` or `sort -u`. – astgtciv Jan 26 '17 at 18:35
-
Thanks for the `awk` trick ! I was using a much more convoluted way (using `cat -n`, then keeping unique branch names, then sorting back by line number ...) – LeGEC Jan 27 '17 at 09:03
-