Can I reflog a specific branch?
git reflog
shows all history on the repo. But I want to check the history of one specific branch, say production
. Is there a way to do that?
Can I reflog a specific branch?
git reflog
shows all history on the repo. But I want to check the history of one specific branch, say production
. Is there a way to do that?
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!)
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
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.
Note that git show-branch --no-reflog
is invalid, and Git 2.42 (Q3 2023) now says it explicitely.
See commit 68cbb20, commit 83bb8e5 (19 Jul 2023) by Junio C Hamano (gitster
).
(Merged by Junio C Hamano -- gitster
-- in commit d6966f6, 27 Jul 2023)
show-branch
: reject--[no-](topo|date)-order
"
git show-branch
"(man)--no-topo-order
behaved exactly the same way asgit show-branch --topo-order
(man) did, which was nonsense.
This was because we choose between topo- and date- by setting a variable to eitherREV_SORT_IN_GRAPH_ORDER
orREV_SORT_BY_COMMIT_DATE
withOPT_SET_INT()
andREV_SORT_IN_GRAPH_ORDER
happens to be 0. TheOPT_SET_INT()
macro assigns 0 to the target variable in respose to the negated form of its option."
--no-date-order
" by luck behaves identically to "--topo-order
" exactly for the same reason, and it sort-of makes sense right now, but the "sort-of makes sense" will quickly break down once we add a third way to sort.
Not-A may be B when there are only two choices between A and B, but once your choices become among A, B, and C, not-A does not mean B.Just mark these two ordering options to reject negation, and add a test, which was missing.
"git show-branch --no-reflog
" is also unnegatable, so throw in a test for that while we are at it.
You now will get an "unknown option
" error.