Git reflog a specific branch?
git reflog [show] ref
where ref for example can be a git hash, or anything git can resolve to a hash. Like for instance a branch name:
git reflog production
As noted in the documentation, git reflog
takes an action verb (called <subcommand>
) and optional modifiers. The action defaults to show
, and its optional modifier is the reference name to show.
The default is to show operations on HEAD
. (Most, but not all, "everyday" commands operate on and/or through HEAD
in order to operate on any other reference. Therefore the claim that git reflog
shows all history is in fact false—but it does show most, which might be close enough.) This gives you an immediate and obvious answer to the question of showing operations applied to the specific branch-name production
:
git reflog show production
As the documentation notes, git reflog show
is an alias for git log -g --abbrev-commit --pretty=oneline
, so you can also run:
git log -g --abbrev-commit --pretty=oneline production
to get the exact same output. The key switch here is -g
, which directs git log
to walk the given ref's reflog, rather than commits reachable from the commit to which the ref points.
(You can continue to leave out the show
verb, since it's still the default, though for this case I would advise including it—for instance, if your branch is named show
or expire
the name will be mistaken for the verb!)
But I want to check the history of one specific branch, say
production
.
The more recent (Git 2.9.5, 2017+) command would be git show-branch (-g|--reflog)
git show-branch --reflog production
-g
/--reflog[=<n>[,<base>]] [<ref>]
Shows
<n>
most recent ref-log entries for the given ref.If
<base>
is given,<n>
entries going back from that entry.<base>
can be specified as count or date.When no explicit
<ref>
parameter is given, it defaults to the current branch (or HEAD if it is detached).
Make sure to use Git 2.35 (Q1 2022), as it fixes a bug:
See commit 6527925, commit 3474b60, commit 6887f69, commit 21f0e85, commit f246349 (02 Dec 2021) by Han-Wen Nienhuys (hanwen
).
(Merged by Junio C Hamano -- gitster
-- in commit 250ca49, 15 Dec 2021)
show-branch
: show reflog messageSigned-off-by: Han-Wen Nienhuys
Before,
--reflog
option would look for '\t
' in the reflog message.
Asrefs.c
already parses the reflog line, the '\t' was never found, andshow-branch --reflog
(man) would always say "(none)
" as reflog message
And note: The "--current
" option of "git show-branch
"(man) should have been made incompatible with the --reflog
mode, but this was not enforced, which has been corrected with Git 2.37 (Q3 2022).
See commit 41c64ae (21 Apr 2022) by Junio C Hamano (gitster
).
(Merged by Junio C Hamano -- gitster
-- in commit 18254f1, 25 May 2022)
show-branch
:-g
and--current
are incompatibleReported-by: Gregory David
When "
--current
" is given to "git show-branch
"(man) running in the--reflog
mode, the code tries to reference a "reflog
" message that does not even exist.
This is because the--current
is not prepared to work in that mode.The reason "
--current
" exists is to support this request:I list branches on the command line.
These are the branches I care about and I use as anchoring points.
I may or may not be on one of these main branches.Please make sure I can view the commits on the current branch with respect to what is in these other branches.
And to serve that request, the code checks if the current branch is among the ones listed on the command line, and adds it only if it is not to the end of one array, which essentially lists the objects.
The reflog mode additionally uses another array to list reflog messages, which the "
--current
" code does not add to.
This leaves one uninitialized slot at the end of the array of reflog messages, and causes the program to show garbage or segfault.Catch the unsupported (and meaningless) combination and exit with a usage error.