git reflog - branch level summary

147 views Asked by At

git reflog shows how HEAD moved between commits.

Is there a way to show only branches?

(i.e. show only one line per branch visit)

2

There are 2 answers

0
IndustryUser1942 On BEST ANSWER

Close enough (includes also tags):

 git reflog -5 --pretty='%d' | uniq
0
Romain Valeri On

One way to list previous HEAD positions is to use the [HEAD]@{-<n>} construct, where n means nth last position of HEAD, branch-wise. But as eftshift0 already mentioned in comments, this is not set in stone information, since branches are moving. Can be useful in some contexts, though, to understand what happened.

Here, an alias to illustrate my point :

$ git config alias.last '!f() { for i in $(seq 1 $1); do git name-rev --name-only --exclude=refs/tags/\* @{-$i}; done; }; f'

which is just a loop to invoke iteratively git name-rev (doc) on each, like

git name-rev @{-1}
git name-rev @{-2}
git name-rev @{-3}
# ...and so on

where --exclude=refs/tags/\* is filtering out tags,
and --name-only is here to avoid printing the unnecessary hash

$ git last 5
master
dev
feature/abc
master
feature/abc