2

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)

IndustryUser1942
  • 1,052
  • 1
  • 8
  • 12
  • 2
    Short answer: not really. The messages say when you switched into a branch and so on, but even if it says a branch, the _current_ commit ID for the branch is likely to have moved from the one in the reflog. – eftshift0 Dec 14 '22 at 14:58

2 Answers2

2

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
Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
0

Close enough (includes also tags):

 git reflog -5 --pretty='%d' | uniq
IndustryUser1942
  • 1,052
  • 1
  • 8
  • 12