{"thread":{"id":"66545","subject":"[PATCH 0/3] reflog: add --operations and --historical-branch","startedAt":"2026-10-15T13:13:55Z","lastAt":"2026-11-07T11:55:36Z","messageCount":51,"participants":["Harald Nordgren via GitGitGadget","Kristoffer Haugsbakk","Harald Nordgren","D. Ben Knoble","Junio C Hamano","Phillip Wood","Ben Knoble"],"isPatch":true,"patchVersion":1,"patchTotal":3},"messages":[{"id":"555140","messageId":"pull.2448.git.git.1792070038.gitgitgadget@gmail.com","threadId":"66545","inReplyTo":null,"subject":"[PATCH 0/3] reflog: add --operations and --historical-branch","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-15T13:13:55Z","receivedAt":"2026-10-15T13:13:55Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"Create a better reflog. Follows the discussion in\nhttps://lore.kernel.org/git/akZfm-igZKeHaDST@pks.im/.\n\n\"git reflog\" lists every step of a rebase and every branch switch, which\nmakes it hard to see what was done and on which branch. This adds two\noptions to \"git reflog\" and \"git log -g\":\n\n * --operations shows one entry per command, folding a rebase into the entry\n   that finished or aborted it, and leaves out entries that change no\n   branch.\n * --historical-branch decorates each entry with the branch HEAD was on when\n   it was recorded.\n\nHarald Nordgren (3):\n  reflog: name the strings that mark switches, renames and rebase steps\n  reflog: add --operations to show one entry per command\n  reflog: add --historical-branch to name the branch of each entry\n\n Documentation/git-reflog.adoc       |   4 +\n Documentation/rev-list-options.adoc |  19 +++\n builtin/branch.c                    |   3 +-\n builtin/checkout.c                  |   3 +-\n builtin/rebase.c                    |  17 +--\n log-tree.c                          |  21 +++\n object-name.c                       |   3 +-\n reflog-walk.c                       | 172 ++++++++++++++++++++++--\n reflog-walk.h                       |   3 +\n reflog.h                            |   6 +\n revision.c                          |  14 ++\n revision.h                          |   2 +\n sequencer.c                         |  13 +-\n t/t1414-reflog-walk.sh              | 198 ++++++++++++++++++++++++++++\n wt-status.c                         |   3 +-\n 15 files changed, 455 insertions(+), 26 deletions(-)\n\n\nbase-commit: 6de20f6092dcf9bdb1c8efe03db4b70c82b423dd\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2448%2FHaraldNordgren%2Freflog-operations-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2448/HaraldNordgren/reflog-operations-v1\nPull-Request: https://github.com/git/git/pull/2448\n-- \ngitgitgadget\n\n"},{"id":"555141","messageId":"d95ed5bd679cea41320c39824b969377e8c88e09.1792070038.git.gitgitgadget@gmail.com","threadId":"66545","inReplyTo":"pull.2448.git.git.1792070038.gitgitgadget@gmail.com","subject":"[PATCH 1/3] reflog: name the strings that mark switches, renames and rebase steps","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-15T13:13:56Z","receivedAt":"2026-10-15T13:13:56Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\n\"git checkout\" records a move of HEAD in the reflog as \"checkout:\nmoving from <old> to <new>\", \"git branch -m\" records a rename of the\ncurrent branch as \"Branch: renamed <old> to <new>\", and \"git rebase\"\nmarks where it starts and ends with steps like \"rebase (start)\" and\n\"rebase (finish)\". \"@{-<n>}\" and \"git status\" read the switch message\nback, each with its own copy of the prefix, and a later commit adds a\nreader for all of them.\n\nName these strings in reflog.h and use them where the messages are\nwritten and read, so they cannot drift apart.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n builtin/branch.c   |  3 ++-\n builtin/checkout.c |  3 ++-\n builtin/rebase.c   | 17 +++++++++--------\n object-name.c      |  3 ++-\n reflog.h           |  6 ++++++\n sequencer.c        | 13 +++++++------\n wt-status.c        |  3 ++-\n 7 files changed, 30 insertions(+), 18 deletions(-)\n\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex a613148fc7..30062ceb7c 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -13,6 +13,7 @@\n #include \"editor.h\"\n #include \"environment.h\"\n #include \"refs.h\"\n+#include \"reflog.h\"\n #include \"commit.h\"\n #include \"gettext.h\"\n #include \"object-name.h\"\n@@ -669,7 +670,7 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int\n \t\tstrbuf_addf(&logmsg, \"Branch: copied %s to %s\",\n \t\t\t    oldref.buf, newref.buf);\n \telse\n-\t\tstrbuf_addf(&logmsg, \"Branch: renamed %s to %s\",\n+\t\tstrbuf_addf(&logmsg, REFLOG_RENAME_PREFIX \"%s to %s\",\n \t\t\t    oldref.buf, newref.buf);\n \n \tif (!copy && !(oldref_usage & IS_ORPHAN) &&\ndiff --git a/builtin/checkout.c b/builtin/checkout.c\nindex c0f0d2c700..9f0f09e50f 100644\n--- a/builtin/checkout.c\n+++ b/builtin/checkout.c\n@@ -25,6 +25,7 @@\n #include \"preload-index.h\"\n #include \"read-cache.h\"\n #include \"refs.h\"\n+#include \"reflog.h\"\n #include \"remote.h\"\n #include \"repo-settings.h\"\n #include \"resolve-undo.h\"\n@@ -999,7 +1000,7 @@ static void update_refs_for_switch(const struct checkout_opts *opts,\n \n \treflog_msg = getenv(\"GIT_REFLOG_ACTION\");\n \tif (!reflog_msg)\n-\t\tstrbuf_addf(&msg, \"checkout: moving from %s to %s\",\n+\t\tstrbuf_addf(&msg, REFLOG_SWITCH_PREFIX \"%s to %s\",\n \t\t\told_desc ? old_desc : \"(invalid)\", new_branch_info->name);\n \telse\n \t\tstrbuf_insertstr(&msg, 0, reflog_msg);\ndiff --git a/builtin/rebase.c b/builtin/rebase.c\nindex 535db60181..26ef589e89 100644\n--- a/builtin/rebase.c\n+++ b/builtin/rebase.c\n@@ -17,6 +17,7 @@\n #include \"strvec.h\"\n #include \"dir.h\"\n #include \"refs.h\"\n+#include \"reflog.h\"\n #include \"config.h\"\n #include \"unpack-trees.h\"\n #include \"lockfile.h\"\n@@ -601,11 +602,11 @@ static int move_to_original_branch(struct rebase_options *opts)\n \tif (!opts->onto)\n \t\tBUG(\"move_to_original_branch without onto\");\n \n-\tstrbuf_addf(&branch_reflog, \"%s (finish): %s onto %s\",\n-\t\t    opts->reflog_action,\n+\tstrbuf_addf(&branch_reflog, \"%s (%s): %s onto %s\",\n+\t\t    opts->reflog_action, REFLOG_STEP_FINISH,\n \t\t    opts->head_name, oid_to_hex(&opts->onto->object.oid));\n-\tstrbuf_addf(&head_reflog, \"%s (finish): returning to %s\",\n-\t\t    opts->reflog_action, opts->head_name);\n+\tstrbuf_addf(&head_reflog, \"%s (%s): returning to %s\",\n+\t\t    opts->reflog_action, REFLOG_STEP_FINISH, opts->head_name);\n \tropts.branch = opts->head_name;\n \tropts.flags = RESET_WORKING_TREE_REFS_ONLY |\n \t\t      RESET_WORKING_TREE_UPDATE_HEAD;\n@@ -1413,8 +1414,8 @@ int cmd_rebase(int argc,\n \t\tif (read_basic_state(&options))\n \t\t\texit(1);\n \n-\t\tstrbuf_addf(&head_msg, \"%s (abort): returning to %s\",\n-\t\t\t    options.reflog_action,\n+\t\tstrbuf_addf(&head_msg, \"%s (%s): returning to %s\",\n+\t\t\t    options.reflog_action, REFLOG_STEP_ABORT,\n \t\t\t    options.head_name ? options.head_name\n \t\t\t\t\t      : oid_to_hex(&options.orig_head->object.oid));\n \t\tropts.oid = &options.orig_head->object.oid;\n@@ -1884,8 +1885,8 @@ int cmd_rebase(int argc,\n \t\tprintf(_(\"First, rewinding head to replay your work on top of \"\n \t\t\t \"it...\\n\"));\n \n-\tstrbuf_addf(&msg, \"%s (start): checkout %s\",\n-\t\t    options.reflog_action, options.onto_name);\n+\tstrbuf_addf(&msg, \"%s (%s): checkout %s\", options.reflog_action,\n+\t\t    REFLOG_STEP_START, options.onto_name);\n \tropts.oid = &options.onto->object.oid;\n \tropts.orig_head = &options.orig_head->object.oid;\n \tropts.flags = RESET_WORKING_TREE_DETACH |\ndiff --git a/object-name.c b/object-name.c\nindex 4eda8c8eac..bb349014ee 100644\n--- a/object-name.c\n+++ b/object-name.c\n@@ -13,6 +13,7 @@\n #include \"tree.h\"\n #include \"tree-walk.h\"\n #include \"refs.h\"\n+#include \"reflog.h\"\n #include \"remote.h\"\n #include \"dir.h\"\n #include \"odb.h\"\n@@ -1251,7 +1252,7 @@ static int grab_nth_branch_switch(const char *refname UNUSED,\n \tconst char *match = NULL, *target = NULL;\n \tsize_t len;\n \n-\tif (skip_prefix(message, \"checkout: moving from \", &match))\n+\tif (skip_prefix(message, REFLOG_SWITCH_PREFIX, &match))\n \t\ttarget = strstr(match, \" to \");\n \n \tif (!match || !target)\ndiff --git a/reflog.h b/reflog.h\nindex 3bdd1ca8a5..32c3737db4 100644\n--- a/reflog.h\n+++ b/reflog.h\n@@ -2,6 +2,12 @@\n #define REFLOG_H\n #include \"refs.h\"\n \n+#define REFLOG_SWITCH_PREFIX \"checkout: moving from \"\n+#define REFLOG_RENAME_PREFIX \"Branch: renamed \"\n+#define REFLOG_STEP_START \"start\"\n+#define REFLOG_STEP_FINISH \"finish\"\n+#define REFLOG_STEP_ABORT \"abort\"\n+\n #define REFLOG_EXPIRE_TOTAL   (1 << 0)\n #define REFLOG_EXPIRE_UNREACH (1 << 1)\n \ndiff --git a/sequencer.c b/sequencer.c\nindex efbfd1315b..c55df2d38f 100644\n--- a/sequencer.c\n+++ b/sequencer.c\n@@ -31,6 +31,7 @@\n #include \"merge-ort.h\"\n #include \"merge-ort-wrappers.h\"\n #include \"refs.h\"\n+#include \"reflog.h\"\n #include \"sparse-index.h\"\n #include \"strvec.h\"\n #include \"quote.h\"\n@@ -5011,8 +5012,8 @@ static int checkout_onto(struct repository *r, struct replay_opts *opts,\n \t\t\t RESET_WORKING_TREE_UPDATE_HEAD |\n \t\t\t RESET_WORKING_TREE_UPDATE_ORIG_HEAD |\n \t\t\t RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK,\n-\t\t.head_msg = reflog_message(opts, \"start\", \"checkout %s\",\n-\t\t\t\t\t   onto_name),\n+\t\t.head_msg = reflog_message(opts, REFLOG_STEP_START,\n+\t\t\t\t\t   \"checkout %s\", onto_name),\n \t\t.default_reflog_action = sequencer_reflog_action(opts)\n \t};\n \tif (reset_working_tree(r, &ropts)) {\n@@ -5310,16 +5311,16 @@ cleanup_head_ref:\n \t\t\t\tres = error(_(\"could not read 'onto'\"));\n \t\t\t\tgoto cleanup_head_ref;\n \t\t\t}\n-\t\t\tmsg = reflog_message(opts, \"finish\", \"%s onto %s\",\n-\t\t\t\thead_ref.buf, buf.buf);\n+\t\t\tmsg = reflog_message(opts, REFLOG_STEP_FINISH,\n+\t\t\t\t\"%s onto %s\", head_ref.buf, buf.buf);\n \t\t\tif (refs_update_ref(get_main_ref_store(the_repository), msg, head_ref.buf, &head, &orig,\n \t\t\t\t\t    REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) {\n \t\t\t\tres = error(_(\"could not update %s\"),\n \t\t\t\t\thead_ref.buf);\n \t\t\t\tgoto cleanup_head_ref;\n \t\t\t}\n-\t\t\tmsg = reflog_message(opts, \"finish\", \"returning to %s\",\n-\t\t\t\thead_ref.buf);\n+\t\t\tmsg = reflog_message(opts, REFLOG_STEP_FINISH,\n+\t\t\t\t\"returning to %s\", head_ref.buf);\n \t\t\tif (refs_update_symref(get_main_ref_store(the_repository), \"HEAD\", head_ref.buf, msg)) {\n \t\t\t\tres = error(_(\"could not update HEAD to %s\"),\n \t\t\t\t\thead_ref.buf);\ndiff --git a/wt-status.c b/wt-status.c\nindex 57772c7501..ffd54353a8 100644\n--- a/wt-status.c\n+++ b/wt-status.c\n@@ -22,6 +22,7 @@\n #include \"strvec.h\"\n #include \"remote.h\"\n #include \"refs.h\"\n+#include \"reflog.h\"\n #include \"submodule.h\"\n #include \"column.h\"\n #include \"read-cache.h\"\n@@ -1790,7 +1791,7 @@ static int grab_1st_switch(const char *refname UNUSED,\n \tstruct grab_1st_switch_cbdata *cb = cb_data;\n \tconst char *target = NULL, *end;\n \n-\tif (!skip_prefix(message, \"checkout: moving from \", &message))\n+\tif (!skip_prefix(message, REFLOG_SWITCH_PREFIX, &message))\n \t\treturn 0;\n \ttarget = strstr(message, \" to \");\n \tif (!target)\n-- \ngitgitgadget\n\n\n"},{"id":"555142","messageId":"579e13b36a08d710ed083d9c38a7926145dcdf42.1792070038.git.gitgitgadget@gmail.com","threadId":"66545","inReplyTo":"pull.2448.git.git.1792070038.gitgitgadget@gmail.com","subject":"[PATCH 2/3] reflog: add --operations to show one entry per command","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-15T13:13:57Z","receivedAt":"2026-10-15T13:13:57Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nAfter a rebase, \"git reflog\" shows one entry for every commit the\nrebase replayed, plus one for its start and one for its end. Together\nwith an entry for every branch switch, finding where a branch was\nbefore the rebase means reading past all of them.\n\nTeach \"git reflog\" and \"git log -g\" an \"--operations\" option that\nshows each rebase as the single entry that finished or aborted it, and\nleaves out entries that change no branch, like branch switches and the\n\"reset: moving to HEAD\" that \"git stash\" and \"git merge --abort\" leave\nbehind:\n\n  $ git reflog --operations\n  4e5f6a7 HEAD@{0}: rebase (finish): returning to refs/heads/topic\n  8c9d0e1 HEAD@{5}: commit: fix typo\n  1a2b3c4 HEAD@{7}: commit: add tests\n\nThe HEAD@{<n>} numbers keep their usual values, so the previous entry\non the same branch can be used to undo the rebase:\n\n  $ git reset --hard HEAD@{7}\n\nCommands run while a rebase is stopped, such as an amend after an\n\"edit\", are folded into that rebase, and a \"git pull --rebase\" is shown\nas one entry the same way.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-reflog.adoc       |   3 +\n Documentation/rev-list-options.adoc |  11 +++\n reflog-walk.c                       |  61 +++++++++++++++-\n reflog-walk.h                       |   1 +\n revision.c                          |   8 +++\n revision.h                          |   1 +\n t/t1414-reflog-walk.sh              | 105 ++++++++++++++++++++++++++++\n 7 files changed, 188 insertions(+), 2 deletions(-)\n\ndiff --git a/Documentation/git-reflog.adoc b/Documentation/git-reflog.adoc\nindex 38af0c977a..672b9ea72e 100644\n--- a/Documentation/git-reflog.adoc\n+++ b/Documentation/git-reflog.adoc\n@@ -41,6 +41,9 @@ command-line (or `HEAD`, by default). The reflog covers all recent\n actions, and in addition the `HEAD` reflog records branch switching.\n `git reflog show` is an alias for `git log -g --abbrev-commit\n --pretty=oneline`; see linkgit:git-log[1] for more information.\n+With `--operations`, branch switches and other entries that change no\n+branch are left out, and a rebase is shown as a single entry instead\n+of one entry for each commit it replayed.\n \n The \"list\" subcommand lists all refs which have a corresponding reflog.\n \ndiff --git a/Documentation/rev-list-options.adoc b/Documentation/rev-list-options.adoc\nindex bd9f345690..a40ba2641b 100644\n--- a/Documentation/rev-list-options.adoc\n+++ b/Documentation/rev-list-options.adoc\n@@ -348,6 +348,17 @@ See also linkgit:git-reflog[1].\n +\n Under `--pretty=reference`, this information will not be shown at all.\n \n+`--operations`::\n+\tShow one reflog entry for each command that changed a branch.\n+\tA rebase is shown as only the entry that finished or aborted it,\n+\tincluding any commands run while it was stopped, such as\n+\t`git commit --amend`. Switching branches is not shown, and\n+\tneither is a reset that left `HEAD` where it was, such as the one\n+\t`git stash` does.\n+\tThe previous entry on the same branch is the state before the\n+\trebase started. It is an error to use this option unless\n+\t`--walk-reflogs` is in use.\n+\n `--merge`::\n \tShow commits touching conflicted paths in the range `HEAD...<other>`,\n \twhere `<other>` is the first existing pseudoref in `MERGE_HEAD`,\ndiff --git a/reflog-walk.c b/reflog-walk.c\nindex 4dbeaa93a7..82528f5b66 100644\n--- a/reflog-walk.c\n+++ b/reflog-walk.c\n@@ -3,6 +3,7 @@\n #include \"git-compat-util.h\"\n #include \"commit.h\"\n #include \"refs.h\"\n+#include \"reflog.h\"\n #include \"diff.h\"\n #include \"repository.h\"\n #include \"revision.h\"\n@@ -18,6 +19,7 @@ struct complete_reflogs {\n \t\ttimestamp_t timestamp;\n \t\tint tz;\n \t\tchar *message;\n+\t\tbool user_operation;\n \t} *items;\n \tint nr, alloc;\n };\n@@ -38,6 +40,7 @@ static int read_one_reflog(const char *refname UNUSED,\n \titem->timestamp = timestamp;\n \titem->tz = tz;\n \titem->message = xstrdup(message);\n+\titem->user_operation = true;\n \tarray->nr++;\n \treturn 0;\n }\n@@ -65,6 +68,46 @@ static void complete_reflogs_clear(void *util, const char *str UNUSED)\n \tfree_complete_reflog(array);\n }\n \n+static bool has_rebase_step(const char *message, const char *step)\n+{\n+\tsize_t end = strcspn(message, \":\\n\");\n+\tsize_t len = strlen(step);\n+\n+\treturn end >= len + 3 && message[end - 1] == ')' &&\n+\t       !strncmp(message + end - len - 3, \" (\", 2) &&\n+\t       !strncmp(message + end - len - 1, step, len);\n+}\n+\n+static void mark_operations(struct complete_reflogs *array)\n+{\n+\tint start = -1;\n+\n+\tfor (int i = 0; i < array->nr; i++) {\n+\t\tstruct reflog_info *item = &array->items[i];\n+\t\tconst char *msg = item->message;\n+\n+\t\tif (has_rebase_step(msg, REFLOG_STEP_FINISH) ||\n+\t\t    has_rebase_step(msg, REFLOG_STEP_ABORT)) {\n+\t\t\tif (start >= 0)\n+\t\t\t\tfor (int j = start; j < i; j++)\n+\t\t\t\t\tarray->items[j].user_operation = false;\n+\t\t\tstart = -1;\n+\t\t\titem->user_operation = true;\n+\t\t\tcontinue;\n+\t\t}\n+\t\tif (has_rebase_step(msg, REFLOG_STEP_START))\n+\t\t\tstart = i;\n+\n+\t\tif (starts_with(msg, REFLOG_SWITCH_PREFIX))\n+\t\t\titem->user_operation = false;\n+\t\telse if (starts_with(msg, REFLOG_RENAME_PREFIX))\n+\t\t\titem->user_operation = true;\n+\t\telse\n+\t\t\titem->user_operation =\n+\t\t\t\t!oideq(&item->ooid, &item->noid);\n+\t}\n+}\n+\n static struct complete_reflogs *read_complete_reflog(const char *ref)\n {\n \tstruct complete_reflogs *reflogs =\n@@ -135,6 +178,16 @@ void init_reflog_walk(struct reflog_walk_info **info)\n \t(*info)->complete_reflogs.strdup_strings = 1;\n }\n \n+void reflog_walk_mark(struct reflog_walk_info *info, bool operations)\n+{\n+\tstruct string_list_item *item;\n+\n+\tfor_each_string_list_item(item, &info->complete_reflogs) {\n+\t\tif (operations)\n+\t\t\tmark_operations(item->util);\n+\t}\n+}\n+\n void reflog_walk_info_release(struct reflog_walk_info *info)\n {\n \tsize_t i;\n@@ -342,8 +395,12 @@ static struct commit *next_reflog_commit(struct commit_reflog *log)\n {\n \tfor (; log->recno >= 0; log->recno--) {\n \t\tstruct reflog_info *entry = &log->reflogs->items[log->recno];\n-\t\tstruct object *obj = parse_object(the_repository,\n-\t\t\t\t\t\t  &entry->noid);\n+\t\tstruct object *obj;\n+\n+\t\tif (!entry->user_operation)\n+\t\t\tcontinue;\n+\n+\t\tobj = parse_object(the_repository, &entry->noid);\n \n \t\tif (obj && obj->type == OBJ_COMMIT)\n \t\t\treturn (struct commit *)obj;\ndiff --git a/reflog-walk.h b/reflog-walk.h\nindex 989583dc55..1d86795f84 100644\n--- a/reflog-walk.h\n+++ b/reflog-walk.h\n@@ -7,6 +7,7 @@ struct date_mode;\n \n void init_reflog_walk(struct reflog_walk_info **info);\n void reflog_walk_info_release(struct reflog_walk_info *info);\n+void reflog_walk_mark(struct reflog_walk_info *info, bool operations);\n int add_reflog_for_walk(struct reflog_walk_info *info,\n \t\t\tstruct commit *commit, const char *name);\n void show_reflog_message(struct reflog_walk_info *info, int,\ndiff --git a/revision.c b/revision.c\nindex a83e499047..88785651fe 100644\n--- a/revision.c\n+++ b/revision.c\n@@ -2458,6 +2458,8 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg\n \t\tcommit_list_insert(c, &revs->ancestry_path_bottoms);\n \t} else if (!strcmp(arg, \"-g\") || !strcmp(arg, \"--walk-reflogs\")) {\n \t\tinit_reflog_walk(&revs->reflog_info);\n+\t} else if (!strcmp(arg, \"--operations\")) {\n+\t\trevs->reflog_operations = 1;\n \t} else if (!strcmp(arg, \"--default\")) {\n \t\tif (argc <= 1)\n \t\t\treturn error(\"bad --default argument\");\n@@ -3247,6 +3249,12 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s\n \tif (!revs->reflog_info && revs->grep_filter.use_reflog_filter)\n \t\tdie(_(\"the option '%s' requires '%s'\"), \"--grep-reflog\", \"--walk-reflogs\");\n \n+\tif (!revs->reflog_info && revs->reflog_operations)\n+\t\tdie(_(\"the option '%s' requires '%s'\"), \"--operations\", \"--walk-reflogs\");\n+\n+\tif (revs->reflog_info)\n+\t\treflog_walk_mark(revs->reflog_info, revs->reflog_operations);\n+\n \tif (revs->line_level_traverse &&\n \t    (revs->full_diff ||\n \t     (revs->diffopt.output_format &\ndiff --git a/revision.h b/revision.h\nindex e5dabd18ce..c4f5e06698 100644\n--- a/revision.h\n+++ b/revision.h\n@@ -221,6 +221,7 @@ struct rev_info {\n \t\t\texclude_first_parent_only:1,\n \t\t\tline_level_traverse:1,\n \t\t\ttree_blobs_in_commit_order:1,\n+\t\t\treflog_operations:1,\n \n \t\t\t/*\n \t\t\t * Blobs are shown without regard for their existence.\ndiff --git a/t/t1414-reflog-walk.sh b/t/t1414-reflog-walk.sh\nindex be6c3f472c..e15f6f2381 100755\n--- a/t/t1414-reflog-walk.sh\n+++ b/t/t1414-reflog-walk.sh\n@@ -5,6 +5,7 @@ GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main\n export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME\n \n . ./test-lib.sh\n+. \"$TEST_DIRECTORY\"/lib-rebase.sh\n \n test_expect_success 'set up some reflog entries' '\n \ttest_commit one &&\n@@ -131,6 +132,110 @@ test_expect_success 'walk prefers reflog to ref tip' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success '--operations shows a rebase as one entry' '\n+\tgit init operations &&\n+\t(\n+\t\tcd operations &&\n+\t\ttest_commit base &&\n+\t\tgit switch -c topic &&\n+\t\ttest_commit A &&\n+\t\ttest_commit B &&\n+\t\ttest_commit C &&\n+\t\tgit switch main &&\n+\t\ttest_commit upstream &&\n+\t\tgit switch topic &&\n+\t\tgit rebase main &&\n+\n+\t\tcat >expect <<-\\EOF &&\n+\t\tHEAD@{0} rebase (finish): returning to refs/heads/topic\n+\t\tHEAD@{6} commit: upstream\n+\t\tHEAD@{8} commit: C\n+\t\tHEAD@{9} commit: B\n+\t\tHEAD@{10} commit: A\n+\t\tHEAD@{12} commit (initial): base\n+\t\tEOF\n+\t\tgit log -g --format=\"%gd %gs\" HEAD --operations >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '--operations shows a stopped rebase as one entry once it finishes' '\n+\tset_fake_editor &&\n+\t(\n+\t\tcd operations &&\n+\t\tFAKE_LINES=\"edit 1 2\" git rebase -i HEAD~2 &&\n+\n+\t\tcat >expect <<-\\EOF &&\n+\t\tHEAD@{0} rebase: fast-forward\n+\t\tHEAD@{1} rebase (start): checkout HEAD~2\n+\t\tEOF\n+\t\tgit log -g --operations --format=\"%gd %gs\" -2 >actual &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tgit commit --amend -m \"B amended\" &&\n+\t\tgit rebase --continue &&\n+\n+\t\tcat >expect <<-\\EOF &&\n+\t\tHEAD@{0} rebase (finish): returning to refs/heads/topic\n+\t\tHEAD@{5} rebase (finish): returning to refs/heads/topic\n+\t\tEOF\n+\t\tgit log -g --operations --format=\"%gd %gs\" -2 >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '--operations shows an aborted rebase as one entry' '\n+\t(\n+\t\tcd operations &&\n+\t\tFAKE_LINES=\"edit 1 2\" git rebase -i HEAD~2 &&\n+\t\tgit rebase --abort &&\n+\n+\t\tcat >expect <<-\\EOF &&\n+\t\tHEAD@{0} rebase (abort): returning to refs/heads/topic\n+\t\tHEAD@{3} rebase (finish): returning to refs/heads/topic\n+\t\tEOF\n+\t\tgit log -g --operations --format=\"%gd %gs\" -2 >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '--operations leaves out the reset done by git stash' '\n+\t(\n+\t\tcd operations &&\n+\t\techo change >>C.t &&\n+\t\tgit stash &&\n+\n+\t\techo \"HEAD@{1} rebase (abort): returning to refs/heads/topic\" >expect &&\n+\t\tgit log -g --operations --format=\"%gd %gs\" -1 >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '--operations shows a pull --rebase as one entry' '\n+\tgit clone operations downstream &&\n+\t(\n+\t\tcd operations &&\n+\t\ttest_commit upstream-change\n+\t) &&\n+\t(\n+\t\tcd downstream &&\n+\t\ttest_commit local-change &&\n+\t\tgit pull --rebase &&\n+\n+\t\tcat >expect <<-\\EOF &&\n+\t\tHEAD@{0} pull --rebase (finish): returning to refs/heads/topic\n+\t\tHEAD@{3} commit: local-change\n+\t\tEOF\n+\t\tgit log -g --operations --format=\"%gd %gs\" -2 >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '--operations requires --walk-reflogs' '\n+\ttest_must_fail git log --operations 2>err &&\n+\ttest_grep \"requires .--walk-reflogs.\" err\n+'\n+\n test_expect_success 'rev-list -g complains when there are no reflogs' '\n \ttest_must_fail git rev-list -g\n '\n-- \ngitgitgadget\n\n\n"},{"id":"555143","messageId":"4efe74b04e3c69b78caa4acd24c95a8cb64d0c57.1792070038.git.gitgitgadget@gmail.com","threadId":"66545","inReplyTo":"pull.2448.git.git.1792070038.gitgitgadget@gmail.com","subject":"[PATCH 3/3] reflog: add --historical-branch to name the branch of each entry","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-15T13:13:58Z","receivedAt":"2026-10-15T13:13:58Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\n\"git reflog\" does not say which branch HEAD was on when an entry was\nrecorded. After switching between a few branches, telling which\ncommits and rebases belong to which branch means reading back to the\nlast \"checkout: moving from\" entry for each of them. Commit\ndecorations do not help either, since they only name branches that\nstill point at that commit, and a rebased or amended commit has none.\n\nTeach \"git reflog\" and \"git log -g\" a \"--historical-branch\" option\nthat decorates each entry with the branch it was made on, in place of\nthe refs that point at the commit now. The name is colored like a\nbranch decoration:\n\n  $ git reflog --historical-branch\n  4e5f6a7 (topic) HEAD@{0}: commit: add tests\n  1a2b3c4 (topic) HEAD@{1}: checkout: moving from main to topic\n  8c9d0e1 (main) HEAD@{2}: commit: fix typo\n\nEvery entry gets a name, including ones whose commits are no longer on\nany branch. Entries from before a branch was renamed show its old name,\nand entries made on a detached HEAD show \"detached at <commit>\".\nCombined with \"--operations\", which leaves out the branch switches\nthemselves, it lists what was done to each branch.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-reflog.adoc       |   3 +-\n Documentation/rev-list-options.adoc |   8 ++\n log-tree.c                          |  21 ++++++\n reflog-walk.c                       | 113 ++++++++++++++++++++++++++--\n reflog-walk.h                       |   4 +-\n revision.c                          |   8 +-\n revision.h                          |   1 +\n t/t1414-reflog-walk.sh              |  95 ++++++++++++++++++++++-\n 8 files changed, 242 insertions(+), 11 deletions(-)\n\ndiff --git a/Documentation/git-reflog.adoc b/Documentation/git-reflog.adoc\nindex 672b9ea72e..cd988f438b 100644\n--- a/Documentation/git-reflog.adoc\n+++ b/Documentation/git-reflog.adoc\n@@ -43,7 +43,8 @@ actions, and in addition the `HEAD` reflog records branch switching.\n --pretty=oneline`; see linkgit:git-log[1] for more information.\n With `--operations`, branch switches and other entries that change no\n branch are left out, and a rebase is shown as a single entry instead\n-of one entry for each commit it replayed.\n+of one entry for each commit it replayed. With `--historical-branch`,\n+each entry is decorated with the branch it was made on.\n \n The \"list\" subcommand lists all refs which have a corresponding reflog.\n \ndiff --git a/Documentation/rev-list-options.adoc b/Documentation/rev-list-options.adoc\nindex a40ba2641b..d2a0a9b3bc 100644\n--- a/Documentation/rev-list-options.adoc\n+++ b/Documentation/rev-list-options.adoc\n@@ -359,6 +359,14 @@ Under `--pretty=reference`, this information will not be shown at all.\n \trebase started. It is an error to use this option unless\n \t`--walk-reflogs` is in use.\n \n+`--historical-branch`::\n+\tDecorate each reflog entry with the branch `HEAD` was on when\n+\tthe entry was recorded, such as `(topic)`, in place of the refs\n+\tthat point at the commit now. When `HEAD` was detached, the\n+\tentry shows `(detached at <commit>)`. For the reflog of any\n+\tother ref, the name of that ref is shown. It is an error to use\n+\tthis option unless `--walk-reflogs` is in use.\n+\n `--merge`::\n \tShow commits touching conflicted paths in the range `HEAD...<other>`,\n \twhere `<other>` is the first existing pseudoref in `MERGE_HEAD`,\ndiff --git a/log-tree.c b/log-tree.c\nindex 83a3c4bf9b..d457f0451a 100644\n--- a/log-tree.c\n+++ b/log-tree.c\n@@ -421,6 +421,23 @@ void format_decorations(struct strbuf *sb,\n \t}\n }\n \n+static void show_historical_branch(struct rev_info *opt)\n+{\n+\tconst char *branch = get_reflog_branch(opt->reflog_info);\n+\tenum git_colorbool use_color = opt->diffopt.use_color;\n+\tconst char *color_commit, *color_reset;\n+\n+\tif (!branch)\n+\t\treturn;\n+\n+\tcolor_commit = diff_get_color(use_color, DIFF_COMMIT);\n+\tcolor_reset = decorate_get_color(use_color, DECORATION_NONE);\n+\tfprintf(opt->diffopt.file, \"%s (%s%s%s%s%s)%s\",\n+\t\tcolor_commit, color_reset,\n+\t\tdecorate_get_color(use_color, DECORATION_REF_LOCAL),\n+\t\tbranch, color_reset, color_commit, color_reset);\n+}\n+\n void show_decorations(struct rev_info *opt, struct commit *commit)\n {\n \tstruct strbuf sb = STRBUF_INIT;\n@@ -431,6 +448,10 @@ void show_decorations(struct rev_info *opt, struct commit *commit)\n \t\tif (slot && *slot)\n \t\t\tfprintf(opt->diffopt.file, \"\\t%s\", *slot);\n \t}\n+\tif (opt->historical_branch) {\n+\t\tshow_historical_branch(opt);\n+\t\treturn;\n+\t}\n \tif (!opt->show_decorations)\n \t\treturn;\n \tformat_decorations(&sb, commit, opt->diffopt.use_color, NULL);\ndiff --git a/reflog-walk.c b/reflog-walk.c\nindex 82528f5b66..cc42fcd7d8 100644\n--- a/reflog-walk.c\n+++ b/reflog-walk.c\n@@ -5,6 +5,8 @@\n #include \"refs.h\"\n #include \"reflog.h\"\n #include \"diff.h\"\n+#include \"hex.h\"\n+#include \"object-name.h\"\n #include \"repository.h\"\n #include \"revision.h\"\n #include \"string-list.h\"\n@@ -19,6 +21,7 @@ struct complete_reflogs {\n \t\ttimestamp_t timestamp;\n \t\tint tz;\n \t\tchar *message;\n+\t\tchar *branch;\n \t\tbool user_operation;\n \t} *items;\n \tint nr, alloc;\n@@ -40,6 +43,7 @@ static int read_one_reflog(const char *refname UNUSED,\n \titem->timestamp = timestamp;\n \titem->tz = tz;\n \titem->message = xstrdup(message);\n+\titem->branch = NULL;\n \titem->user_operation = true;\n \tarray->nr++;\n \treturn 0;\n@@ -55,6 +59,7 @@ static void free_complete_reflog(struct complete_reflogs *array)\n \tfor (i = 0; i < array->nr; i++) {\n \t\tfree(array->items[i].email);\n \t\tfree(array->items[i].message);\n+\t\tfree(array->items[i].branch);\n \t}\n \tfree(array->items);\n \tfree(array->ref);\n@@ -108,6 +113,80 @@ static void mark_operations(struct complete_reflogs *array)\n \t}\n }\n \n+static char *branch_label(const char *name, size_t len)\n+{\n+\tstruct object_id oid;\n+\tconst char *short_name;\n+\n+\tif (len == the_hash_algo->hexsz && !get_oid_hex(name, &oid))\n+\t\treturn xstrfmt(\"detached at %s\",\n+\t\t\t       repo_find_unique_abbrev(the_repository, &oid,\n+\t\t\t\t\t\t       DEFAULT_ABBREV));\n+\tif (skip_prefix(name, \"refs/heads/\", &short_name))\n+\t\tlen -= short_name - name;\n+\telse\n+\t\tshort_name = name;\n+\treturn xmemdupz(short_name, len);\n+}\n+\n+struct branch_move {\n+\tconst char *from, *to;\n+\tsize_t from_len, to_len;\n+};\n+\n+static bool parse_branch_move(const char *message, struct branch_move *move)\n+{\n+\tconst char *to;\n+\n+\tif (!skip_prefix(message, REFLOG_SWITCH_PREFIX, &move->from) &&\n+\t    !skip_prefix(message, REFLOG_RENAME_PREFIX, &move->from))\n+\t\treturn false;\n+\tto = strstr(move->from, \" to \");\n+\tif (!to)\n+\t\treturn false;\n+\tmove->from_len = to - move->from;\n+\tmove->to = to + strlen(\" to \");\n+\tmove->to_len = strcspn(move->to, \"\\n\");\n+\treturn true;\n+}\n+\n+static void mark_branches(struct complete_reflogs *array)\n+{\n+\tchar *branch = NULL;\n+\n+\tif (strcmp(array->ref, \"HEAD\"))\n+\t\treturn;\n+\n+\tfor (int i = array->nr - 1; i >= 0; i--) {\n+\t\tstruct reflog_info *item = &array->items[i];\n+\t\tstruct branch_move move;\n+\n+\t\tif (!parse_branch_move(item->message, &move)) {\n+\t\t\titem->branch = xstrdup_or_null(branch);\n+\t\t\tcontinue;\n+\t\t}\n+\n+\t\tif (!branch)\n+\t\t\tfor (int j = i + 1; j < array->nr; j++)\n+\t\t\t\tarray->items[j].branch =\n+\t\t\t\t\tbranch_label(move.to, move.to_len);\n+\t\titem->branch = branch_label(move.to, move.to_len);\n+\t\tfree(branch);\n+\t\tbranch = branch_label(move.from, move.from_len);\n+\t}\n+\n+\tif (!branch) {\n+\t\tstruct ref_store *refs = get_main_ref_store(the_repository);\n+\t\tconst char *head = refs_resolve_ref_unsafe(refs, \"HEAD\", 0,\n+\t\t\t\t\t\t\t   NULL, NULL);\n+\n+\t\tif (head && skip_prefix(head, \"refs/heads/\", &head))\n+\t\t\tfor (int i = 0; i < array->nr; i++)\n+\t\t\t\tarray->items[i].branch = xstrdup(head);\n+\t}\n+\tfree(branch);\n+}\n+\n static struct complete_reflogs *read_complete_reflog(const char *ref)\n {\n \tstruct complete_reflogs *reflogs =\n@@ -178,13 +257,16 @@ void init_reflog_walk(struct reflog_walk_info **info)\n \t(*info)->complete_reflogs.strdup_strings = 1;\n }\n \n-void reflog_walk_mark(struct reflog_walk_info *info, bool operations)\n+void reflog_walk_mark(struct reflog_walk_info *info, bool operations,\n+\t\t      bool historical_branch)\n {\n \tstruct string_list_item *item;\n \n \tfor_each_string_list_item(item, &info->complete_reflogs) {\n \t\tif (operations)\n \t\t\tmark_operations(item->util);\n+\t\tif (historical_branch)\n+\t\t\tmark_branches(item->util);\n \t}\n }\n \n@@ -287,6 +369,16 @@ int add_reflog_for_walk(struct reflog_walk_info *info,\n \treturn 0;\n }\n \n+static const char *short_ref(struct complete_reflogs *reflogs)\n+{\n+\tstruct ref_store *refs = get_main_ref_store(the_repository);\n+\n+\tif (!reflogs->short_ref)\n+\t\treflogs->short_ref =\n+\t\t\trefs_shorten_unambiguous_ref(refs, reflogs->ref, 0);\n+\treturn reflogs->short_ref;\n+}\n+\n void get_reflog_selector(struct strbuf *sb,\n \t\t\t struct reflog_walk_info *reflog_info,\n \t\t\t struct date_mode dmode, int force_date,\n@@ -300,12 +392,7 @@ void get_reflog_selector(struct strbuf *sb,\n \t\treturn;\n \n \tif (shorten) {\n-\t\tif (!commit_reflog->reflogs->short_ref)\n-\t\t\tcommit_reflog->reflogs->short_ref\n-\t\t\t\t= refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t\t       commit_reflog->reflogs->ref,\n-\t\t\t\t\t\t\t       0);\n-\t\tprinted_ref = commit_reflog->reflogs->short_ref;\n+\t\tprinted_ref = short_ref(commit_reflog->reflogs);\n \t} else {\n \t\tprinted_ref = commit_reflog->reflogs->ref;\n \t}\n@@ -340,6 +427,18 @@ void get_reflog_message(struct strbuf *sb,\n \tstrbuf_add(sb, info->message, len);\n }\n \n+const char *get_reflog_branch(struct reflog_walk_info *reflog_info)\n+{\n+\tstruct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;\n+\n+\tif (!commit_reflog)\n+\t\treturn NULL;\n+\n+\tif (strcmp(commit_reflog->reflogs->ref, \"HEAD\"))\n+\t\treturn short_ref(commit_reflog->reflogs);\n+\treturn commit_reflog->reflogs->items[commit_reflog->recno+1].branch;\n+}\n+\n const char *get_reflog_ident(struct reflog_walk_info *reflog_info)\n {\n \tstruct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;\ndiff --git a/reflog-walk.h b/reflog-walk.h\nindex 1d86795f84..223f4a0dd3 100644\n--- a/reflog-walk.h\n+++ b/reflog-walk.h\n@@ -7,13 +7,15 @@ struct date_mode;\n \n void init_reflog_walk(struct reflog_walk_info **info);\n void reflog_walk_info_release(struct reflog_walk_info *info);\n-void reflog_walk_mark(struct reflog_walk_info *info, bool operations);\n+void reflog_walk_mark(struct reflog_walk_info *info, bool operations,\n+\t\t      bool historical_branch);\n int add_reflog_for_walk(struct reflog_walk_info *info,\n \t\t\tstruct commit *commit, const char *name);\n void show_reflog_message(struct reflog_walk_info *info, int,\n \t\t\t struct date_mode, int force_date);\n void get_reflog_message(struct strbuf *sb,\n \t\t\tstruct reflog_walk_info *reflog_info);\n+const char *get_reflog_branch(struct reflog_walk_info *reflog_info);\n const char *get_reflog_ident(struct reflog_walk_info *reflog_info);\n timestamp_t get_reflog_timestamp(struct reflog_walk_info *reflog_info);\n void get_reflog_selector(struct strbuf *sb,\ndiff --git a/revision.c b/revision.c\nindex 88785651fe..0367ad9b5e 100644\n--- a/revision.c\n+++ b/revision.c\n@@ -2622,6 +2622,8 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg\n \t\trevs->show_signature = 1;\n \t} else if (!strcmp(arg, \"--no-show-signature\")) {\n \t\trevs->show_signature = 0;\n+\t} else if (!strcmp(arg, \"--historical-branch\")) {\n+\t\trevs->historical_branch = 1;\n \t} else if (!strcmp(arg, \"--show-linear-break\")) {\n \t\trevs->break_bar = \"                    ..........\";\n \t\trevs->track_linear = 1;\n@@ -3249,11 +3251,15 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s\n \tif (!revs->reflog_info && revs->grep_filter.use_reflog_filter)\n \t\tdie(_(\"the option '%s' requires '%s'\"), \"--grep-reflog\", \"--walk-reflogs\");\n \n+\tif (!revs->reflog_info && revs->historical_branch)\n+\t\tdie(_(\"the option '%s' requires '%s'\"), \"--historical-branch\", \"--walk-reflogs\");\n+\n \tif (!revs->reflog_info && revs->reflog_operations)\n \t\tdie(_(\"the option '%s' requires '%s'\"), \"--operations\", \"--walk-reflogs\");\n \n \tif (revs->reflog_info)\n-\t\treflog_walk_mark(revs->reflog_info, revs->reflog_operations);\n+\t\treflog_walk_mark(revs->reflog_info, revs->reflog_operations,\n+\t\t\t\t revs->historical_branch);\n \n \tif (revs->line_level_traverse &&\n \t    (revs->full_diff ||\ndiff --git a/revision.h b/revision.h\nindex c4f5e06698..6a0fd120e5 100644\n--- a/revision.h\n+++ b/revision.h\n@@ -269,6 +269,7 @@ struct rev_info {\n \t\t\tshow_notes_given:1,\n \t\t\tshow_notes_by_default:1,\n \t\t\tshow_signature:1,\n+\t\t\thistorical_branch:1,\n \t\t\tpretty_given:1,\n \t\t\tabbrev_commit:1,\n \t\t\tabbrev_commit_given:1,\ndiff --git a/t/t1414-reflog-walk.sh b/t/t1414-reflog-walk.sh\nindex e15f6f2381..eaf8187ba7 100755\n--- a/t/t1414-reflog-walk.sh\n+++ b/t/t1414-reflog-walk.sh\n@@ -159,6 +159,52 @@ test_expect_success '--operations shows a rebase as one entry' '\n \t)\n '\n \n+test_expect_success '--historical-branch names the branch of each entry' '\n+\t(\n+\t\tcd operations &&\n+\n+\t\tcat >expect <<-\\EOF &&\n+\t\t(topic) HEAD@{0}: rebase (finish): returning to refs/heads/topic\n+\t\t(topic) HEAD@{1}: rebase (pick): C\n+\t\t(topic) HEAD@{2}: rebase (pick): B\n+\t\t(topic) HEAD@{3}: rebase (pick): A\n+\t\t(topic) HEAD@{4}: rebase (start): checkout main\n+\t\t(topic) HEAD@{5}: checkout: moving from main to topic\n+\t\t(main) HEAD@{6}: commit: upstream\n+\t\t(main) HEAD@{7}: checkout: moving from topic to main\n+\t\t(topic) HEAD@{8}: commit: C\n+\t\t(topic) HEAD@{9}: commit: B\n+\t\t(topic) HEAD@{10}: commit: A\n+\t\t(topic) HEAD@{11}: checkout: moving from main to topic\n+\t\t(main) HEAD@{12}: commit (initial): base\n+\t\tEOF\n+\t\tgit reflog --historical-branch >out &&\n+\t\tcut -d \" \" -f 2- out >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '--historical-branch takes the place of decorations' '\n+\t(\n+\t\tcd operations &&\n+\t\thash=$(git rev-parse --short HEAD) &&\n+\t\techo \"<YELLOW>$hash<RESET><YELLOW> (<RESET><BOLD;GREEN>topic<RESET><YELLOW>)<RESET>\" >expect &&\n+\t\tgit reflog --historical-branch --decorate --color -1 >out &&\n+\t\ttest_decode_color <out | sed \"s/ HEAD@.*//\" >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '--historical-branch names the ref of other reflogs' '\n+\t(\n+\t\tcd operations &&\n+\t\techo \"(main) main@{0}: commit: upstream\" >expect &&\n+\t\tgit reflog -1 main --historical-branch >out &&\n+\t\tcut -d \" \" -f 2- out >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n test_expect_success '--operations shows a stopped rebase as one entry once it finishes' '\n \tset_fake_editor &&\n \t(\n@@ -231,8 +277,55 @@ test_expect_success '--operations shows a pull --rebase as one entry' '\n \t)\n '\n \n-test_expect_success '--operations requires --walk-reflogs' '\n+test_expect_success '--historical-branch names the current branch when HEAD never switched' '\n+\t(\n+\t\tcd downstream &&\n+\t\techo \"(topic)\" >expect &&\n+\t\tgit reflog --historical-branch >out &&\n+\t\tcut -d \" \" -f 2 out | sort -u >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '--historical-branch follows a renamed branch' '\n+\t(\n+\t\tcd operations &&\n+\t\ttest_commit D &&\n+\t\tgit branch -m topic renamed &&\n+\t\ttest_commit E &&\n+\n+\t\tcat >expect <<-\\EOF &&\n+\t\t(renamed) HEAD@{0}: commit: E\n+\t\t(renamed) HEAD@{1}: Branch: renamed refs/heads/topic to refs/heads/renamed\n+\t\t(topic) HEAD@{3}: commit: D\n+\t\tEOF\n+\t\tgit reflog --historical-branch -3 >out &&\n+\t\tcut -d \" \" -f 2- out >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '--historical-branch shows where HEAD was detached' '\n+\t(\n+\t\tcd operations &&\n+\t\tgit checkout --detach &&\n+\t\ttest_commit F &&\n+\t\tgit checkout renamed &&\n+\n+\t\tcat >expect <<-EOF &&\n+\t\t(renamed) HEAD@{0}: checkout: moving from $(git rev-parse F) to renamed\n+\t\t(detached at $(git rev-parse --short F)) HEAD@{1}: commit: F\n+\t\tEOF\n+\t\tgit reflog --historical-branch -2 >out &&\n+\t\tcut -d \" \" -f 2- out >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '--operations and --historical-branch require --walk-reflogs' '\n \ttest_must_fail git log --operations 2>err &&\n+\ttest_grep \"requires .--walk-reflogs.\" err &&\n+\ttest_must_fail git log --historical-branch 2>err &&\n \ttest_grep \"requires .--walk-reflogs.\" err\n '\n \n-- \ngitgitgadget\n\n"},{"id":"555147","messageId":"b55b6643-b9a2-4e41-8100-2de545004845@app.fastmail.com","threadId":"66545","inReplyTo":"d95ed5bd679cea41320c39824b969377e8c88e09.1792070038.git.gitgitgadget@gmail.com","subject":"Re: [PATCH 1/3] reflog: name the strings that mark switches, renames and rebase steps","fromName":"Kristoffer Haugsbakk","fromEmail":"kristofferhaugsbakk@fastmail.com","sentAt":"2026-10-15T14:35:59Z","receivedAt":"2026-10-15T14:35:59Z","isPatch":true,"sender":{"key":"kristofferhaugsbakk@fastmail.com","avatar":null},"body":"On Thu, Oct 15, 2026, at 15:13, Harald Nordgren via GitGitGadget wrote:\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n>\n> \"git checkout\" records a move of HEAD in the reflog as \"checkout:\n> moving from <old> to <new>\", \"git branch -m\" records a rename of the\n> current branch as \"Branch: renamed <old> to <new>\", and \"git rebase\"\n> marks where it starts and ends with steps like \"rebase (start)\" and\n> \"rebase (finish)\". \"@{-<n>}\" and \"git status\" read the switch message\n> back, each with its own copy of the prefix,\n\nHere it feels like we are thrown into an enumeration without knowing\nwhat for. What is the problem to solve?\n\n> and a later commit adds a\n> reader for all of them.\n\nThis clause at the end of the six-line sentence seems to reveal why: we\nare about to use these strings in more than one place, so we want to\nintroduce constants in order to not duplicate anything (no drifting\napart). But the why here should have been put at the start. And the\nphrasing is also so passive that I had to do a double-take as to what\n“commit” meant. A commit in the reflog under discussion? Or a commit\nthat *we*/*I* are about to make?\n\nThe context is that you are about to use these prefixes in more than one\nplace. And the principle you are following is no duplication. Then the\ndetails follow. If the details of exactly what they say is worth stating\nin the commit message.\n\n>\n> Name these strings in reflog.h and use them where the messages are\n> written and read, so they cannot drift apart.\n>\n> Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n> ---\n>[snip]\n\n"},{"id":"555148","messageId":"fbbbabee-c586-4305-bdfe-3f7cb46b7191@app.fastmail.com","threadId":"66545","inReplyTo":"pull.2448.git.git.1792070038.gitgitgadget@gmail.com","subject":"Re: [PATCH 0/3] reflog: add --operations and --historical-branch","fromName":"Kristoffer Haugsbakk","fromEmail":"kristofferhaugsbakk@fastmail.com","sentAt":"2026-10-15T14:36:07Z","receivedAt":"2026-10-15T14:36:07Z","isPatch":true,"sender":{"key":"kristofferhaugsbakk@fastmail.com","avatar":null},"body":"On Thu, Oct 15, 2026, at 15:13, Harald Nordgren via GitGitGadget wrote:\n> Create a better reflog. Follows the discussion in\n> https://lore.kernel.org/git/akZfm-igZKeHaDST@pks.im/.\n>\n> \"git reflog\" lists every step of a rebase and every branch switch, which\n> makes it hard to see what was done and on which branch. This adds two\n> options to \"git reflog\" and \"git log -g\":\n>\n>  * --operations shows one entry per command, folding a rebase into the entry\n>    that finished or aborted it, and leaves out entries that change no\n>    branch.\n>  * --historical-branch decorates each entry with the branch HEAD was on when\n>    it was recorded.\n\nThis sounds very useful. That you rebase many commits and then the reset\npoints (where each one started and ended) get obscured in the reflog is\nclassic for me. And I think many have had the same issue. This is why I\nsometimes use `ORIG_HEAD` instead, or maybe make a backup ref.\n\n"},{"id":"555155","messageId":"CAHwyqnWyYhRZ0=byi3_yqT2phmVa2DY8XtxA8bQT=pwcetkMAg@mail.gmail.com","threadId":"66545","inReplyTo":"fbbbabee-c586-4305-bdfe-3f7cb46b7191@app.fastmail.com","subject":"Re: [PATCH 0/3] reflog: add --operations and --historical-branch","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-15T15:24:15Z","receivedAt":"2026-10-15T15:24:15Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> This sounds very useful. That you rebase many commits and then the reset\n> points (where each one started and ended) get obscured in the reflog is\n> classic for me. And I think many have had the same issue. This is why I\n> sometimes use `ORIG_HEAD` instead, or maybe make a backup ref.\n\nYes! I love the reflog. But I'm also endlessly confused by it.\n\nSometimes I'll just pick all the commits one by one and do 'git show'\non them to figure out which one to actually pick. Not a great\nexperience.\n\n\nHarald\n\n"},{"id":"555157","messageId":"f06c7de7-f9d5-4c7b-8c65-fc2cec1ee698@app.fastmail.com","threadId":"66545","inReplyTo":"CAHwyqnWyYhRZ0=byi3_yqT2phmVa2DY8XtxA8bQT=pwcetkMAg@mail.gmail.com","subject":"Re: [PATCH 0/3] reflog: add --operations and --historical-branch","fromName":"Kristoffer Haugsbakk","fromEmail":"kristofferhaugsbakk@fastmail.com","sentAt":"2026-10-15T15:35:05Z","receivedAt":"2026-10-15T15:35:05Z","isPatch":true,"sender":{"key":"kristofferhaugsbakk@fastmail.com","avatar":null},"body":"On Thu, Oct 15, 2026, at 17:24, Harald Nordgren wrote:\n>> This sounds very useful. That you rebase many commits and then the reset\n>> points (where each one started and ended) get obscured in the reflog is\n>> classic for me. And I think many have had the same issue. This is why I\n>> sometimes use `ORIG_HEAD` instead, or maybe make a backup ref.\n>\n> Yes! I love the reflog. But I'm also endlessly confused by it.\n>\n> Sometimes I'll just pick all the commits one by one and do 'git show'\n> on them to figure out which one to actually pick. Not a great\n> experience.\n\n:)\n\nI got a bit curious about this forced workflow now. I tried the first\nterminal navigate-and-click Git tool I could think of, tig(1), and it\nturns out that you can do this:\n\n    $ tig reflog\n    $ # navigate up/down j/k\n    $ # See the commit message and diff with `d`\n\n"},{"id":"555193","messageId":"CAHwyqnWqVVsMPseg4oczAZf0gKo1+1c5-kAeNctVKVGe90V1eQ@mail.gmail.com","threadId":"66545","inReplyTo":"f06c7de7-f9d5-4c7b-8c65-fc2cec1ee698@app.fastmail.com","subject":"Re: [PATCH 0/3] reflog: add --operations and --historical-branch","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-15T20:47:59Z","receivedAt":"2026-10-15T20:47:59Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> > Sometimes I'll just pick all the commits one by one and do 'git show'\n> > on them to figure out which one to actually pick. Not a great\n> > experience.\n>\n> :)\n>\n> I got a bit curious about this forced workflow now. I tried the first\n> terminal navigate-and-click Git tool I could think of, tig(1), and it\n> turns out that you can do this:\n>\n>     $ tig reflog\n>     $ # navigate up/down j/k\n>     $ # See the commit message and diff with `d`\n\nI don't use tig, lazygit, github desktop or vscode’s version control\nextension, not because they’re not good (they probably are) but only\nbecause I don't want to be removed from the fundamentals. I want to\nfeel the pain of using git :)\n\nBut that seems like an improvement, yes, thanks for pointing it out!\nGood news is that git options are passed through to tig, so with this\nnew feature we could run:\n\n    tig reflog --operations\n\n\nHarald\n\n"},{"id":"555194","messageId":"CAHwyqnUATY3chz+Ryz9D3fAmhbfTOr36pbNa_xLkT5yBgVWjyQ@mail.gmail.com","threadId":"66545","inReplyTo":"b55b6643-b9a2-4e41-8100-2de545004845@app.fastmail.com","subject":"Re: [PATCH 1/3] reflog: name the strings that mark switches, renames and rebase steps","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-15T20:52:08Z","receivedAt":"2026-10-15T20:52:08Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> [snip]\n>\n> The context is that you are about to use these prefixes in more than one\n> place. And the principle you are following is no duplication. Then the\n> details follow. If the details of exactly what they say is worth stating\n> in the commit message.\n\nThanks!\n\n\nHarald\n\n"},{"id":"555259","messageId":"CALnO6CC+6r=t_ENdFLBxyR=KWxW8hZ_0MPvH1o79TVLG8N5RGg@mail.gmail.com","threadId":"66545","inReplyTo":"CAHwyqnWyYhRZ0=byi3_yqT2phmVa2DY8XtxA8bQT=pwcetkMAg@mail.gmail.com","subject":"Re: [PATCH 0/3] reflog: add --operations and --historical-branch","fromName":"D. Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-10-16T18:26:53Z","receivedAt":"2026-10-16T18:26:53Z","isPatch":true,"sender":{"key":"ben.knoble@gmail.com","avatar":"https://avatars.githubusercontent.com/u/22802209?v=4"},"body":"On Thu, Oct 15, 2026 at 12:04 PM Harald Nordgren\n<haraldnordgren@gmail.com> wrote:\n>\n> > This sounds very useful. That you rebase many commits and then the reset\n> > points (where each one started and ended) get obscured in the reflog is\n> > classic for me. And I think many have had the same issue. This is why I\n> > sometimes use `ORIG_HEAD` instead, or maybe make a backup ref.\n>\n> Yes! I love the reflog. But I'm also endlessly confused by it.\n>\n> Sometimes I'll just pick all the commits one by one and do 'git show'\n> on them to figure out which one to actually pick. Not a great\n> experience.\n\nPerhaps `git log -g -p` in that case? :)\n\n-- \nD. Ben Knoble\n\n"},{"id":"555262","messageId":"CALnO6CDOh1NCp4Y9euLbhVFJJnOc1jSk7q0ekdUeGniG8LTCGQ@mail.gmail.com","threadId":"66545","inReplyTo":"pull.2448.git.git.1792070038.gitgitgadget@gmail.com","subject":"Re: [PATCH 0/3] reflog: add --operations and --historical-branch","fromName":"D. Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-10-16T18:36:25Z","receivedAt":"2026-10-16T18:36:25Z","isPatch":true,"sender":{"key":"ben.knoble@gmail.com","avatar":"https://avatars.githubusercontent.com/u/22802209?v=4"},"body":"[Apologies for replying in the wrong chain; it appears I haven't\nreceived 3/3 yet and it's easier for me to reply to a message I have\nthan one I can only see in lore.]\n\nOn Thu, Oct 15, 2026 at 9:20 AM Harald Nordgren via GitGitGadget\n<gitgitgadget@gmail.com> wrote:\n>   reflog: add --historical-branch to name the branch of each entry\n\n> Teach \"git reflog\" and \"git log -g\" a \"--historical-branch\" option\n> that decorates each entry with the branch it was made on, in place of\n> the refs that point at the commit now. The name is colored like a\n> branch decoration:\n>\n>   $ git reflog --historical-branch\n>   4e5f6a7 (topic) HEAD@{0}: commit: add tests\n>   1a2b3c4 (topic) HEAD@{1}: checkout: moving from main to topic\n>   8c9d0e1 (main) HEAD@{2}: commit: fix typo\n\nNotice how the \"HEAD@\" column is now not aligned? I find it really convient to\nhave fixed-width data at the front of the line so that I can visually pick out\npieces in columns.\n\nSo I'd suggest an alternat end-of-line format:\n\n    $ git reflog --historical-branch\n    4e5f6a7 HEAD@{0}: commit: add tests (topic)\n    1a2b3c4 HEAD@{1}: checkout: moving from main to topic (topic)\n    8c9d0e1 HEAD@{2}: commit: fix typo (main)\n\nThat might make picking out the branch name a little harder, so I suppose you\ncould try right before the messages?\n\n    $ git reflog --historical-branch\n    4e5f6a7 HEAD@{0}: (topic) commit: add tests\n    1a2b3c4 HEAD@{1}: (topic) checkout: moving from main to topic\n    8c9d0e1 HEAD@{2}: (main) commit: fix typo\n\nor (since the \"operation\" prefixes\" are useful)\n\n    $ git reflog --historical-branch\n    4e5f6a7 HEAD@{0}: commit: (topic) add tests\n    1a2b3c4 HEAD@{1}: checkout: (topic) moving from main to topic\n    8c9d0e1 HEAD@{2}: commit: (main) fix typo\n\n? Obviously the misalignment in all the examples (except the EOL format) gets\nworse when you have some short and some long branch names.\n\n"},{"id":"555311","messageId":"CAHwyqnUd5NwZMGYQeFhYB=PiJ-0peA8hSJezJGyNorDM1hzehw@mail.gmail.com","threadId":"66545","inReplyTo":"CALnO6CDOh1NCp4Y9euLbhVFJJnOc1jSk7q0ekdUeGniG8LTCGQ@mail.gmail.com","subject":"Re: [PATCH 0/3] reflog: add --operations and --historical-branch","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-17T10:03:57Z","receivedAt":"2026-10-17T10:03:57Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":">     $ git reflog --historical-branch\n>     4e5f6a7 HEAD@{0}: (topic) commit: add tests\n>     1a2b3c4 HEAD@{1}: (topic) checkout: moving from main to topic\n>     8c9d0e1 HEAD@{2}: (main) commit: fix typo\n\nThanks!\n\nThis is my favorite then.\n\n\nHarald\n\n"},{"id":"555357","messageId":"pull.2448.v2.git.git.1792348037.gitgitgadget@gmail.com","threadId":"66545","inReplyTo":"pull.2448.git.git.1792070038.gitgitgadget@gmail.com","subject":"[PATCH v2 0/3] reflog: add --operations and --historical-branch","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-18T18:27:14Z","receivedAt":"2026-10-18T18:27:14Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"Create a better reflog. Follows the discussion in\nhttps://lore.kernel.org/git/akZfm-igZKeHaDST@pks.im/.\n\n\"git reflog\" lists every step of a rebase and every branch switch, which\nmakes it hard to see what was done and on which branch. This adds two\noptions to \"git reflog\" and \"git log -g\":\n\n * --operations shows one entry per command, folding a rebase into the entry\n   that finished or aborted it, and leaves out entries that change no\n   branch.\n * --historical-branch decorates each entry with the branch HEAD was on when\n   it was recorded.\n\nChanges in v2:\n\n * Move the \"historical branch\" rightward in the display for cleaner\n   alignment.\n * Improve commit messages.\n\nHarald Nordgren (3):\n  reflog: name the strings that mark switches, renames and rebase steps\n  reflog: add --operations to show one entry per command\n  reflog: add --historical-branch to name the branch of each entry\n\n Documentation/git-reflog.adoc       |   4 +\n Documentation/rev-list-options.adoc |  21 +++\n builtin/branch.c                    |   3 +-\n builtin/checkout.c                  |   3 +-\n builtin/rebase.c                    |  17 +--\n log-tree.c                          |  28 +++-\n object-name.c                       |   3 +-\n reflog-walk.c                       | 183 +++++++++++++++++++++++--\n reflog-walk.h                       |   6 +-\n reflog.h                            |   6 +\n revision.c                          |  14 ++\n revision.h                          |   2 +\n sequencer.c                         |  13 +-\n t/t1414-reflog-walk.sh              | 198 ++++++++++++++++++++++++++++\n wt-status.c                         |   3 +-\n 15 files changed, 471 insertions(+), 33 deletions(-)\n\n\nbase-commit: c51408889c3b372814bc20b9431c3b74d0d268eb\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2448%2FHaraldNordgren%2Freflog-operations-v2\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2448/HaraldNordgren/reflog-operations-v2\nPull-Request: https://github.com/git/git/pull/2448\n\nRange-diff vs v1:\n\n 1:  d95ed5bd67 ! 1:  745929bd80 reflog: name the strings that mark switches, renames and rebase steps\n     @@ Metadata\n       ## Commit message ##\n          reflog: name the strings that mark switches, renames and rebase steps\n      \n     -    \"git checkout\" records a move of HEAD in the reflog as \"checkout:\n     -    moving from <old> to <new>\", \"git branch -m\" records a rename of the\n     -    current branch as \"Branch: renamed <old> to <new>\", and \"git rebase\"\n     -    marks where it starts and ends with steps like \"rebase (start)\" and\n     -    \"rebase (finish)\". \"@{-<n>}\" and \"git status\" read the switch message\n     -    back, each with its own copy of the prefix, and a later commit adds a\n     -    reader for all of them.\n     +    The next commit adds \"git reflog --operations\", which finds branch\n     +    switches, branch renames and the start and end of a rebase by the\n     +    messages they leave in the reflog. The commands that write these\n     +    messages spell out the strings by hand, and \"@{-<n>}\" and \"git status\"\n     +    each keep their own copy of the switch prefix to read it back. Reading\n     +    them in one more place would add yet another copy that has to stay in\n     +    sync.\n      \n     -    Name these strings in reflog.h and use them where the messages are\n     -    written and read, so they cannot drift apart.\n     +    Name the strings once in reflog.h and use the names wherever the\n     +    messages are written or read: the \"checkout: moving from \" prefix\n     +    written by \"git checkout\", the \"Branch: renamed \" prefix written by\n     +    \"git branch -m\", and the \"start\", \"finish\" and \"abort\" steps written\n     +    by \"git rebase\".\n     +\n     +    No change in behavior.\n      \n          Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n      \n 2:  579e13b36a = 2:  1c47ba225e reflog: add --operations to show one entry per command\n 3:  4efe74b04e ! 3:  aa5a98b80d reflog: add --historical-branch to name the branch of each entry\n     @@ Commit message\n          still point at that commit, and a rebased or amended commit has none.\n      \n          Teach \"git reflog\" and \"git log -g\" a \"--historical-branch\" option\n     -    that decorates each entry with the branch it was made on, in place of\n     -    the refs that point at the commit now. The name is colored like a\n     -    branch decoration:\n     +    that shows the branch each entry was made on before its message, in\n     +    place of the refs that point at the commit now. The hashes and\n     +    selectors stay lined up in their columns, and the name is colored like\n     +    a branch decoration:\n      \n            $ git reflog --historical-branch\n     -      4e5f6a7 (topic) HEAD@{0}: commit: add tests\n     -      1a2b3c4 (topic) HEAD@{1}: checkout: moving from main to topic\n     -      8c9d0e1 (main) HEAD@{2}: commit: fix typo\n     +      4e5f6a7 HEAD@{0}: (topic) commit: add tests\n     +      1a2b3c4 HEAD@{1}: (topic) checkout: moving from main to topic\n     +      8c9d0e1 HEAD@{2}: (main) commit: fix typo\n      \n          Every entry gets a name, including ones whose commits are no longer on\n          any branch. Entries from before a branch was renamed show its old name,\n     @@ Documentation/git-reflog.adoc: actions, and in addition the `HEAD` reflog record\n       branch are left out, and a rebase is shown as a single entry instead\n      -of one entry for each commit it replayed.\n      +of one entry for each commit it replayed. With `--historical-branch`,\n     -+each entry is decorated with the branch it was made on.\n     ++each entry shows the branch it was made on before its message.\n       \n       The \"list\" subcommand lists all refs which have a corresponding reflog.\n       \n     @@ Documentation/rev-list-options.adoc: Under `--pretty=reference`, this informatio\n       \trebase started. It is an error to use this option unless\n       \t`--walk-reflogs` is in use.\n       \n     ++ifndef::git-rev-list[]\n      +`--historical-branch`::\n     -+\tDecorate each reflog entry with the branch `HEAD` was on when\n     -+\tthe entry was recorded, such as `(topic)`, in place of the refs\n     -+\tthat point at the commit now. When `HEAD` was detached, the\n     -+\tentry shows `(detached at <commit>)`. For the reflog of any\n     -+\tother ref, the name of that ref is shown. It is an error to use\n     -+\tthis option unless `--walk-reflogs` is in use.\n     ++\tShow the branch `HEAD` was on when each reflog entry was\n     ++\trecorded, such as `(topic)`, before the reflog message, in\n     ++\tplace of the refs that point at the commit now. When `HEAD` was\n     ++\tdetached, the entry shows `(detached at <commit>)`. For the\n     ++\treflog of any other ref, the name of that ref is shown. It is\n     ++\tan error to use this option unless `--walk-reflogs` is in use.\n     ++endif::git-rev-list[]\n      +\n       `--merge`::\n       \tShow commits touching conflicted paths in the range `HEAD...<other>`,\n     @@ log-tree.c: void format_decorations(struct strbuf *sb,\n       \t}\n       }\n       \n     -+static void show_historical_branch(struct rev_info *opt)\n     ++static void format_historical_branch(struct strbuf *sb,\n     ++\t\t\t\t     struct rev_info *opt)\n      +{\n      +\tconst char *branch = get_reflog_branch(opt->reflog_info);\n      +\tenum git_colorbool use_color = opt->diffopt.use_color;\n     @@ log-tree.c: void format_decorations(struct strbuf *sb,\n      +\n      +\tcolor_commit = diff_get_color(use_color, DIFF_COMMIT);\n      +\tcolor_reset = decorate_get_color(use_color, DECORATION_NONE);\n     -+\tfprintf(opt->diffopt.file, \"%s (%s%s%s%s%s)%s\",\n     -+\t\tcolor_commit, color_reset,\n     -+\t\tdecorate_get_color(use_color, DECORATION_REF_LOCAL),\n     -+\t\tbranch, color_reset, color_commit, color_reset);\n     ++\tstrbuf_addf(sb, \"%s(%s%s%s%s%s)%s \",\n     ++\t\t    color_commit, color_reset,\n     ++\t\t    decorate_get_color(use_color, DECORATION_REF_LOCAL),\n     ++\t\t    branch, color_reset, color_commit, color_reset);\n      +}\n      +\n       void show_decorations(struct rev_info *opt, struct commit *commit)\n     @@ log-tree.c: void show_decorations(struct rev_info *opt, struct commit *commit)\n       \t\tif (slot && *slot)\n       \t\t\tfprintf(opt->diffopt.file, \"\\t%s\", *slot);\n       \t}\n     -+\tif (opt->historical_branch) {\n     -+\t\tshow_historical_branch(opt);\n     -+\t\treturn;\n     -+\t}\n     - \tif (!opt->show_decorations)\n     +-\tif (!opt->show_decorations)\n     ++\tif (!opt->show_decorations || opt->historical_branch)\n       \t\treturn;\n       \tformat_decorations(&sb, commit, opt->diffopt.use_color, NULL);\n     + \tfputs(sb.buf, opt->diffopt.file);\n     +@@ log-tree.c: void show_log(struct rev_info *opt)\n     + \t\t\tgraph_show_oneline(opt->graph);\n     + \t\t}\n     + \t\tif (opt->reflog_info) {\n     ++\t\t\tstruct strbuf branch = STRBUF_INIT;\n     ++\n     + \t\t\t/*\n     + \t\t\t * setup_revisions() ensures that opt->reflog_info\n     + \t\t\t * and opt->graph cannot both be set,\n     + \t\t\t * so we don't need to worry about printing the\n     + \t\t\t * graph info here.\n     + \t\t\t */\n     ++\t\t\tif (opt->historical_branch)\n     ++\t\t\t\tformat_historical_branch(&branch, opt);\n     + \t\t\tshow_reflog_message(opt->reflog_info,\n     + \t\t\t\t\t    opt->commit_format == CMIT_FMT_ONELINE,\n     + \t\t\t\t\t    opt->date_mode,\n     +-\t\t\t\t\t    opt->date_mode_explicit);\n     ++\t\t\t\t\t    opt->date_mode_explicit,\n     ++\t\t\t\t\t    branch.buf);\n     ++\t\t\tstrbuf_release(&branch);\n     + \t\t\tif (opt->commit_format == CMIT_FMT_ONELINE)\n     + \t\t\t\treturn;\n     + \t\t}\n      \n       ## reflog-walk.c ##\n      @@\n     @@ reflog-walk.c: void get_reflog_message(struct strbuf *sb,\n       const char *get_reflog_ident(struct reflog_walk_info *reflog_info)\n       {\n       \tstruct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;\n     +@@ reflog-walk.c: timestamp_t get_reflog_timestamp(struct reflog_walk_info *reflog_info)\n     + }\n     + \n     + void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,\n     +-\t\t\t struct date_mode dmode, int force_date)\n     ++\t\t\t struct date_mode dmode, int force_date,\n     ++\t\t\t const char *branch)\n     + {\n     + \tif (reflog_info && reflog_info->last_commit_reflog) {\n     + \t\tstruct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;\n     +@@ reflog-walk.c: void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,\n     + \t\tinfo = &commit_reflog->reflogs->items[commit_reflog->recno+1];\n     + \t\tget_reflog_selector(&selector, reflog_info, dmode, force_date, 0);\n     + \t\tif (oneline) {\n     +-\t\t\tprintf(\"%s: %s\", selector.buf, info->message);\n     ++\t\t\tprintf(\"%s: %s%s\", selector.buf, branch,\n     ++\t\t\t       info->message);\n     + \t\t}\n     + \t\telse {\n     +-\t\t\tprintf(\"Reflog: %s (%s)\\nReflog message: %s\",\n     +-\t\t\t       selector.buf, info->email, info->message);\n     ++\t\t\tprintf(\"Reflog: %s (%s)\\nReflog message: %s%s\",\n     ++\t\t\t       selector.buf, info->email, branch,\n     ++\t\t\t       info->message);\n     + \t\t}\n     + \n     + \t\tstrbuf_release(&selector);\n      \n       ## reflog-walk.h ##\n      @@ reflog-walk.h: struct date_mode;\n     @@ reflog-walk.h: struct date_mode;\n       int add_reflog_for_walk(struct reflog_walk_info *info,\n       \t\t\tstruct commit *commit, const char *name);\n       void show_reflog_message(struct reflog_walk_info *info, int,\n     - \t\t\t struct date_mode, int force_date);\n     +-\t\t\t struct date_mode, int force_date);\n     ++\t\t\t struct date_mode, int force_date,\n     ++\t\t\t const char *branch);\n       void get_reflog_message(struct strbuf *sb,\n       \t\t\tstruct reflog_walk_info *reflog_info);\n      +const char *get_reflog_branch(struct reflog_walk_info *reflog_info);\n     @@ t/t1414-reflog-walk.sh: test_expect_success '--operations shows a rebase as one\n      +\t\tcd operations &&\n      +\n      +\t\tcat >expect <<-\\EOF &&\n     -+\t\t(topic) HEAD@{0}: rebase (finish): returning to refs/heads/topic\n     -+\t\t(topic) HEAD@{1}: rebase (pick): C\n     -+\t\t(topic) HEAD@{2}: rebase (pick): B\n     -+\t\t(topic) HEAD@{3}: rebase (pick): A\n     -+\t\t(topic) HEAD@{4}: rebase (start): checkout main\n     -+\t\t(topic) HEAD@{5}: checkout: moving from main to topic\n     -+\t\t(main) HEAD@{6}: commit: upstream\n     -+\t\t(main) HEAD@{7}: checkout: moving from topic to main\n     -+\t\t(topic) HEAD@{8}: commit: C\n     -+\t\t(topic) HEAD@{9}: commit: B\n     -+\t\t(topic) HEAD@{10}: commit: A\n     -+\t\t(topic) HEAD@{11}: checkout: moving from main to topic\n     -+\t\t(main) HEAD@{12}: commit (initial): base\n     ++\t\tHEAD@{0}: (topic) rebase (finish): returning to refs/heads/topic\n     ++\t\tHEAD@{1}: (topic) rebase (pick): C\n     ++\t\tHEAD@{2}: (topic) rebase (pick): B\n     ++\t\tHEAD@{3}: (topic) rebase (pick): A\n     ++\t\tHEAD@{4}: (topic) rebase (start): checkout main\n     ++\t\tHEAD@{5}: (topic) checkout: moving from main to topic\n     ++\t\tHEAD@{6}: (main) commit: upstream\n     ++\t\tHEAD@{7}: (main) checkout: moving from topic to main\n     ++\t\tHEAD@{8}: (topic) commit: C\n     ++\t\tHEAD@{9}: (topic) commit: B\n     ++\t\tHEAD@{10}: (topic) commit: A\n     ++\t\tHEAD@{11}: (topic) checkout: moving from main to topic\n     ++\t\tHEAD@{12}: (main) commit (initial): base\n      +\t\tEOF\n      +\t\tgit reflog --historical-branch >out &&\n      +\t\tcut -d \" \" -f 2- out >actual &&\n     @@ t/t1414-reflog-walk.sh: test_expect_success '--operations shows a rebase as one\n      +\t(\n      +\t\tcd operations &&\n      +\t\thash=$(git rev-parse --short HEAD) &&\n     -+\t\techo \"<YELLOW>$hash<RESET><YELLOW> (<RESET><BOLD;GREEN>topic<RESET><YELLOW>)<RESET>\" >expect &&\n     ++\t\techo \"<YELLOW>$hash<RESET> HEAD@{0}: <YELLOW>(<RESET><BOLD;GREEN>topic<RESET><YELLOW>)<RESET> rebase (finish): returning to refs/heads/topic\" >expect &&\n      +\t\tgit reflog --historical-branch --decorate --color -1 >out &&\n     -+\t\ttest_decode_color <out | sed \"s/ HEAD@.*//\" >actual &&\n     ++\t\ttest_decode_color <out >actual &&\n      +\t\ttest_cmp expect actual\n      +\t)\n      +'\n     @@ t/t1414-reflog-walk.sh: test_expect_success '--operations shows a rebase as one\n      +test_expect_success '--historical-branch names the ref of other reflogs' '\n      +\t(\n      +\t\tcd operations &&\n     -+\t\techo \"(main) main@{0}: commit: upstream\" >expect &&\n     ++\t\techo \"main@{0}: (main) commit: upstream\" >expect &&\n      +\t\tgit reflog -1 main --historical-branch >out &&\n      +\t\tcut -d \" \" -f 2- out >actual &&\n      +\t\ttest_cmp expect actual\n     @@ t/t1414-reflog-walk.sh: test_expect_success '--operations shows a pull --rebase\n      +\t\tcd downstream &&\n      +\t\techo \"(topic)\" >expect &&\n      +\t\tgit reflog --historical-branch >out &&\n     -+\t\tcut -d \" \" -f 2 out | sort -u >actual &&\n     ++\t\tcut -d \" \" -f 3 out | sort -u >actual &&\n      +\t\ttest_cmp expect actual\n      +\t)\n      +'\n     @@ t/t1414-reflog-walk.sh: test_expect_success '--operations shows a pull --rebase\n      +\t\ttest_commit E &&\n      +\n      +\t\tcat >expect <<-\\EOF &&\n     -+\t\t(renamed) HEAD@{0}: commit: E\n     -+\t\t(renamed) HEAD@{1}: Branch: renamed refs/heads/topic to refs/heads/renamed\n     -+\t\t(topic) HEAD@{3}: commit: D\n     ++\t\tHEAD@{0}: (renamed) commit: E\n     ++\t\tHEAD@{1}: (renamed) Branch: renamed refs/heads/topic to refs/heads/renamed\n     ++\t\tHEAD@{3}: (topic) commit: D\n      +\t\tEOF\n      +\t\tgit reflog --historical-branch -3 >out &&\n      +\t\tcut -d \" \" -f 2- out >actual &&\n     @@ t/t1414-reflog-walk.sh: test_expect_success '--operations shows a pull --rebase\n      +\t\tgit checkout renamed &&\n      +\n      +\t\tcat >expect <<-EOF &&\n     -+\t\t(renamed) HEAD@{0}: checkout: moving from $(git rev-parse F) to renamed\n     -+\t\t(detached at $(git rev-parse --short F)) HEAD@{1}: commit: F\n     ++\t\tHEAD@{0}: (renamed) checkout: moving from $(git rev-parse F) to renamed\n     ++\t\tHEAD@{1}: (detached at $(git rev-parse --short F)) commit: F\n      +\t\tEOF\n      +\t\tgit reflog --historical-branch -2 >out &&\n      +\t\tcut -d \" \" -f 2- out >actual &&\n\n-- \ngitgitgadget\n\n"},{"id":"555358","messageId":"745929bd805784bebeccc7e3ddee638aaffaeded.1792348037.git.gitgitgadget@gmail.com","threadId":"66545","inReplyTo":"pull.2448.v2.git.git.1792348037.gitgitgadget@gmail.com","subject":"[PATCH v2 1/3] reflog: name the strings that mark switches, renames and rebase steps","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-18T18:27:15Z","receivedAt":"2026-10-18T18:27:15Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nThe next commit adds \"git reflog --operations\", which finds branch\nswitches, branch renames and the start and end of a rebase by the\nmessages they leave in the reflog. The commands that write these\nmessages spell out the strings by hand, and \"@{-<n>}\" and \"git status\"\neach keep their own copy of the switch prefix to read it back. Reading\nthem in one more place would add yet another copy that has to stay in\nsync.\n\nName the strings once in reflog.h and use the names wherever the\nmessages are written or read: the \"checkout: moving from \" prefix\nwritten by \"git checkout\", the \"Branch: renamed \" prefix written by\n\"git branch -m\", and the \"start\", \"finish\" and \"abort\" steps written\nby \"git rebase\".\n\nNo change in behavior.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n builtin/branch.c   |  3 ++-\n builtin/checkout.c |  3 ++-\n builtin/rebase.c   | 17 +++++++++--------\n object-name.c      |  3 ++-\n reflog.h           |  6 ++++++\n sequencer.c        | 13 +++++++------\n wt-status.c        |  3 ++-\n 7 files changed, 30 insertions(+), 18 deletions(-)\n\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex a613148fc7..30062ceb7c 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -13,6 +13,7 @@\n #include \"editor.h\"\n #include \"environment.h\"\n #include \"refs.h\"\n+#include \"reflog.h\"\n #include \"commit.h\"\n #include \"gettext.h\"\n #include \"object-name.h\"\n@@ -669,7 +670,7 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int\n \t\tstrbuf_addf(&logmsg, \"Branch: copied %s to %s\",\n \t\t\t    oldref.buf, newref.buf);\n \telse\n-\t\tstrbuf_addf(&logmsg, \"Branch: renamed %s to %s\",\n+\t\tstrbuf_addf(&logmsg, REFLOG_RENAME_PREFIX \"%s to %s\",\n \t\t\t    oldref.buf, newref.buf);\n \n \tif (!copy && !(oldref_usage & IS_ORPHAN) &&\ndiff --git a/builtin/checkout.c b/builtin/checkout.c\nindex 2d575a4f57..f011284084 100644\n--- a/builtin/checkout.c\n+++ b/builtin/checkout.c\n@@ -25,6 +25,7 @@\n #include \"preload-index.h\"\n #include \"read-cache.h\"\n #include \"refs.h\"\n+#include \"reflog.h\"\n #include \"remote.h\"\n #include \"repo-settings.h\"\n #include \"resolve-undo.h\"\n@@ -999,7 +1000,7 @@ static void update_refs_for_switch(const struct checkout_opts *opts,\n \n \treflog_msg = getenv(\"GIT_REFLOG_ACTION\");\n \tif (!reflog_msg)\n-\t\tstrbuf_addf(&msg, \"checkout: moving from %s to %s\",\n+\t\tstrbuf_addf(&msg, REFLOG_SWITCH_PREFIX \"%s to %s\",\n \t\t\told_desc ? old_desc : \"(invalid)\", new_branch_info->name);\n \telse\n \t\tstrbuf_insertstr(&msg, 0, reflog_msg);\ndiff --git a/builtin/rebase.c b/builtin/rebase.c\nindex 535db60181..26ef589e89 100644\n--- a/builtin/rebase.c\n+++ b/builtin/rebase.c\n@@ -17,6 +17,7 @@\n #include \"strvec.h\"\n #include \"dir.h\"\n #include \"refs.h\"\n+#include \"reflog.h\"\n #include \"config.h\"\n #include \"unpack-trees.h\"\n #include \"lockfile.h\"\n@@ -601,11 +602,11 @@ static int move_to_original_branch(struct rebase_options *opts)\n \tif (!opts->onto)\n \t\tBUG(\"move_to_original_branch without onto\");\n \n-\tstrbuf_addf(&branch_reflog, \"%s (finish): %s onto %s\",\n-\t\t    opts->reflog_action,\n+\tstrbuf_addf(&branch_reflog, \"%s (%s): %s onto %s\",\n+\t\t    opts->reflog_action, REFLOG_STEP_FINISH,\n \t\t    opts->head_name, oid_to_hex(&opts->onto->object.oid));\n-\tstrbuf_addf(&head_reflog, \"%s (finish): returning to %s\",\n-\t\t    opts->reflog_action, opts->head_name);\n+\tstrbuf_addf(&head_reflog, \"%s (%s): returning to %s\",\n+\t\t    opts->reflog_action, REFLOG_STEP_FINISH, opts->head_name);\n \tropts.branch = opts->head_name;\n \tropts.flags = RESET_WORKING_TREE_REFS_ONLY |\n \t\t      RESET_WORKING_TREE_UPDATE_HEAD;\n@@ -1413,8 +1414,8 @@ int cmd_rebase(int argc,\n \t\tif (read_basic_state(&options))\n \t\t\texit(1);\n \n-\t\tstrbuf_addf(&head_msg, \"%s (abort): returning to %s\",\n-\t\t\t    options.reflog_action,\n+\t\tstrbuf_addf(&head_msg, \"%s (%s): returning to %s\",\n+\t\t\t    options.reflog_action, REFLOG_STEP_ABORT,\n \t\t\t    options.head_name ? options.head_name\n \t\t\t\t\t      : oid_to_hex(&options.orig_head->object.oid));\n \t\tropts.oid = &options.orig_head->object.oid;\n@@ -1884,8 +1885,8 @@ int cmd_rebase(int argc,\n \t\tprintf(_(\"First, rewinding head to replay your work on top of \"\n \t\t\t \"it...\\n\"));\n \n-\tstrbuf_addf(&msg, \"%s (start): checkout %s\",\n-\t\t    options.reflog_action, options.onto_name);\n+\tstrbuf_addf(&msg, \"%s (%s): checkout %s\", options.reflog_action,\n+\t\t    REFLOG_STEP_START, options.onto_name);\n \tropts.oid = &options.onto->object.oid;\n \tropts.orig_head = &options.orig_head->object.oid;\n \tropts.flags = RESET_WORKING_TREE_DETACH |\ndiff --git a/object-name.c b/object-name.c\nindex 6546685760..5b6ce8e0e0 100644\n--- a/object-name.c\n+++ b/object-name.c\n@@ -13,6 +13,7 @@\n #include \"tree.h\"\n #include \"tree-walk.h\"\n #include \"refs.h\"\n+#include \"reflog.h\"\n #include \"remote.h\"\n #include \"dir.h\"\n #include \"odb.h\"\n@@ -1251,7 +1252,7 @@ static int grab_nth_branch_switch(const char *refname UNUSED,\n \tconst char *match = NULL, *target = NULL;\n \tsize_t len;\n \n-\tif (skip_prefix(message, \"checkout: moving from \", &match))\n+\tif (skip_prefix(message, REFLOG_SWITCH_PREFIX, &match))\n \t\ttarget = strstr(match, \" to \");\n \n \tif (!match || !target)\ndiff --git a/reflog.h b/reflog.h\nindex 3bdd1ca8a5..32c3737db4 100644\n--- a/reflog.h\n+++ b/reflog.h\n@@ -2,6 +2,12 @@\n #define REFLOG_H\n #include \"refs.h\"\n \n+#define REFLOG_SWITCH_PREFIX \"checkout: moving from \"\n+#define REFLOG_RENAME_PREFIX \"Branch: renamed \"\n+#define REFLOG_STEP_START \"start\"\n+#define REFLOG_STEP_FINISH \"finish\"\n+#define REFLOG_STEP_ABORT \"abort\"\n+\n #define REFLOG_EXPIRE_TOTAL   (1 << 0)\n #define REFLOG_EXPIRE_UNREACH (1 << 1)\n \ndiff --git a/sequencer.c b/sequencer.c\nindex efbfd1315b..c55df2d38f 100644\n--- a/sequencer.c\n+++ b/sequencer.c\n@@ -31,6 +31,7 @@\n #include \"merge-ort.h\"\n #include \"merge-ort-wrappers.h\"\n #include \"refs.h\"\n+#include \"reflog.h\"\n #include \"sparse-index.h\"\n #include \"strvec.h\"\n #include \"quote.h\"\n@@ -5011,8 +5012,8 @@ static int checkout_onto(struct repository *r, struct replay_opts *opts,\n \t\t\t RESET_WORKING_TREE_UPDATE_HEAD |\n \t\t\t RESET_WORKING_TREE_UPDATE_ORIG_HEAD |\n \t\t\t RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK,\n-\t\t.head_msg = reflog_message(opts, \"start\", \"checkout %s\",\n-\t\t\t\t\t   onto_name),\n+\t\t.head_msg = reflog_message(opts, REFLOG_STEP_START,\n+\t\t\t\t\t   \"checkout %s\", onto_name),\n \t\t.default_reflog_action = sequencer_reflog_action(opts)\n \t};\n \tif (reset_working_tree(r, &ropts)) {\n@@ -5310,16 +5311,16 @@ cleanup_head_ref:\n \t\t\t\tres = error(_(\"could not read 'onto'\"));\n \t\t\t\tgoto cleanup_head_ref;\n \t\t\t}\n-\t\t\tmsg = reflog_message(opts, \"finish\", \"%s onto %s\",\n-\t\t\t\thead_ref.buf, buf.buf);\n+\t\t\tmsg = reflog_message(opts, REFLOG_STEP_FINISH,\n+\t\t\t\t\"%s onto %s\", head_ref.buf, buf.buf);\n \t\t\tif (refs_update_ref(get_main_ref_store(the_repository), msg, head_ref.buf, &head, &orig,\n \t\t\t\t\t    REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) {\n \t\t\t\tres = error(_(\"could not update %s\"),\n \t\t\t\t\thead_ref.buf);\n \t\t\t\tgoto cleanup_head_ref;\n \t\t\t}\n-\t\t\tmsg = reflog_message(opts, \"finish\", \"returning to %s\",\n-\t\t\t\thead_ref.buf);\n+\t\t\tmsg = reflog_message(opts, REFLOG_STEP_FINISH,\n+\t\t\t\t\"returning to %s\", head_ref.buf);\n \t\t\tif (refs_update_symref(get_main_ref_store(the_repository), \"HEAD\", head_ref.buf, msg)) {\n \t\t\t\tres = error(_(\"could not update HEAD to %s\"),\n \t\t\t\t\thead_ref.buf);\ndiff --git a/wt-status.c b/wt-status.c\nindex 57772c7501..ffd54353a8 100644\n--- a/wt-status.c\n+++ b/wt-status.c\n@@ -22,6 +22,7 @@\n #include \"strvec.h\"\n #include \"remote.h\"\n #include \"refs.h\"\n+#include \"reflog.h\"\n #include \"submodule.h\"\n #include \"column.h\"\n #include \"read-cache.h\"\n@@ -1790,7 +1791,7 @@ static int grab_1st_switch(const char *refname UNUSED,\n \tstruct grab_1st_switch_cbdata *cb = cb_data;\n \tconst char *target = NULL, *end;\n \n-\tif (!skip_prefix(message, \"checkout: moving from \", &message))\n+\tif (!skip_prefix(message, REFLOG_SWITCH_PREFIX, &message))\n \t\treturn 0;\n \ttarget = strstr(message, \" to \");\n \tif (!target)\n-- \ngitgitgadget\n\n\n"},{"id":"555359","messageId":"1c47ba225e221a35e4df3057dbf47cea37a83fe5.1792348037.git.gitgitgadget@gmail.com","threadId":"66545","inReplyTo":"pull.2448.v2.git.git.1792348037.gitgitgadget@gmail.com","subject":"[PATCH v2 2/3] reflog: add --operations to show one entry per command","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-18T18:27:16Z","receivedAt":"2026-10-18T18:27:16Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nAfter a rebase, \"git reflog\" shows one entry for every commit the\nrebase replayed, plus one for its start and one for its end. Together\nwith an entry for every branch switch, finding where a branch was\nbefore the rebase means reading past all of them.\n\nTeach \"git reflog\" and \"git log -g\" an \"--operations\" option that\nshows each rebase as the single entry that finished or aborted it, and\nleaves out entries that change no branch, like branch switches and the\n\"reset: moving to HEAD\" that \"git stash\" and \"git merge --abort\" leave\nbehind:\n\n  $ git reflog --operations\n  4e5f6a7 HEAD@{0}: rebase (finish): returning to refs/heads/topic\n  8c9d0e1 HEAD@{5}: commit: fix typo\n  1a2b3c4 HEAD@{7}: commit: add tests\n\nThe HEAD@{<n>} numbers keep their usual values, so the previous entry\non the same branch can be used to undo the rebase:\n\n  $ git reset --hard HEAD@{7}\n\nCommands run while a rebase is stopped, such as an amend after an\n\"edit\", are folded into that rebase, and a \"git pull --rebase\" is shown\nas one entry the same way.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-reflog.adoc       |   3 +\n Documentation/rev-list-options.adoc |  11 +++\n reflog-walk.c                       |  61 +++++++++++++++-\n reflog-walk.h                       |   1 +\n revision.c                          |   8 +++\n revision.h                          |   1 +\n t/t1414-reflog-walk.sh              | 105 ++++++++++++++++++++++++++++\n 7 files changed, 188 insertions(+), 2 deletions(-)\n\ndiff --git a/Documentation/git-reflog.adoc b/Documentation/git-reflog.adoc\nindex 38af0c977a..672b9ea72e 100644\n--- a/Documentation/git-reflog.adoc\n+++ b/Documentation/git-reflog.adoc\n@@ -41,6 +41,9 @@ command-line (or `HEAD`, by default). The reflog covers all recent\n actions, and in addition the `HEAD` reflog records branch switching.\n `git reflog show` is an alias for `git log -g --abbrev-commit\n --pretty=oneline`; see linkgit:git-log[1] for more information.\n+With `--operations`, branch switches and other entries that change no\n+branch are left out, and a rebase is shown as a single entry instead\n+of one entry for each commit it replayed.\n \n The \"list\" subcommand lists all refs which have a corresponding reflog.\n \ndiff --git a/Documentation/rev-list-options.adoc b/Documentation/rev-list-options.adoc\nindex bd9f345690..a40ba2641b 100644\n--- a/Documentation/rev-list-options.adoc\n+++ b/Documentation/rev-list-options.adoc\n@@ -348,6 +348,17 @@ See also linkgit:git-reflog[1].\n +\n Under `--pretty=reference`, this information will not be shown at all.\n \n+`--operations`::\n+\tShow one reflog entry for each command that changed a branch.\n+\tA rebase is shown as only the entry that finished or aborted it,\n+\tincluding any commands run while it was stopped, such as\n+\t`git commit --amend`. Switching branches is not shown, and\n+\tneither is a reset that left `HEAD` where it was, such as the one\n+\t`git stash` does.\n+\tThe previous entry on the same branch is the state before the\n+\trebase started. It is an error to use this option unless\n+\t`--walk-reflogs` is in use.\n+\n `--merge`::\n \tShow commits touching conflicted paths in the range `HEAD...<other>`,\n \twhere `<other>` is the first existing pseudoref in `MERGE_HEAD`,\ndiff --git a/reflog-walk.c b/reflog-walk.c\nindex 4dbeaa93a7..82528f5b66 100644\n--- a/reflog-walk.c\n+++ b/reflog-walk.c\n@@ -3,6 +3,7 @@\n #include \"git-compat-util.h\"\n #include \"commit.h\"\n #include \"refs.h\"\n+#include \"reflog.h\"\n #include \"diff.h\"\n #include \"repository.h\"\n #include \"revision.h\"\n@@ -18,6 +19,7 @@ struct complete_reflogs {\n \t\ttimestamp_t timestamp;\n \t\tint tz;\n \t\tchar *message;\n+\t\tbool user_operation;\n \t} *items;\n \tint nr, alloc;\n };\n@@ -38,6 +40,7 @@ static int read_one_reflog(const char *refname UNUSED,\n \titem->timestamp = timestamp;\n \titem->tz = tz;\n \titem->message = xstrdup(message);\n+\titem->user_operation = true;\n \tarray->nr++;\n \treturn 0;\n }\n@@ -65,6 +68,46 @@ static void complete_reflogs_clear(void *util, const char *str UNUSED)\n \tfree_complete_reflog(array);\n }\n \n+static bool has_rebase_step(const char *message, const char *step)\n+{\n+\tsize_t end = strcspn(message, \":\\n\");\n+\tsize_t len = strlen(step);\n+\n+\treturn end >= len + 3 && message[end - 1] == ')' &&\n+\t       !strncmp(message + end - len - 3, \" (\", 2) &&\n+\t       !strncmp(message + end - len - 1, step, len);\n+}\n+\n+static void mark_operations(struct complete_reflogs *array)\n+{\n+\tint start = -1;\n+\n+\tfor (int i = 0; i < array->nr; i++) {\n+\t\tstruct reflog_info *item = &array->items[i];\n+\t\tconst char *msg = item->message;\n+\n+\t\tif (has_rebase_step(msg, REFLOG_STEP_FINISH) ||\n+\t\t    has_rebase_step(msg, REFLOG_STEP_ABORT)) {\n+\t\t\tif (start >= 0)\n+\t\t\t\tfor (int j = start; j < i; j++)\n+\t\t\t\t\tarray->items[j].user_operation = false;\n+\t\t\tstart = -1;\n+\t\t\titem->user_operation = true;\n+\t\t\tcontinue;\n+\t\t}\n+\t\tif (has_rebase_step(msg, REFLOG_STEP_START))\n+\t\t\tstart = i;\n+\n+\t\tif (starts_with(msg, REFLOG_SWITCH_PREFIX))\n+\t\t\titem->user_operation = false;\n+\t\telse if (starts_with(msg, REFLOG_RENAME_PREFIX))\n+\t\t\titem->user_operation = true;\n+\t\telse\n+\t\t\titem->user_operation =\n+\t\t\t\t!oideq(&item->ooid, &item->noid);\n+\t}\n+}\n+\n static struct complete_reflogs *read_complete_reflog(const char *ref)\n {\n \tstruct complete_reflogs *reflogs =\n@@ -135,6 +178,16 @@ void init_reflog_walk(struct reflog_walk_info **info)\n \t(*info)->complete_reflogs.strdup_strings = 1;\n }\n \n+void reflog_walk_mark(struct reflog_walk_info *info, bool operations)\n+{\n+\tstruct string_list_item *item;\n+\n+\tfor_each_string_list_item(item, &info->complete_reflogs) {\n+\t\tif (operations)\n+\t\t\tmark_operations(item->util);\n+\t}\n+}\n+\n void reflog_walk_info_release(struct reflog_walk_info *info)\n {\n \tsize_t i;\n@@ -342,8 +395,12 @@ static struct commit *next_reflog_commit(struct commit_reflog *log)\n {\n \tfor (; log->recno >= 0; log->recno--) {\n \t\tstruct reflog_info *entry = &log->reflogs->items[log->recno];\n-\t\tstruct object *obj = parse_object(the_repository,\n-\t\t\t\t\t\t  &entry->noid);\n+\t\tstruct object *obj;\n+\n+\t\tif (!entry->user_operation)\n+\t\t\tcontinue;\n+\n+\t\tobj = parse_object(the_repository, &entry->noid);\n \n \t\tif (obj && obj->type == OBJ_COMMIT)\n \t\t\treturn (struct commit *)obj;\ndiff --git a/reflog-walk.h b/reflog-walk.h\nindex 989583dc55..1d86795f84 100644\n--- a/reflog-walk.h\n+++ b/reflog-walk.h\n@@ -7,6 +7,7 @@ struct date_mode;\n \n void init_reflog_walk(struct reflog_walk_info **info);\n void reflog_walk_info_release(struct reflog_walk_info *info);\n+void reflog_walk_mark(struct reflog_walk_info *info, bool operations);\n int add_reflog_for_walk(struct reflog_walk_info *info,\n \t\t\tstruct commit *commit, const char *name);\n void show_reflog_message(struct reflog_walk_info *info, int,\ndiff --git a/revision.c b/revision.c\nindex a83e499047..88785651fe 100644\n--- a/revision.c\n+++ b/revision.c\n@@ -2458,6 +2458,8 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg\n \t\tcommit_list_insert(c, &revs->ancestry_path_bottoms);\n \t} else if (!strcmp(arg, \"-g\") || !strcmp(arg, \"--walk-reflogs\")) {\n \t\tinit_reflog_walk(&revs->reflog_info);\n+\t} else if (!strcmp(arg, \"--operations\")) {\n+\t\trevs->reflog_operations = 1;\n \t} else if (!strcmp(arg, \"--default\")) {\n \t\tif (argc <= 1)\n \t\t\treturn error(\"bad --default argument\");\n@@ -3247,6 +3249,12 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s\n \tif (!revs->reflog_info && revs->grep_filter.use_reflog_filter)\n \t\tdie(_(\"the option '%s' requires '%s'\"), \"--grep-reflog\", \"--walk-reflogs\");\n \n+\tif (!revs->reflog_info && revs->reflog_operations)\n+\t\tdie(_(\"the option '%s' requires '%s'\"), \"--operations\", \"--walk-reflogs\");\n+\n+\tif (revs->reflog_info)\n+\t\treflog_walk_mark(revs->reflog_info, revs->reflog_operations);\n+\n \tif (revs->line_level_traverse &&\n \t    (revs->full_diff ||\n \t     (revs->diffopt.output_format &\ndiff --git a/revision.h b/revision.h\nindex e5dabd18ce..c4f5e06698 100644\n--- a/revision.h\n+++ b/revision.h\n@@ -221,6 +221,7 @@ struct rev_info {\n \t\t\texclude_first_parent_only:1,\n \t\t\tline_level_traverse:1,\n \t\t\ttree_blobs_in_commit_order:1,\n+\t\t\treflog_operations:1,\n \n \t\t\t/*\n \t\t\t * Blobs are shown without regard for their existence.\ndiff --git a/t/t1414-reflog-walk.sh b/t/t1414-reflog-walk.sh\nindex be6c3f472c..e15f6f2381 100755\n--- a/t/t1414-reflog-walk.sh\n+++ b/t/t1414-reflog-walk.sh\n@@ -5,6 +5,7 @@ GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main\n export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME\n \n . ./test-lib.sh\n+. \"$TEST_DIRECTORY\"/lib-rebase.sh\n \n test_expect_success 'set up some reflog entries' '\n \ttest_commit one &&\n@@ -131,6 +132,110 @@ test_expect_success 'walk prefers reflog to ref tip' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success '--operations shows a rebase as one entry' '\n+\tgit init operations &&\n+\t(\n+\t\tcd operations &&\n+\t\ttest_commit base &&\n+\t\tgit switch -c topic &&\n+\t\ttest_commit A &&\n+\t\ttest_commit B &&\n+\t\ttest_commit C &&\n+\t\tgit switch main &&\n+\t\ttest_commit upstream &&\n+\t\tgit switch topic &&\n+\t\tgit rebase main &&\n+\n+\t\tcat >expect <<-\\EOF &&\n+\t\tHEAD@{0} rebase (finish): returning to refs/heads/topic\n+\t\tHEAD@{6} commit: upstream\n+\t\tHEAD@{8} commit: C\n+\t\tHEAD@{9} commit: B\n+\t\tHEAD@{10} commit: A\n+\t\tHEAD@{12} commit (initial): base\n+\t\tEOF\n+\t\tgit log -g --format=\"%gd %gs\" HEAD --operations >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '--operations shows a stopped rebase as one entry once it finishes' '\n+\tset_fake_editor &&\n+\t(\n+\t\tcd operations &&\n+\t\tFAKE_LINES=\"edit 1 2\" git rebase -i HEAD~2 &&\n+\n+\t\tcat >expect <<-\\EOF &&\n+\t\tHEAD@{0} rebase: fast-forward\n+\t\tHEAD@{1} rebase (start): checkout HEAD~2\n+\t\tEOF\n+\t\tgit log -g --operations --format=\"%gd %gs\" -2 >actual &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\tgit commit --amend -m \"B amended\" &&\n+\t\tgit rebase --continue &&\n+\n+\t\tcat >expect <<-\\EOF &&\n+\t\tHEAD@{0} rebase (finish): returning to refs/heads/topic\n+\t\tHEAD@{5} rebase (finish): returning to refs/heads/topic\n+\t\tEOF\n+\t\tgit log -g --operations --format=\"%gd %gs\" -2 >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '--operations shows an aborted rebase as one entry' '\n+\t(\n+\t\tcd operations &&\n+\t\tFAKE_LINES=\"edit 1 2\" git rebase -i HEAD~2 &&\n+\t\tgit rebase --abort &&\n+\n+\t\tcat >expect <<-\\EOF &&\n+\t\tHEAD@{0} rebase (abort): returning to refs/heads/topic\n+\t\tHEAD@{3} rebase (finish): returning to refs/heads/topic\n+\t\tEOF\n+\t\tgit log -g --operations --format=\"%gd %gs\" -2 >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '--operations leaves out the reset done by git stash' '\n+\t(\n+\t\tcd operations &&\n+\t\techo change >>C.t &&\n+\t\tgit stash &&\n+\n+\t\techo \"HEAD@{1} rebase (abort): returning to refs/heads/topic\" >expect &&\n+\t\tgit log -g --operations --format=\"%gd %gs\" -1 >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '--operations shows a pull --rebase as one entry' '\n+\tgit clone operations downstream &&\n+\t(\n+\t\tcd operations &&\n+\t\ttest_commit upstream-change\n+\t) &&\n+\t(\n+\t\tcd downstream &&\n+\t\ttest_commit local-change &&\n+\t\tgit pull --rebase &&\n+\n+\t\tcat >expect <<-\\EOF &&\n+\t\tHEAD@{0} pull --rebase (finish): returning to refs/heads/topic\n+\t\tHEAD@{3} commit: local-change\n+\t\tEOF\n+\t\tgit log -g --operations --format=\"%gd %gs\" -2 >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '--operations requires --walk-reflogs' '\n+\ttest_must_fail git log --operations 2>err &&\n+\ttest_grep \"requires .--walk-reflogs.\" err\n+'\n+\n test_expect_success 'rev-list -g complains when there are no reflogs' '\n \ttest_must_fail git rev-list -g\n '\n-- \ngitgitgadget\n\n\n"},{"id":"555360","messageId":"aa5a98b80d2682cc0fa321c091c76e483fb6810f.1792348037.git.gitgitgadget@gmail.com","threadId":"66545","inReplyTo":"pull.2448.v2.git.git.1792348037.gitgitgadget@gmail.com","subject":"[PATCH v2 3/3] reflog: add --historical-branch to name the branch of each entry","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-18T18:27:17Z","receivedAt":"2026-10-18T18:27:17Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\n\"git reflog\" does not say which branch HEAD was on when an entry was\nrecorded. After switching between a few branches, telling which\ncommits and rebases belong to which branch means reading back to the\nlast \"checkout: moving from\" entry for each of them. Commit\ndecorations do not help either, since they only name branches that\nstill point at that commit, and a rebased or amended commit has none.\n\nTeach \"git reflog\" and \"git log -g\" a \"--historical-branch\" option\nthat shows the branch each entry was made on before its message, in\nplace of the refs that point at the commit now. The hashes and\nselectors stay lined up in their columns, and the name is colored like\na branch decoration:\n\n  $ git reflog --historical-branch\n  4e5f6a7 HEAD@{0}: (topic) commit: add tests\n  1a2b3c4 HEAD@{1}: (topic) checkout: moving from main to topic\n  8c9d0e1 HEAD@{2}: (main) commit: fix typo\n\nEvery entry gets a name, including ones whose commits are no longer on\nany branch. Entries from before a branch was renamed show its old name,\nand entries made on a detached HEAD show \"detached at <commit>\".\nCombined with \"--operations\", which leaves out the branch switches\nthemselves, it lists what was done to each branch.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/git-reflog.adoc       |   3 +-\n Documentation/rev-list-options.adoc |  10 +++\n log-tree.c                          |  28 ++++++-\n reflog-walk.c                       | 124 +++++++++++++++++++++++++---\n reflog-walk.h                       |   7 +-\n revision.c                          |   8 +-\n revision.h                          |   1 +\n t/t1414-reflog-walk.sh              |  95 ++++++++++++++++++++-\n 8 files changed, 258 insertions(+), 18 deletions(-)\n\ndiff --git a/Documentation/git-reflog.adoc b/Documentation/git-reflog.adoc\nindex 672b9ea72e..04ed7849c2 100644\n--- a/Documentation/git-reflog.adoc\n+++ b/Documentation/git-reflog.adoc\n@@ -43,7 +43,8 @@ actions, and in addition the `HEAD` reflog records branch switching.\n --pretty=oneline`; see linkgit:git-log[1] for more information.\n With `--operations`, branch switches and other entries that change no\n branch are left out, and a rebase is shown as a single entry instead\n-of one entry for each commit it replayed.\n+of one entry for each commit it replayed. With `--historical-branch`,\n+each entry shows the branch it was made on before its message.\n \n The \"list\" subcommand lists all refs which have a corresponding reflog.\n \ndiff --git a/Documentation/rev-list-options.adoc b/Documentation/rev-list-options.adoc\nindex a40ba2641b..8c0010695b 100644\n--- a/Documentation/rev-list-options.adoc\n+++ b/Documentation/rev-list-options.adoc\n@@ -359,6 +359,16 @@ Under `--pretty=reference`, this information will not be shown at all.\n \trebase started. It is an error to use this option unless\n \t`--walk-reflogs` is in use.\n \n+ifndef::git-rev-list[]\n+`--historical-branch`::\n+\tShow the branch `HEAD` was on when each reflog entry was\n+\trecorded, such as `(topic)`, before the reflog message, in\n+\tplace of the refs that point at the commit now. When `HEAD` was\n+\tdetached, the entry shows `(detached at <commit>)`. For the\n+\treflog of any other ref, the name of that ref is shown. It is\n+\tan error to use this option unless `--walk-reflogs` is in use.\n+endif::git-rev-list[]\n+\n `--merge`::\n \tShow commits touching conflicted paths in the range `HEAD...<other>`,\n \twhere `<other>` is the first existing pseudoref in `MERGE_HEAD`,\ndiff --git a/log-tree.c b/log-tree.c\nindex 83a3c4bf9b..b2d9a954e7 100644\n--- a/log-tree.c\n+++ b/log-tree.c\n@@ -421,6 +421,24 @@ void format_decorations(struct strbuf *sb,\n \t}\n }\n \n+static void format_historical_branch(struct strbuf *sb,\n+\t\t\t\t     struct rev_info *opt)\n+{\n+\tconst char *branch = get_reflog_branch(opt->reflog_info);\n+\tenum git_colorbool use_color = opt->diffopt.use_color;\n+\tconst char *color_commit, *color_reset;\n+\n+\tif (!branch)\n+\t\treturn;\n+\n+\tcolor_commit = diff_get_color(use_color, DIFF_COMMIT);\n+\tcolor_reset = decorate_get_color(use_color, DECORATION_NONE);\n+\tstrbuf_addf(sb, \"%s(%s%s%s%s%s)%s \",\n+\t\t    color_commit, color_reset,\n+\t\t    decorate_get_color(use_color, DECORATION_REF_LOCAL),\n+\t\t    branch, color_reset, color_commit, color_reset);\n+}\n+\n void show_decorations(struct rev_info *opt, struct commit *commit)\n {\n \tstruct strbuf sb = STRBUF_INIT;\n@@ -431,7 +449,7 @@ void show_decorations(struct rev_info *opt, struct commit *commit)\n \t\tif (slot && *slot)\n \t\t\tfprintf(opt->diffopt.file, \"\\t%s\", *slot);\n \t}\n-\tif (!opt->show_decorations)\n+\tif (!opt->show_decorations || opt->historical_branch)\n \t\treturn;\n \tformat_decorations(&sb, commit, opt->diffopt.use_color, NULL);\n \tfputs(sb.buf, opt->diffopt.file);\n@@ -834,16 +852,22 @@ void show_log(struct rev_info *opt)\n \t\t\tgraph_show_oneline(opt->graph);\n \t\t}\n \t\tif (opt->reflog_info) {\n+\t\t\tstruct strbuf branch = STRBUF_INIT;\n+\n \t\t\t/*\n \t\t\t * setup_revisions() ensures that opt->reflog_info\n \t\t\t * and opt->graph cannot both be set,\n \t\t\t * so we don't need to worry about printing the\n \t\t\t * graph info here.\n \t\t\t */\n+\t\t\tif (opt->historical_branch)\n+\t\t\t\tformat_historical_branch(&branch, opt);\n \t\t\tshow_reflog_message(opt->reflog_info,\n \t\t\t\t\t    opt->commit_format == CMIT_FMT_ONELINE,\n \t\t\t\t\t    opt->date_mode,\n-\t\t\t\t\t    opt->date_mode_explicit);\n+\t\t\t\t\t    opt->date_mode_explicit,\n+\t\t\t\t\t    branch.buf);\n+\t\t\tstrbuf_release(&branch);\n \t\t\tif (opt->commit_format == CMIT_FMT_ONELINE)\n \t\t\t\treturn;\n \t\t}\ndiff --git a/reflog-walk.c b/reflog-walk.c\nindex 82528f5b66..0cdca76efd 100644\n--- a/reflog-walk.c\n+++ b/reflog-walk.c\n@@ -5,6 +5,8 @@\n #include \"refs.h\"\n #include \"reflog.h\"\n #include \"diff.h\"\n+#include \"hex.h\"\n+#include \"object-name.h\"\n #include \"repository.h\"\n #include \"revision.h\"\n #include \"string-list.h\"\n@@ -19,6 +21,7 @@ struct complete_reflogs {\n \t\ttimestamp_t timestamp;\n \t\tint tz;\n \t\tchar *message;\n+\t\tchar *branch;\n \t\tbool user_operation;\n \t} *items;\n \tint nr, alloc;\n@@ -40,6 +43,7 @@ static int read_one_reflog(const char *refname UNUSED,\n \titem->timestamp = timestamp;\n \titem->tz = tz;\n \titem->message = xstrdup(message);\n+\titem->branch = NULL;\n \titem->user_operation = true;\n \tarray->nr++;\n \treturn 0;\n@@ -55,6 +59,7 @@ static void free_complete_reflog(struct complete_reflogs *array)\n \tfor (i = 0; i < array->nr; i++) {\n \t\tfree(array->items[i].email);\n \t\tfree(array->items[i].message);\n+\t\tfree(array->items[i].branch);\n \t}\n \tfree(array->items);\n \tfree(array->ref);\n@@ -108,6 +113,80 @@ static void mark_operations(struct complete_reflogs *array)\n \t}\n }\n \n+static char *branch_label(const char *name, size_t len)\n+{\n+\tstruct object_id oid;\n+\tconst char *short_name;\n+\n+\tif (len == the_hash_algo->hexsz && !get_oid_hex(name, &oid))\n+\t\treturn xstrfmt(\"detached at %s\",\n+\t\t\t       repo_find_unique_abbrev(the_repository, &oid,\n+\t\t\t\t\t\t       DEFAULT_ABBREV));\n+\tif (skip_prefix(name, \"refs/heads/\", &short_name))\n+\t\tlen -= short_name - name;\n+\telse\n+\t\tshort_name = name;\n+\treturn xmemdupz(short_name, len);\n+}\n+\n+struct branch_move {\n+\tconst char *from, *to;\n+\tsize_t from_len, to_len;\n+};\n+\n+static bool parse_branch_move(const char *message, struct branch_move *move)\n+{\n+\tconst char *to;\n+\n+\tif (!skip_prefix(message, REFLOG_SWITCH_PREFIX, &move->from) &&\n+\t    !skip_prefix(message, REFLOG_RENAME_PREFIX, &move->from))\n+\t\treturn false;\n+\tto = strstr(move->from, \" to \");\n+\tif (!to)\n+\t\treturn false;\n+\tmove->from_len = to - move->from;\n+\tmove->to = to + strlen(\" to \");\n+\tmove->to_len = strcspn(move->to, \"\\n\");\n+\treturn true;\n+}\n+\n+static void mark_branches(struct complete_reflogs *array)\n+{\n+\tchar *branch = NULL;\n+\n+\tif (strcmp(array->ref, \"HEAD\"))\n+\t\treturn;\n+\n+\tfor (int i = array->nr - 1; i >= 0; i--) {\n+\t\tstruct reflog_info *item = &array->items[i];\n+\t\tstruct branch_move move;\n+\n+\t\tif (!parse_branch_move(item->message, &move)) {\n+\t\t\titem->branch = xstrdup_or_null(branch);\n+\t\t\tcontinue;\n+\t\t}\n+\n+\t\tif (!branch)\n+\t\t\tfor (int j = i + 1; j < array->nr; j++)\n+\t\t\t\tarray->items[j].branch =\n+\t\t\t\t\tbranch_label(move.to, move.to_len);\n+\t\titem->branch = branch_label(move.to, move.to_len);\n+\t\tfree(branch);\n+\t\tbranch = branch_label(move.from, move.from_len);\n+\t}\n+\n+\tif (!branch) {\n+\t\tstruct ref_store *refs = get_main_ref_store(the_repository);\n+\t\tconst char *head = refs_resolve_ref_unsafe(refs, \"HEAD\", 0,\n+\t\t\t\t\t\t\t   NULL, NULL);\n+\n+\t\tif (head && skip_prefix(head, \"refs/heads/\", &head))\n+\t\t\tfor (int i = 0; i < array->nr; i++)\n+\t\t\t\tarray->items[i].branch = xstrdup(head);\n+\t}\n+\tfree(branch);\n+}\n+\n static struct complete_reflogs *read_complete_reflog(const char *ref)\n {\n \tstruct complete_reflogs *reflogs =\n@@ -178,13 +257,16 @@ void init_reflog_walk(struct reflog_walk_info **info)\n \t(*info)->complete_reflogs.strdup_strings = 1;\n }\n \n-void reflog_walk_mark(struct reflog_walk_info *info, bool operations)\n+void reflog_walk_mark(struct reflog_walk_info *info, bool operations,\n+\t\t      bool historical_branch)\n {\n \tstruct string_list_item *item;\n \n \tfor_each_string_list_item(item, &info->complete_reflogs) {\n \t\tif (operations)\n \t\t\tmark_operations(item->util);\n+\t\tif (historical_branch)\n+\t\t\tmark_branches(item->util);\n \t}\n }\n \n@@ -287,6 +369,16 @@ int add_reflog_for_walk(struct reflog_walk_info *info,\n \treturn 0;\n }\n \n+static const char *short_ref(struct complete_reflogs *reflogs)\n+{\n+\tstruct ref_store *refs = get_main_ref_store(the_repository);\n+\n+\tif (!reflogs->short_ref)\n+\t\treflogs->short_ref =\n+\t\t\trefs_shorten_unambiguous_ref(refs, reflogs->ref, 0);\n+\treturn reflogs->short_ref;\n+}\n+\n void get_reflog_selector(struct strbuf *sb,\n \t\t\t struct reflog_walk_info *reflog_info,\n \t\t\t struct date_mode dmode, int force_date,\n@@ -300,12 +392,7 @@ void get_reflog_selector(struct strbuf *sb,\n \t\treturn;\n \n \tif (shorten) {\n-\t\tif (!commit_reflog->reflogs->short_ref)\n-\t\t\tcommit_reflog->reflogs->short_ref\n-\t\t\t\t= refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t\t       commit_reflog->reflogs->ref,\n-\t\t\t\t\t\t\t       0);\n-\t\tprinted_ref = commit_reflog->reflogs->short_ref;\n+\t\tprinted_ref = short_ref(commit_reflog->reflogs);\n \t} else {\n \t\tprinted_ref = commit_reflog->reflogs->ref;\n \t}\n@@ -340,6 +427,18 @@ void get_reflog_message(struct strbuf *sb,\n \tstrbuf_add(sb, info->message, len);\n }\n \n+const char *get_reflog_branch(struct reflog_walk_info *reflog_info)\n+{\n+\tstruct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;\n+\n+\tif (!commit_reflog)\n+\t\treturn NULL;\n+\n+\tif (strcmp(commit_reflog->reflogs->ref, \"HEAD\"))\n+\t\treturn short_ref(commit_reflog->reflogs);\n+\treturn commit_reflog->reflogs->items[commit_reflog->recno+1].branch;\n+}\n+\n const char *get_reflog_ident(struct reflog_walk_info *reflog_info)\n {\n \tstruct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;\n@@ -365,7 +464,8 @@ timestamp_t get_reflog_timestamp(struct reflog_walk_info *reflog_info)\n }\n \n void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,\n-\t\t\t struct date_mode dmode, int force_date)\n+\t\t\t struct date_mode dmode, int force_date,\n+\t\t\t const char *branch)\n {\n \tif (reflog_info && reflog_info->last_commit_reflog) {\n \t\tstruct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;\n@@ -375,11 +475,13 @@ void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,\n \t\tinfo = &commit_reflog->reflogs->items[commit_reflog->recno+1];\n \t\tget_reflog_selector(&selector, reflog_info, dmode, force_date, 0);\n \t\tif (oneline) {\n-\t\t\tprintf(\"%s: %s\", selector.buf, info->message);\n+\t\t\tprintf(\"%s: %s%s\", selector.buf, branch,\n+\t\t\t       info->message);\n \t\t}\n \t\telse {\n-\t\t\tprintf(\"Reflog: %s (%s)\\nReflog message: %s\",\n-\t\t\t       selector.buf, info->email, info->message);\n+\t\t\tprintf(\"Reflog: %s (%s)\\nReflog message: %s%s\",\n+\t\t\t       selector.buf, info->email, branch,\n+\t\t\t       info->message);\n \t\t}\n \n \t\tstrbuf_release(&selector);\ndiff --git a/reflog-walk.h b/reflog-walk.h\nindex 1d86795f84..8951251f8a 100644\n--- a/reflog-walk.h\n+++ b/reflog-walk.h\n@@ -7,13 +7,16 @@ struct date_mode;\n \n void init_reflog_walk(struct reflog_walk_info **info);\n void reflog_walk_info_release(struct reflog_walk_info *info);\n-void reflog_walk_mark(struct reflog_walk_info *info, bool operations);\n+void reflog_walk_mark(struct reflog_walk_info *info, bool operations,\n+\t\t      bool historical_branch);\n int add_reflog_for_walk(struct reflog_walk_info *info,\n \t\t\tstruct commit *commit, const char *name);\n void show_reflog_message(struct reflog_walk_info *info, int,\n-\t\t\t struct date_mode, int force_date);\n+\t\t\t struct date_mode, int force_date,\n+\t\t\t const char *branch);\n void get_reflog_message(struct strbuf *sb,\n \t\t\tstruct reflog_walk_info *reflog_info);\n+const char *get_reflog_branch(struct reflog_walk_info *reflog_info);\n const char *get_reflog_ident(struct reflog_walk_info *reflog_info);\n timestamp_t get_reflog_timestamp(struct reflog_walk_info *reflog_info);\n void get_reflog_selector(struct strbuf *sb,\ndiff --git a/revision.c b/revision.c\nindex 88785651fe..0367ad9b5e 100644\n--- a/revision.c\n+++ b/revision.c\n@@ -2622,6 +2622,8 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg\n \t\trevs->show_signature = 1;\n \t} else if (!strcmp(arg, \"--no-show-signature\")) {\n \t\trevs->show_signature = 0;\n+\t} else if (!strcmp(arg, \"--historical-branch\")) {\n+\t\trevs->historical_branch = 1;\n \t} else if (!strcmp(arg, \"--show-linear-break\")) {\n \t\trevs->break_bar = \"                    ..........\";\n \t\trevs->track_linear = 1;\n@@ -3249,11 +3251,15 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s\n \tif (!revs->reflog_info && revs->grep_filter.use_reflog_filter)\n \t\tdie(_(\"the option '%s' requires '%s'\"), \"--grep-reflog\", \"--walk-reflogs\");\n \n+\tif (!revs->reflog_info && revs->historical_branch)\n+\t\tdie(_(\"the option '%s' requires '%s'\"), \"--historical-branch\", \"--walk-reflogs\");\n+\n \tif (!revs->reflog_info && revs->reflog_operations)\n \t\tdie(_(\"the option '%s' requires '%s'\"), \"--operations\", \"--walk-reflogs\");\n \n \tif (revs->reflog_info)\n-\t\treflog_walk_mark(revs->reflog_info, revs->reflog_operations);\n+\t\treflog_walk_mark(revs->reflog_info, revs->reflog_operations,\n+\t\t\t\t revs->historical_branch);\n \n \tif (revs->line_level_traverse &&\n \t    (revs->full_diff ||\ndiff --git a/revision.h b/revision.h\nindex c4f5e06698..6a0fd120e5 100644\n--- a/revision.h\n+++ b/revision.h\n@@ -269,6 +269,7 @@ struct rev_info {\n \t\t\tshow_notes_given:1,\n \t\t\tshow_notes_by_default:1,\n \t\t\tshow_signature:1,\n+\t\t\thistorical_branch:1,\n \t\t\tpretty_given:1,\n \t\t\tabbrev_commit:1,\n \t\t\tabbrev_commit_given:1,\ndiff --git a/t/t1414-reflog-walk.sh b/t/t1414-reflog-walk.sh\nindex e15f6f2381..6d5272aacf 100755\n--- a/t/t1414-reflog-walk.sh\n+++ b/t/t1414-reflog-walk.sh\n@@ -159,6 +159,52 @@ test_expect_success '--operations shows a rebase as one entry' '\n \t)\n '\n \n+test_expect_success '--historical-branch names the branch of each entry' '\n+\t(\n+\t\tcd operations &&\n+\n+\t\tcat >expect <<-\\EOF &&\n+\t\tHEAD@{0}: (topic) rebase (finish): returning to refs/heads/topic\n+\t\tHEAD@{1}: (topic) rebase (pick): C\n+\t\tHEAD@{2}: (topic) rebase (pick): B\n+\t\tHEAD@{3}: (topic) rebase (pick): A\n+\t\tHEAD@{4}: (topic) rebase (start): checkout main\n+\t\tHEAD@{5}: (topic) checkout: moving from main to topic\n+\t\tHEAD@{6}: (main) commit: upstream\n+\t\tHEAD@{7}: (main) checkout: moving from topic to main\n+\t\tHEAD@{8}: (topic) commit: C\n+\t\tHEAD@{9}: (topic) commit: B\n+\t\tHEAD@{10}: (topic) commit: A\n+\t\tHEAD@{11}: (topic) checkout: moving from main to topic\n+\t\tHEAD@{12}: (main) commit (initial): base\n+\t\tEOF\n+\t\tgit reflog --historical-branch >out &&\n+\t\tcut -d \" \" -f 2- out >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '--historical-branch takes the place of decorations' '\n+\t(\n+\t\tcd operations &&\n+\t\thash=$(git rev-parse --short HEAD) &&\n+\t\techo \"<YELLOW>$hash<RESET> HEAD@{0}: <YELLOW>(<RESET><BOLD;GREEN>topic<RESET><YELLOW>)<RESET> rebase (finish): returning to refs/heads/topic\" >expect &&\n+\t\tgit reflog --historical-branch --decorate --color -1 >out &&\n+\t\ttest_decode_color <out >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '--historical-branch names the ref of other reflogs' '\n+\t(\n+\t\tcd operations &&\n+\t\techo \"main@{0}: (main) commit: upstream\" >expect &&\n+\t\tgit reflog -1 main --historical-branch >out &&\n+\t\tcut -d \" \" -f 2- out >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n test_expect_success '--operations shows a stopped rebase as one entry once it finishes' '\n \tset_fake_editor &&\n \t(\n@@ -231,8 +277,55 @@ test_expect_success '--operations shows a pull --rebase as one entry' '\n \t)\n '\n \n-test_expect_success '--operations requires --walk-reflogs' '\n+test_expect_success '--historical-branch names the current branch when HEAD never switched' '\n+\t(\n+\t\tcd downstream &&\n+\t\techo \"(topic)\" >expect &&\n+\t\tgit reflog --historical-branch >out &&\n+\t\tcut -d \" \" -f 3 out | sort -u >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '--historical-branch follows a renamed branch' '\n+\t(\n+\t\tcd operations &&\n+\t\ttest_commit D &&\n+\t\tgit branch -m topic renamed &&\n+\t\ttest_commit E &&\n+\n+\t\tcat >expect <<-\\EOF &&\n+\t\tHEAD@{0}: (renamed) commit: E\n+\t\tHEAD@{1}: (renamed) Branch: renamed refs/heads/topic to refs/heads/renamed\n+\t\tHEAD@{3}: (topic) commit: D\n+\t\tEOF\n+\t\tgit reflog --historical-branch -3 >out &&\n+\t\tcut -d \" \" -f 2- out >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '--historical-branch shows where HEAD was detached' '\n+\t(\n+\t\tcd operations &&\n+\t\tgit checkout --detach &&\n+\t\ttest_commit F &&\n+\t\tgit checkout renamed &&\n+\n+\t\tcat >expect <<-EOF &&\n+\t\tHEAD@{0}: (renamed) checkout: moving from $(git rev-parse F) to renamed\n+\t\tHEAD@{1}: (detached at $(git rev-parse --short F)) commit: F\n+\t\tEOF\n+\t\tgit reflog --historical-branch -2 >out &&\n+\t\tcut -d \" \" -f 2- out >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '--operations and --historical-branch require --walk-reflogs' '\n \ttest_must_fail git log --operations 2>err &&\n+\ttest_grep \"requires .--walk-reflogs.\" err &&\n+\ttest_must_fail git log --historical-branch 2>err &&\n \ttest_grep \"requires .--walk-reflogs.\" err\n '\n \n-- \ngitgitgadget\n\n"},{"id":"556067","messageId":"CAHwyqnUSfx=NgKD_CepbEU5GZRrTPnMjTCDHcQUMA+huwc0MMQ@mail.gmail.com","threadId":"66545","inReplyTo":"fbbbabee-c586-4305-bdfe-3f7cb46b7191@app.fastmail.com","subject":"Re: [PATCH 0/3] reflog: add --operations and --historical-branch","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-23T15:31:10Z","receivedAt":"2026-10-23T15:31:10Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> This sounds very useful. That you rebase many commits and then the reset\n> points (where each one started and ended) get obscured in the reflog is\n> classic for me. And I think many have had the same issue. This is why I\n> sometimes use `ORIG_HEAD` instead, or maybe make a backup ref.\n\nHi Kristoffer!\n\nWould you have any time to take a look at the latest version of this?\n\n\nHarald\n\n"},{"id":"556068","messageId":"CAHwyqnUGByZs20wbwcz009f2=XLzJB3BiPz7C7DRG8igYgx2iA@mail.gmail.com","threadId":"66545","inReplyTo":"pull.2448.v2.git.git.1792348037.gitgitgadget@gmail.com","subject":"Re: [PATCH v2 0/3] reflog: add --operations and --historical-branch","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-23T15:32:54Z","receivedAt":"2026-10-23T15:32:54Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"Hi Ben!\n\nWould you have some time to take a look at the latest version here?\n\n\nHarald\n\n"},{"id":"556111","messageId":"xmqqmrs43yj2.fsf@gitster.g","threadId":"66545","inReplyTo":"745929bd805784bebeccc7e3ddee638aaffaeded.1792348037.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v2 1/3] reflog: name the strings that mark switches, renames and rebase steps","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-23T21:19:45Z","receivedAt":"2026-10-23T21:19:45Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> Name the strings once in reflog.h and use the names wherever the\n> messages are written or read: the \"checkout: moving from \" prefix\n> written by \"git checkout\", the \"Branch: renamed \" prefix written by\n> \"git branch -m\", and the \"start\", \"finish\" and \"abort\" steps written\n> by \"git rebase\".\n>\n> No change in behavior.\n\nWould it make sense to test the behaviour to demonstrate this claim?\n\n> Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n> ---\n>  builtin/branch.c   |  3 ++-\n>  builtin/checkout.c |  3 ++-\n>  builtin/rebase.c   | 17 +++++++++--------\n>  object-name.c      |  3 ++-\n>  reflog.h           |  6 ++++++\n>  sequencer.c        | 13 +++++++------\n>  wt-status.c        |  3 ++-\n>  7 files changed, 30 insertions(+), 18 deletions(-)\n\n> diff --git a/reflog.h b/reflog.h\n> index 3bdd1ca8a5..32c3737db4 100644\n> --- a/reflog.h\n> +++ b/reflog.h\n> @@ -2,6 +2,12 @@\n>  #define REFLOG_H\n>  #include \"refs.h\"\n>  \n> +#define REFLOG_SWITCH_PREFIX \"checkout: moving from \"\n> +#define REFLOG_RENAME_PREFIX \"Branch: renamed \"\n> +#define REFLOG_STEP_START \"start\"\n> +#define REFLOG_STEP_FINISH \"finish\"\n> +#define REFLOG_STEP_ABORT \"abort\"\n\nI was hoping that we could eventually make reflog messages\ntranslatable, by splitting each entry in the reflog record into\ntwo: a human-readable part (shown in the usual output of the\n'git reflog' command) and a machine-parseable part to be used\nexactly for features like this.  We would hopefully need to update\nonly reflog_message() and its dozens of callers to make it happen.\n\nI wonder how far this change will set us back from that future.\nThe best case would be that this detour does not make it any\nharder.  The locations this patch touches are where we would\nneed to leave clues in the machine-parseable format, and the\nlocations touched by later patches in the series are the places\nthat need to be updated to look at the machine-parseable part.\n\nThe changes in this series looked sensible, though.\n\n"},{"id":"556122","messageId":"CAHwyqnXN+OM0CMCKY8s=V8On-597xtc7NjPsu0i8L=WrAyy2hQ@mail.gmail.com","threadId":"66545","inReplyTo":"xmqqmrs43yj2.fsf@gitster.g","subject":"Re: [PATCH v2 1/3] reflog: name the strings that mark switches, renames and rebase steps","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-23T22:47:12Z","receivedAt":"2026-10-23T22:47:12Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"On Fri, Oct 23, 2026 at 11:19 PM Junio C Hamano <gitster@pobox.com> wrote:\n>\n> \"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n>\n> > Name the strings once in reflog.h and use the names wherever the\n> > messages are written or read: the \"checkout: moving from \" prefix\n> > written by \"git checkout\", the \"Branch: renamed \" prefix written by\n> > \"git branch -m\", and the \"start\", \"finish\" and \"abort\" steps written\n> > by \"git rebase\".\n> >\n> > No change in behavior.\n>\n> Would it make sense to test the behaviour to demonstrate this claim?\n\nWe have previous tests for this which I think would break if this was\nincorrectly refactored.\n\n\nHarald\n\nOn Fri, Oct 23, 2026 at 11:19 PM Junio C Hamano <gitster@pobox.com> wrote:\n>\n> \"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n>\n> > Name the strings once in reflog.h and use the names wherever the\n> > messages are written or read: the \"checkout: moving from \" prefix\n> > written by \"git checkout\", the \"Branch: renamed \" prefix written by\n> > \"git branch -m\", and the \"start\", \"finish\" and \"abort\" steps written\n> > by \"git rebase\".\n> >\n> > No change in behavior.\n>\n> Would it make sense to test the behaviour to demonstrate this claim?\n>\n> > Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n> > ---\n> >  builtin/branch.c   |  3 ++-\n> >  builtin/checkout.c |  3 ++-\n> >  builtin/rebase.c   | 17 +++++++++--------\n> >  object-name.c      |  3 ++-\n> >  reflog.h           |  6 ++++++\n> >  sequencer.c        | 13 +++++++------\n> >  wt-status.c        |  3 ++-\n> >  7 files changed, 30 insertions(+), 18 deletions(-)\n>\n> > diff --git a/reflog.h b/reflog.h\n> > index 3bdd1ca8a5..32c3737db4 100644\n> > --- a/reflog.h\n> > +++ b/reflog.h\n> > @@ -2,6 +2,12 @@\n> >  #define REFLOG_H\n> >  #include \"refs.h\"\n> >\n> > +#define REFLOG_SWITCH_PREFIX \"checkout: moving from \"\n> > +#define REFLOG_RENAME_PREFIX \"Branch: renamed \"\n> > +#define REFLOG_STEP_START \"start\"\n> > +#define REFLOG_STEP_FINISH \"finish\"\n> > +#define REFLOG_STEP_ABORT \"abort\"\n>\n> I was hoping that we could eventually make reflog messages\n> translatable, by splitting each entry in the reflog record into\n> two: a human-readable part (shown in the usual output of the\n> 'git reflog' command) and a machine-parseable part to be used\n> exactly for features like this.  We would hopefully need to update\n> only reflog_message() and its dozens of callers to make it happen.\n>\n> I wonder how far this change will set us back from that future.\n> The best case would be that this detour does not make it any\n> harder.  The locations this patch touches are where we would\n> need to leave clues in the machine-parseable format, and the\n> locations touched by later patches in the series are the places\n> that need to be updated to look at the machine-parseable part.\n>\n> The changes in this series looked sensible, though.\n\n"},{"id":"556124","messageId":"xmqqpkx02fim.fsf@gitster.g","threadId":"66545","inReplyTo":"CAHwyqnXN+OM0CMCKY8s=V8On-597xtc7NjPsu0i8L=WrAyy2hQ@mail.gmail.com","subject":"Re: [PATCH v2 1/3] reflog: name the strings that mark switches, renames and rebase steps","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-23T22:55:45Z","receivedAt":"2026-10-23T22:55:45Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Harald Nordgren <haraldnordgren@gmail.com> writes:\n\n> On Fri, Oct 23, 2026 at 11:19 PM Junio C Hamano <gitster@pobox.com> wrote:\n>>\n>> \"Harald Nordgren via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n>>\n>> > Name the strings once in reflog.h and use the names wherever the\n>> > messages are written or read: the \"checkout: moving from \" prefix\n>> > written by \"git checkout\", the \"Branch: renamed \" prefix written by\n>> > \"git branch -m\", and the \"start\", \"finish\" and \"abort\" steps written\n>> > by \"git rebase\".\n>> >\n>> > No change in behavior.\n>>\n>> Would it make sense to test the behaviour to demonstrate this claim?\n>\n> We have previous tests for this which I think would break if this was\n> incorrectly refactored.\n\nOK, if we have sufficient coverage, that would be fine.\n\n\n"},{"id":"556156","messageId":"bfc2b43a-6198-47b0-8868-d92551548f22@gmail.com","threadId":"66545","inReplyTo":"pull.2448.v2.git.git.1792348037.gitgitgadget@gmail.com","subject":"Re: [PATCH v2 0/3] reflog: add --operations and --historical-branch","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-24T13:51:13Z","receivedAt":"2026-10-24T13:51:13Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Harald\n\nOn 18/10/2026 19:27, Harald Nordgren via GitGitGadget wrote:\n> Create a better reflog. Follows the discussion in\n> https://lore.kernel.org/git/akZfm-igZKeHaDST@pks.im/.\n> > \"git reflog\" lists every step of a rebase and every branch switch, which\n> makes it hard to see what was done and on which branch. This adds two\n> options to \"git reflog\" and \"git log -g\":\n> >   * --operations shows one entry per command, folding a rebase into the entry\n>     that finished or aborted it, and leaves out entries that change no\n>     branch.\n\nI'm rather skeptical of this in its current form. Not all commands that modify the working tree update the reflog and some commands such as \"git cherry-pick A B C\" create multiple reflog entires that are indistinguishable from multiple invocations of a single command. The message you cite above is arguing that we need a separate operations log if we want to overcome these problems. Most of what this option does can be achieved by looking at the branch reflog instead (I really wish that \"git reflog\" defaulted to the current branch rather than HEAD). I'm not opposed to supporting some kind of filtering/simplification particularly for HEAD, but we should not pretend we're showing users one entry per command.\n\n>   * --historical-branch decorates each entry with the branch HEAD was on when\n>     it was recorded.\n\nI quite like this idea, but think it would be nicer implemented as a format specifier (\"%gb\" seems to be free) so that users can use it in their custom formats. I'd be quite happy to change the default format do\nfor HEAD to include the branch name after the reflog message but I'm not sure others would be so keen (perhaps we could get away with guarding any change by isatty()/GIT_PAGER_IN_USE?).\n\nIf we're going to do this we should make sure it supports \"git rebase <upstream> <branch>\" so that the branch names are accurate. That is a bit involved because you have to compare the name of the branch being rebased (which is in the \"(finish)\" reflog message) to the name of the branch that was checked out before (which isn't in that message so you have to go looking for a previous rebase or checkout). It also needs to take account of rebases that checkout a detached HEAD which don't have the \"(finish)\" message and those that checkout an up-to-date branch or detached HEAD where the message is \"$GIT_REFLOG_ACTION: checkout $revision\". I've got a patch [1] that fixes this for \"@{-N}\" which I think works, but it needs some more tests.\n\nThanks\n\nPhillip\n\n[1] https://github.com/git/git/compare/master...phillipwood:git:fix-nth-prior-checkout\n\n> Changes in v2:\n> >   * Move the \"historical branch\" rightward in the display for cleaner\n>     alignment.\n>   * Improve commit messages.\n> > Harald Nordgren (3):\n>    reflog: name the strings that mark switches, renames and rebase steps\n>    reflog: add --operations to show one entry per command\n>    reflog: add --historical-branch to name the branch of each entry\n> >   Documentation/git-reflog.adoc       |   4 +\n>   Documentation/rev-list-options.adoc |  21 +++\n>   builtin/branch.c                    |   3 +-\n>   builtin/checkout.c                  |   3 +-\n>   builtin/rebase.c                    |  17 +--\n>   log-tree.c                          |  28 +++-\n>   object-name.c                       |   3 +-\n>   reflog-walk.c                       | 183 +++++++++++++++++++++++--\n>   reflog-walk.h                       |   6 +-\n>   reflog.h                            |   6 +\n>   revision.c                          |  14 ++\n>   revision.h                          |   2 +\n>   sequencer.c                         |  13 +-\n>   t/t1414-reflog-walk.sh              | 198 ++++++++++++++++++++++++++++\n>   wt-status.c                         |   3 +-\n>   15 files changed, 471 insertions(+), 33 deletions(-)\n> > > base-commit: c51408889c3b372814bc20b9431c3b74d0d268eb\n> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2448%2FHaraldNordgren%2Freflog-operations-v2\n> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2448/HaraldNordgren/reflog-operations-v2\n> Pull-Request: https://github.com/git/git/pull/2448\n> > Range-diff vs v1:\n> >   1:  d95ed5bd67 ! 1:  745929bd80 reflog: name the strings that mark switches, renames and rebase steps\n>       @@ Metadata\n>         ## Commit message ##\n>            reflog: name the strings that mark switches, renames and rebase steps\n>        >       -    \"git checkout\" records a move of HEAD in the reflog as \"checkout:\n>       -    moving from <old> to <new>\", \"git branch -m\" records a rename of the\n>       -    current branch as \"Branch: renamed <old> to <new>\", and \"git rebase\"\n>       -    marks where it starts and ends with steps like \"rebase (start)\" and\n>       -    \"rebase (finish)\". \"@{-<n>}\" and \"git status\" read the switch message\n>       -    back, each with its own copy of the prefix, and a later commit adds a\n>       -    reader for all of them.\n>       +    The next commit adds \"git reflog --operations\", which finds branch\n>       +    switches, branch renames and the start and end of a rebase by the\n>       +    messages they leave in the reflog. The commands that write these\n>       +    messages spell out the strings by hand, and \"@{-<n>}\" and \"git status\"\n>       +    each keep their own copy of the switch prefix to read it back. Reading\n>       +    them in one more place would add yet another copy that has to stay in\n>       +    sync.\n>        >       -    Name these strings in reflog.h and use them where the messages are\n>       -    written and read, so they cannot drift apart.\n>       +    Name the strings once in reflog.h and use the names wherever the\n>       +    messages are written or read: the \"checkout: moving from \" prefix\n>       +    written by \"git checkout\", the \"Branch: renamed \" prefix written by\n>       +    \"git branch -m\", and the \"start\", \"finish\" and \"abort\" steps written\n>       +    by \"git rebase\".\n>       +\n>       +    No change in behavior.\n>        >            Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n>        >   2:  579e13b36a = 2:  1c47ba225e reflog: add --operations to show one entry per command\n>   3:  4efe74b04e ! 3:  aa5a98b80d reflog: add --historical-branch to name the branch of each entry\n>       @@ Commit message\n>            still point at that commit, and a rebased or amended commit has none.\n>        >            Teach \"git reflog\" and \"git log -g\" a \"--historical-branch\" option\n>       -    that decorates each entry with the branch it was made on, in place of\n>       -    the refs that point at the commit now. The name is colored like a\n>       -    branch decoration:\n>       +    that shows the branch each entry was made on before its message, in\n>       +    place of the refs that point at the commit now. The hashes and\n>       +    selectors stay lined up in their columns, and the name is colored like\n>       +    a branch decoration:\n>        >              $ git reflog --historical-branch\n>       -      4e5f6a7 (topic) HEAD@{0}: commit: add tests\n>       -      1a2b3c4 (topic) HEAD@{1}: checkout: moving from main to topic\n>       -      8c9d0e1 (main) HEAD@{2}: commit: fix typo\n>       +      4e5f6a7 HEAD@{0}: (topic) commit: add tests\n>       +      1a2b3c4 HEAD@{1}: (topic) checkout: moving from main to topic\n>       +      8c9d0e1 HEAD@{2}: (main) commit: fix typo\n>        >            Every entry gets a name, including ones whose commits are no longer on\n>            any branch. Entries from before a branch was renamed show its old name,\n>       @@ Documentation/git-reflog.adoc: actions, and in addition the `HEAD` reflog record\n>         branch are left out, and a rebase is shown as a single entry instead\n>        -of one entry for each commit it replayed.\n>        +of one entry for each commit it replayed. With `--historical-branch`,\n>       -+each entry is decorated with the branch it was made on.\n>       ++each entry shows the branch it was made on before its message.\n>         >         The \"list\" subcommand lists all refs which have a corresponding reflog.\n>         >       @@ Documentation/rev-list-options.adoc: Under `--pretty=reference`, this informatio\n>         \trebase started. It is an error to use this option unless\n>         \t`--walk-reflogs` is in use.\n>         >       ++ifndef::git-rev-list[]\n>        +`--historical-branch`::\n>       -+\tDecorate each reflog entry with the branch `HEAD` was on when\n>       -+\tthe entry was recorded, such as `(topic)`, in place of the refs\n>       -+\tthat point at the commit now. When `HEAD` was detached, the\n>       -+\tentry shows `(detached at <commit>)`. For the reflog of any\n>       -+\tother ref, the name of that ref is shown. It is an error to use\n>       -+\tthis option unless `--walk-reflogs` is in use.\n>       ++\tShow the branch `HEAD` was on when each reflog entry was\n>       ++\trecorded, such as `(topic)`, before the reflog message, in\n>       ++\tplace of the refs that point at the commit now. When `HEAD` was\n>       ++\tdetached, the entry shows `(detached at <commit>)`. For the\n>       ++\treflog of any other ref, the name of that ref is shown. It is\n>       ++\tan error to use this option unless `--walk-reflogs` is in use.\n>       ++endif::git-rev-list[]\n>        +\n>         `--merge`::\n>         \tShow commits touching conflicted paths in the range `HEAD...<other>`,\n>       @@ log-tree.c: void format_decorations(struct strbuf *sb,\n>         \t}\n>         }\n>         >       -+static void show_historical_branch(struct rev_info *opt)\n>       ++static void format_historical_branch(struct strbuf *sb,\n>       ++\t\t\t\t     struct rev_info *opt)\n>        +{\n>        +\tconst char *branch = get_reflog_branch(opt->reflog_info);\n>        +\tenum git_colorbool use_color = opt->diffopt.use_color;\n>       @@ log-tree.c: void format_decorations(struct strbuf *sb,\n>        +\n>        +\tcolor_commit = diff_get_color(use_color, DIFF_COMMIT);\n>        +\tcolor_reset = decorate_get_color(use_color, DECORATION_NONE);\n>       -+\tfprintf(opt->diffopt.file, \"%s (%s%s%s%s%s)%s\",\n>       -+\t\tcolor_commit, color_reset,\n>       -+\t\tdecorate_get_color(use_color, DECORATION_REF_LOCAL),\n>       -+\t\tbranch, color_reset, color_commit, color_reset);\n>       ++\tstrbuf_addf(sb, \"%s(%s%s%s%s%s)%s \",\n>       ++\t\t    color_commit, color_reset,\n>       ++\t\t    decorate_get_color(use_color, DECORATION_REF_LOCAL),\n>       ++\t\t    branch, color_reset, color_commit, color_reset);\n>        +}\n>        +\n>         void show_decorations(struct rev_info *opt, struct commit *commit)\n>       @@ log-tree.c: void show_decorations(struct rev_info *opt, struct commit *commit)\n>         \t\tif (slot && *slot)\n>         \t\t\tfprintf(opt->diffopt.file, \"\\t%s\", *slot);\n>         \t}\n>       -+\tif (opt->historical_branch) {\n>       -+\t\tshow_historical_branch(opt);\n>       -+\t\treturn;\n>       -+\t}\n>       - \tif (!opt->show_decorations)\n>       +-\tif (!opt->show_decorations)\n>       ++\tif (!opt->show_decorations || opt->historical_branch)\n>         \t\treturn;\n>         \tformat_decorations(&sb, commit, opt->diffopt.use_color, NULL);\n>       + \tfputs(sb.buf, opt->diffopt.file);\n>       +@@ log-tree.c: void show_log(struct rev_info *opt)\n>       + \t\t\tgraph_show_oneline(opt->graph);\n>       + \t\t}\n>       + \t\tif (opt->reflog_info) {\n>       ++\t\t\tstruct strbuf branch = STRBUF_INIT;\n>       ++\n>       + \t\t\t/*\n>       + \t\t\t * setup_revisions() ensures that opt->reflog_info\n>       + \t\t\t * and opt->graph cannot both be set,\n>       + \t\t\t * so we don't need to worry about printing the\n>       + \t\t\t * graph info here.\n>       + \t\t\t */\n>       ++\t\t\tif (opt->historical_branch)\n>       ++\t\t\t\tformat_historical_branch(&branch, opt);\n>       + \t\t\tshow_reflog_message(opt->reflog_info,\n>       + \t\t\t\t\t    opt->commit_format == CMIT_FMT_ONELINE,\n>       + \t\t\t\t\t    opt->date_mode,\n>       +-\t\t\t\t\t    opt->date_mode_explicit);\n>       ++\t\t\t\t\t    opt->date_mode_explicit,\n>       ++\t\t\t\t\t    branch.buf);\n>       ++\t\t\tstrbuf_release(&branch);\n>       + \t\t\tif (opt->commit_format == CMIT_FMT_ONELINE)\n>       + \t\t\t\treturn;\n>       + \t\t}\n>        >         ## reflog-walk.c ##\n>        @@\n>       @@ reflog-walk.c: void get_reflog_message(struct strbuf *sb,\n>         const char *get_reflog_ident(struct reflog_walk_info *reflog_info)\n>         {\n>         \tstruct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;\n>       +@@ reflog-walk.c: timestamp_t get_reflog_timestamp(struct reflog_walk_info *reflog_info)\n>       + }\n>       +\n>       + void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,\n>       +-\t\t\t struct date_mode dmode, int force_date)\n>       ++\t\t\t struct date_mode dmode, int force_date,\n>       ++\t\t\t const char *branch)\n>       + {\n>       + \tif (reflog_info && reflog_info->last_commit_reflog) {\n>       + \t\tstruct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;\n>       +@@ reflog-walk.c: void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,\n>       + \t\tinfo = &commit_reflog->reflogs->items[commit_reflog->recno+1];\n>       + \t\tget_reflog_selector(&selector, reflog_info, dmode, force_date, 0);\n>       + \t\tif (oneline) {\n>       +-\t\t\tprintf(\"%s: %s\", selector.buf, info->message);\n>       ++\t\t\tprintf(\"%s: %s%s\", selector.buf, branch,\n>       ++\t\t\t       info->message);\n>       + \t\t}\n>       + \t\telse {\n>       +-\t\t\tprintf(\"Reflog: %s (%s)\\nReflog message: %s\",\n>       +-\t\t\t       selector.buf, info->email, info->message);\n>       ++\t\t\tprintf(\"Reflog: %s (%s)\\nReflog message: %s%s\",\n>       ++\t\t\t       selector.buf, info->email, branch,\n>       ++\t\t\t       info->message);\n>       + \t\t}\n>       +\n>       + \t\tstrbuf_release(&selector);\n>        >         ## reflog-walk.h ##\n>        @@ reflog-walk.h: struct date_mode;\n>       @@ reflog-walk.h: struct date_mode;\n>         int add_reflog_for_walk(struct reflog_walk_info *info,\n>         \t\t\tstruct commit *commit, const char *name);\n>         void show_reflog_message(struct reflog_walk_info *info, int,\n>       - \t\t\t struct date_mode, int force_date);\n>       +-\t\t\t struct date_mode, int force_date);\n>       ++\t\t\t struct date_mode, int force_date,\n>       ++\t\t\t const char *branch);\n>         void get_reflog_message(struct strbuf *sb,\n>         \t\t\tstruct reflog_walk_info *reflog_info);\n>        +const char *get_reflog_branch(struct reflog_walk_info *reflog_info);\n>       @@ t/t1414-reflog-walk.sh: test_expect_success '--operations shows a rebase as one\n>        +\t\tcd operations &&\n>        +\n>        +\t\tcat >expect <<-\\EOF &&\n>       -+\t\t(topic) HEAD@{0}: rebase (finish): returning to refs/heads/topic\n>       -+\t\t(topic) HEAD@{1}: rebase (pick): C\n>       -+\t\t(topic) HEAD@{2}: rebase (pick): B\n>       -+\t\t(topic) HEAD@{3}: rebase (pick): A\n>       -+\t\t(topic) HEAD@{4}: rebase (start): checkout main\n>       -+\t\t(topic) HEAD@{5}: checkout: moving from main to topic\n>       -+\t\t(main) HEAD@{6}: commit: upstream\n>       -+\t\t(main) HEAD@{7}: checkout: moving from topic to main\n>       -+\t\t(topic) HEAD@{8}: commit: C\n>       -+\t\t(topic) HEAD@{9}: commit: B\n>       -+\t\t(topic) HEAD@{10}: commit: A\n>       -+\t\t(topic) HEAD@{11}: checkout: moving from main to topic\n>       -+\t\t(main) HEAD@{12}: commit (initial): base\n>       ++\t\tHEAD@{0}: (topic) rebase (finish): returning to refs/heads/topic\n>       ++\t\tHEAD@{1}: (topic) rebase (pick): C\n>       ++\t\tHEAD@{2}: (topic) rebase (pick): B\n>       ++\t\tHEAD@{3}: (topic) rebase (pick): A\n>       ++\t\tHEAD@{4}: (topic) rebase (start): checkout main\n>       ++\t\tHEAD@{5}: (topic) checkout: moving from main to topic\n>       ++\t\tHEAD@{6}: (main) commit: upstream\n>       ++\t\tHEAD@{7}: (main) checkout: moving from topic to main\n>       ++\t\tHEAD@{8}: (topic) commit: C\n>       ++\t\tHEAD@{9}: (topic) commit: B\n>       ++\t\tHEAD@{10}: (topic) commit: A\n>       ++\t\tHEAD@{11}: (topic) checkout: moving from main to topic\n>       ++\t\tHEAD@{12}: (main) commit (initial): base\n>        +\t\tEOF\n>        +\t\tgit reflog --historical-branch >out &&\n>        +\t\tcut -d \" \" -f 2- out >actual &&\n>       @@ t/t1414-reflog-walk.sh: test_expect_success '--operations shows a rebase as one\n>        +\t(\n>        +\t\tcd operations &&\n>        +\t\thash=$(git rev-parse --short HEAD) &&\n>       -+\t\techo \"<YELLOW>$hash<RESET><YELLOW> (<RESET><BOLD;GREEN>topic<RESET><YELLOW>)<RESET>\" >expect &&\n>       ++\t\techo \"<YELLOW>$hash<RESET> HEAD@{0}: <YELLOW>(<RESET><BOLD;GREEN>topic<RESET><YELLOW>)<RESET> rebase (finish): returning to refs/heads/topic\" >expect &&\n>        +\t\tgit reflog --historical-branch --decorate --color -1 >out &&\n>       -+\t\ttest_decode_color <out | sed \"s/ HEAD@.*//\" >actual &&\n>       ++\t\ttest_decode_color <out >actual &&\n>        +\t\ttest_cmp expect actual\n>        +\t)\n>        +'\n>       @@ t/t1414-reflog-walk.sh: test_expect_success '--operations shows a rebase as one\n>        +test_expect_success '--historical-branch names the ref of other reflogs' '\n>        +\t(\n>        +\t\tcd operations &&\n>       -+\t\techo \"(main) main@{0}: commit: upstream\" >expect &&\n>       ++\t\techo \"main@{0}: (main) commit: upstream\" >expect &&\n>        +\t\tgit reflog -1 main --historical-branch >out &&\n>        +\t\tcut -d \" \" -f 2- out >actual &&\n>        +\t\ttest_cmp expect actual\n>       @@ t/t1414-reflog-walk.sh: test_expect_success '--operations shows a pull --rebase\n>        +\t\tcd downstream &&\n>        +\t\techo \"(topic)\" >expect &&\n>        +\t\tgit reflog --historical-branch >out &&\n>       -+\t\tcut -d \" \" -f 2 out | sort -u >actual &&\n>       ++\t\tcut -d \" \" -f 3 out | sort -u >actual &&\n>        +\t\ttest_cmp expect actual\n>        +\t)\n>        +'\n>       @@ t/t1414-reflog-walk.sh: test_expect_success '--operations shows a pull --rebase\n>        +\t\ttest_commit E &&\n>        +\n>        +\t\tcat >expect <<-\\EOF &&\n>       -+\t\t(renamed) HEAD@{0}: commit: E\n>       -+\t\t(renamed) HEAD@{1}: Branch: renamed refs/heads/topic to refs/heads/renamed\n>       -+\t\t(topic) HEAD@{3}: commit: D\n>       ++\t\tHEAD@{0}: (renamed) commit: E\n>       ++\t\tHEAD@{1}: (renamed) Branch: renamed refs/heads/topic to refs/heads/renamed\n>       ++\t\tHEAD@{3}: (topic) commit: D\n>        +\t\tEOF\n>        +\t\tgit reflog --historical-branch -3 >out &&\n>        +\t\tcut -d \" \" -f 2- out >actual &&\n>       @@ t/t1414-reflog-walk.sh: test_expect_success '--operations shows a pull --rebase\n>        +\t\tgit checkout renamed &&\n>        +\n>        +\t\tcat >expect <<-EOF &&\n>       -+\t\t(renamed) HEAD@{0}: checkout: moving from $(git rev-parse F) to renamed\n>       -+\t\t(detached at $(git rev-parse --short F)) HEAD@{1}: commit: F\n>       ++\t\tHEAD@{0}: (renamed) checkout: moving from $(git rev-parse F) to renamed\n>       ++\t\tHEAD@{1}: (detached at $(git rev-parse --short F)) commit: F\n>        +\t\tEOF\n>        +\t\tgit reflog --historical-branch -2 >out &&\n>        +\t\tcut -d \" \" -f 2- out >actual &&\n> "},{"id":"556167","messageId":"CALnO6CCtm2PLOfnNF8p2aGU5Pn71qimdC4aAS130O5PrzQb_oQ@mail.gmail.com","threadId":"66545","inReplyTo":"bfc2b43a-6198-47b0-8868-d92551548f22@gmail.com","subject":"Re: [PATCH v2 0/3] reflog: add --operations and --historical-branch","fromName":"D. Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-10-24T15:22:28Z","receivedAt":"2026-10-24T15:22:28Z","isPatch":true,"sender":{"key":"ben.knoble@gmail.com","avatar":"https://avatars.githubusercontent.com/u/22802209?v=4"},"body":"On Sat, Oct 24, 2026 at 9:51 AM Phillip Wood <phillip.wood123@gmail.com> wrote:\n>\n> Hi Harald\n>\n> On 18/10/2026 19:27, Harald Nordgren via GitGitGadget wrote:\n> > Create a better reflog. Follows the discussion in\n> > https://lore.kernel.org/git/akZfm-igZKeHaDST@pks.im/.\n> >\n> > \"git reflog\" lists every step of a rebase and every branch switch, which\n> > makes it hard to see what was done and on which branch. This adds two\n> > options to \"git reflog\" and \"git log -g\":\n> >\n> >   * --operations shows one entry per command, folding a rebase into the entry\n> >     that finished or aborted it, and leaves out entries that change no\n> >     branch.\n>\n> I'm rather skeptical of this in its current form. Not all commands that\n> modify the working tree update the reflog and some commands such as \"git\n> cherry-pick A B C\" create multiple reflog entires that are\n> indistinguishable from multiple invocations of a single command.\n\nGreat point. I thought it was very neat that we could make progress\ntowards this simply by parsing out existing reflogs, but it's always\ngoing to be a bit heuristic.\n\n> The\n> message you cite above is arguing that we need a separate operations log\n> if we want to overcome these problems.\n\nI also thought this series would be doing something like a true ops\nlog, which would be very cool.\n\n> Most of what this option does can\n> be achieved by looking at the branch reflog instead (I really wish that\n> \"git reflog\" defaulted to the current branch rather than HEAD). I'm not\n> opposed to supporting some kind of filtering/simplification particularly\n> for HEAD, but we should not pretend we're showing users one entry per\n> command.\n\nDitto\n\n> >   * --historical-branch decorates each entry with the branch HEAD was on when\n> >     it was recorded.\n>\n> I quite like this idea, but think it would be nicer implemented as a\n> format specifier (\"%gb\" seems to be free) so that users can use it in\n> their custom formats. I'd be quite happy to change the default format do\n> for HEAD to include the branch name after the reflog message but I'm not\n> sure others would be so keen (perhaps we could get away with guarding\n> any change by isatty()/GIT_PAGER_IN_USE?).\n\nI'm quite a bit more positive on this change, but I like the\ncomposability of a format-specifier. Great idea.\n\n> If we're going to do this we should make sure it supports \"git rebase\n> <upstream> <branch>\" so that the branch names are accurate. That is a\n> bit involved because you have to compare the name of the branch being\n> rebased (which is in the \"(finish)\" reflog message) to the name of the\n> branch that was checked out before (which isn't in that message so you\n> have to go looking for a previous rebase or checkout). It also needs to\n> take account of rebases that checkout a detached HEAD which don't have\n> the \"(finish)\" message and those that checkout an up-to-date branch or\n> detached HEAD where the message is \"$GIT_REFLOG_ACTION: checkout\n> $revision\". I've got a patch [1] that fixes this for \"@{-N}\" which I\n> think works, but it needs some more tests.\n\nOuch. This definitely points to my \"heuristic\" above, and while we\ncould maybe invest time in the reflog + parsing to make this work, it\nseems somehow simpler to have an event or ops log with more structure\nfor this new purpose.\n\n--\nD. Ben Knoble\n\n"},{"id":"556173","messageId":"xmqq5wyr128q.fsf@gitster.g","threadId":"66545","inReplyTo":"bfc2b43a-6198-47b0-8868-d92551548f22@gmail.com","subject":"Re: [PATCH v2 0/3] reflog: add --operations and --historical-branch","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-24T16:40:05Z","receivedAt":"2026-10-24T16:40:05Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Phillip Wood <phillip.wood123@gmail.com> writes:\n\n> I'm rather skeptical of this in its current form. Not all commands that \n> modify the working tree update the reflog and some commands such as \"git \n> cherry-pick A B C\" create multiple reflog entires that are \n> indistinguishable from multiple invocations of a single command.\n\nI like the direction to make the default 'git reflog' output on a\nsingle branch easier to understand without needing any extra\nfiltering in this series.\n\n'git rebase' doing its work on a detached HEAD was likely the\nfirst, and we may want to convert more multi-step commands to do\nso, including the multiple 'git cherry-pick' you mention.  'git am'\nway predates the rebase that works on a detached HEAD ('git am'\nintroduced in Oct 2005 predates the concept of detached HEAD that\nwas introduced in Git 1.5.0 released in Feb 2007), and it does its\nwork without detaching HEAD.  Its users grew to expect it to work\nthat way, but I am not sure how problematic a regression it would\nbe if we taught it to work more like 'git rebase'.  For example,\nbecause I am used to 'git am' working on the current branch, I\noften do\n\n    $ git checkout --detach master\n    $ git am -s ...mailbox...\n    $ git checkout -b pw/topic-by-phillip\n\nto compensate for the lack of it, in order to keep the events\nrecorded in the reflog of the pw/topic-by-phillip branch to \"one\niteration per entry\".  This helps me compare iterations more easily,\ne.g.,\n\n    $ git checkout pw/topic-by-phillip\n    $ git diff @{1}\n    $ git range-diff @{1}...\n\nBut the workflow to actively detach by the end user would work just\nfine if 'git am' learns to detach first when started on a concrete\nbranch, to work more like 'git rebase'.\n\nBy the way, I too was annoyed that 'git reflog' defaults to the log\nof HEAD a long time ago, but the irritation soon waned once I made\nit a habit to always provide @{now} or @{0} to the command, as that\nnotation refers to the current branch.\n\nThanks.\n\n"},{"id":"556185","messageId":"CAHwyqnVJ618QXq99M=XNm19eEObQC-DTLcpeYOiizzSu_YNEpw@mail.gmail.com","threadId":"66545","inReplyTo":"xmqq5wyr128q.fsf@gitster.g","subject":"Re: [PATCH v2 0/3] reflog: add --operations and --historical-branch","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-24T18:46:28Z","receivedAt":"2026-10-24T18:46:28Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"On Sat, Oct 24, 2026 at 6:40 PM Junio C Hamano <gitster@pobox.com> wrote:\n>\n> Phillip Wood <phillip.wood123@gmail.com> writes:\n>\n> > I'm rather skeptical of this in its current form. Not all commands that\n> > modify the working tree update the reflog and some commands such as \"git\n> > cherry-pick A B C\" create multiple reflog entires that are\n> > indistinguishable from multiple invocations of a single command.\n>\n> I like the direction to make the default 'git reflog' output on a\n> single branch easier to understand without needing any extra\n> filtering in this series.\n>\n> 'git rebase' doing its work on a detached HEAD was likely the\n> first, and we may want to convert more multi-step commands to do\n> so, including the multiple 'git cherry-pick' you mention.  'git am'\n> way predates the rebase that works on a detached HEAD ('git am'\n> introduced in Oct 2005 predates the concept of detached HEAD that\n> was introduced in Git 1.5.0 released in Feb 2007), and it does its\n> work without detaching HEAD.  Its users grew to expect it to work\n> that way, but I am not sure how problematic a regression it would\n> be if we taught it to work more like 'git rebase'.  For example,\n> because I am used to 'git am' working on the current branch, I\n> often do\n>\n>     $ git checkout --detach master\n>     $ git am -s ...mailbox...\n>     $ git checkout -b pw/topic-by-phillip\n>\n> to compensate for the lack of it, in order to keep the events\n> recorded in the reflog of the pw/topic-by-phillip branch to \"one\n> iteration per entry\".  This helps me compare iterations more easily,\n> e.g.,\n>\n>     $ git checkout pw/topic-by-phillip\n>     $ git diff @{1}\n>     $ git range-diff @{1}...\n>\n> But the workflow to actively detach by the end user would work just\n> fine if 'git am' learns to detach first when started on a concrete\n> branch, to work more like 'git rebase'.\n>\n> By the way, I too was annoyed that 'git reflog' defaults to the log\n> of HEAD a long time ago, but the irritation soon waned once I made\n> it a habit to always provide @{now} or @{0} to the command, as that\n> notation refers to the current branch.\n\nThanks Phillip, Ben and Junio for pointing this out!\n\nI didn't realize that reflog could be changed to operate only on one\nbranch. And it makes it a lot better! Seems like it should be the\ndefault, and I don't think a lot of people would complain, I'll\ncertainly never run the old reflog again.\n\nHowever, the format is quite heavy with the full name references,\nseems like they could be cleaned up:\n\n```\n0b8dc70369 (HEAD -> fetch-shallow-narrow-refspec,\norigin/fetch-shallow-narrow-refspec)\nrefs/heads/fetch-shallow-narrow-refspec@{0}: >\ne4882d2794 refs/heads/fetch-shallow-narrow-refspec@{1}: rebase\n(finish): refs/heads/fetch-shallow-narrow-refspec onto 7e34d8174dcda3>\n4b0cb31dd7 refs/heads/fetch-shallow-narrow-refspec@{2}: rebase\n(finish): refs/heads/fetch-shallow-narrow-refspec onto 7e34d8174dcda3>\n11d2326038 refs/heads/fetch-shallow-narrow-refspec@{3}: rebase\n(finish): refs/heads/fetch-shallow-narrow-refspec onto 4603c84d688c32>\n83ce62663e refs/heads/fetch-shallow-narrow-refspec@{4}: rebase\n(finish): refs/heads/fetch-shallow-narrow-refspec onto 7e34d8174dcda3>\nd633b1fc1b refs/heads/fetch-shallow-narrow-refspec@{5}: rebase\n(finish): refs/heads/fetch-shallow-narrow-refspec onto 7e34d8174dcda3>\n9ca7fe6077 refs/heads/fetch-shallow-narrow-refspec@{6}: pull (finish):\nrefs/heads/fetch-shallow-narrow-refspec onto 7e34d8174dcda343>\n552bf08971 (tag:\npr-git-2412/HaraldNordgren/fetch-shallow-narrow-refspec-v10)\nrefs/heads/fetch-shallow-narrow-refspec@{7}: rebase (f>\n9ee090d3d0 (tag:\npr-git-2412/HaraldNordgren/fetch-shallow-narrow-refspec-v9)\nrefs/heads/fetch-shallow-narrow-refspec@{8}: rebase (fi>\ndb27c29086 (tag:\npr-git-2412/HaraldNordgren/fetch-shallow-narrow-refspec-v8)\nrefs/heads/fetch-shallow-narrow-refspec@{9}: rebase (fi>\n10c6be7ce9 refs/heads/fetch-shallow-narrow-refspec@{10}: rebase\n(finish): refs/heads/fetch-shallow-narrow-refspec onto 6de20f6092dcf>\nacb47efdc1 refs/heads/fetch-shallow-narrow-refspec@{11}: rebase\n(finish): refs/heads/fetch-shallow-narrow-refspec onto b5db64d56fa32>\n9fe17a1968 refs/heads/fetch-shallow-narrow-refspec@{12}: rebase\n(finish): refs/heads/fetch-shallow-narrow-refspec onto b5db64d56fa32>\n591e69aa88 refs/heads/fetch-shallow-narrow-refspec@{13}: rebase\n(finish): refs/heads/fetch-shallow-narrow-refspec onto b5db64d56fa32>\n16c467682b refs/heads/fetch-shallow-narrow-refspec@{14}: rebase\n(finish): refs/heads/fetch-shallow-narrow-refspec onto b5db64d56fa32>\nd5107dcb5d refs/heads/fetch-shallow-narrow-refspec@{15}: rebase\n(finish): refs/heads/fetch-shallow-narrow-refspec onto 6de20f6092dcf>\nc8fd073de3 (tag:\npr-git-2412/HaraldNordgren/fetch-shallow-narrow-refspec-v7)\nrefs/heads/fetch-shallow-narrow-refspec@{16}: rebase (f>\n632ad9e6eb refs/heads/fetch-shallow-narrow-refspec@{17}: rebase\n(finish): refs/heads/fetch-shallow-narrow-refspec onto 6de20f6092dcf>\ncbeb1bf547 refs/heads/fetch-shallow-narrow-refspec@{18}: pull\n(finish): refs/heads/fetch-shallow-narrow-refspec onto\n6de20f6092dcf9b>\n005e30ee30 refs/heads/fetch-shallow-narrow-refspec@{19}: rebase\n(finish): refs/heads/fetch-shallow-narrow-refspec onto 8103b446517e0>\n2609473369 refs/heads/fetch-shallow-narrow-refspec@{20}: rebase\n(finish): refs/heads/fetch-shallow-narrow-refspec onto 8103b446517e0>\nd986bb7abe refs/heads/fetch-shallow-narrow-refspec@{21}: rebase\n(finish): refs/heads/fetch-shallow-narrow-refspec onto 7303c95b3b667>\n01d4bba698 refs/heads/fetch-shallow-narrow-refspec@{22}: rebase\n(finish): refs/heads/fetch-shallow-narrow-refspec onto 8103b446517e0>\n844ca50185 refs/heads/fetch-shallow-narrow-refspec@{23}: rebase\n(finish): refs/heads/fetch-shallow-narrow-refspec onto 2e37944e2883f>\n864fc79e86 refs/heads/fetch-shallow-narrow-refspec@{24}: rebase\n(finish): refs/heads/fetch-shallow-narrow-refspec onto 2e37944e2883f>\n8212be8acc refs/heads/fetch-shallow-narrow-refspec@{25}: rebase\n(finish): refs/heads/fetch-shallow-narrow-refspec onto 8103b446517e0>\nde83f683aa refs/heads/fetch-shallow-narrow-refspec@{26}: rebase\n(finish): refs/heads/fetch-shallow-narrow-refspec onto 8103b446517e0>\nbc9f5f0a75 refs/heads/fetch-shallow-narrow-refspec@{27}: rebase\n(finish): refs/heads/fetch-shallow-narrow-refspec onto f9856e2cf3f65>\n29869baf42 refs/heads/fetch-shallow-narrow-refspec@{28}: rebase\n(finish): refs/heads/fetch-shallow-narrow-refspec onto 8103b446517e0>\nbb9c735cc5 refs/heads/fetch-shallow-narrow-refspec@{29}: pull\n(finish): refs/heads/fetch-shallow-narrow-refspec onto\n8103b446517e0c4>\n84d192445c (tag:\npr-git-2412/HaraldNordgren/fetch-shallow-narrow-refspec-v6)\nrefs/heads/fetch-shallow-narrow-refspec@{30}: rebase (f>\n5dd7145ab7 refs/heads/fetch-shallow-narrow-refspec@{31}: pull\n(finish): refs/heads/fetch-shallow-narrow-refspec onto\nc46c1e37724f047>\n8c2a144eb7 (tag:\npr-git-2412/HaraldNordgren/fetch-shallow-narrow-refspec-v5)\nrefs/heads/fetch-shallow-narrow-refspec@{32}: rebase (f>\n43b9711a2c (tag:\npr-git-2412/HaraldNordgren/fetch-shallow-narrow-refspec-v4)\nrefs/heads/fetch-shallow-narrow-refspec@{33}: pull (fin>\n73fec138c3 refs/heads/fetch-shallow-narrow-refspec@{34}: rebase\n(finish): refs/heads/fetch-shallow-narrow-refspec onto d3ee079ea9d66>\n49f194522a refs/heads/fetch-shallow-narrow-refspec@{35}: commit\n(amend): remote: default to --limited-fetch in a shallow repository\n84f6640ab9 refs/heads/fetch-shallow-narrow-refspec@{36}: rebase\n(finish): refs/heads/fetch-shallow-narrow-refspec onto 0f8e75abebff0>\n```\n\n\nHarald\n\n"},{"id":"556187","messageId":"CAHwyqnX5sbmL_2tJ59KoBzMiTiz5OVzJXuwV+hUxU_b3YrXo-g@mail.gmail.com","threadId":"66545","inReplyTo":"bfc2b43a-6198-47b0-8868-d92551548f22@gmail.com","subject":"Re: [PATCH v2 0/3] reflog: add --operations and --historical-branch","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-24T19:12:45Z","receivedAt":"2026-10-24T19:12:45Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> >   * --historical-branch decorates each entry with the branch HEAD was on when\n> >     it was recorded.\n>\n> I quite like this idea, but think it would be nicer implemented as a\n> format specifier (\"%gb\" seems to be free) so that users can use it in\n> their custom formats. I'd be quite happy to change the default format do\n> for HEAD to include the branch name after the reflog message but I'm not\n> sure others would be so keen (perhaps we could get away with guarding\n> any change by isatty()/GIT_PAGER_IN_USE?).\n\nSeems like an interesting plan to do `%gb`, instead. It radically\nchanges this series and drops `--operations` altogether then.\n\n> If we're going to do this we should make sure it supports \"git rebase\n> <upstream> <branch>\" so that the branch names are accurate. That is a\n> bit involved because you have to compare the name of the branch being\n> rebased (which is in the \"(finish)\" reflog message) to the name of the\n> branch that was checked out before (which isn't in that message so you\n> have to go looking for a previous rebase or checkout). It also needs to\n> take account of rebases that checkout a detached HEAD which don't have\n> the \"(finish)\" message and those that checkout an up-to-date branch or\n> detached HEAD where the message is \"$GIT_REFLOG_ACTION: checkout\n> $revision\". I've got a patch [1] that fixes this for \"@{-N}\" which I\n> think works, but it needs some more tests.\n\nNice, looking forward to it.\n\n\nHarald\n\n"},{"id":"556221","messageId":"fc4d4a00-bc3a-4e1a-939c-f3a68fd8ce08@app.fastmail.com","threadId":"66545","inReplyTo":"CAHwyqnVJ618QXq99M=XNm19eEObQC-DTLcpeYOiizzSu_YNEpw@mail.gmail.com","subject":"Re: [PATCH v2 0/3] reflog: add --operations and --historical-branch","fromName":"Kristoffer Haugsbakk","fromEmail":"kristofferhaugsbakk@fastmail.com","sentAt":"2026-10-25T18:52:49Z","receivedAt":"2026-10-25T18:52:49Z","isPatch":true,"sender":{"key":"kristofferhaugsbakk@fastmail.com","avatar":null},"body":"On Sat, Oct 24, 2026, at 20:46, Harald Nordgren wrote:\n> On Sat, Oct 24, 2026 at 6:40 PM Junio C Hamano <gitster@pobox.com> wrote:\n>>\n>> Phillip Wood <phillip.wood123@gmail.com> writes:\n>>\n>> > I'm rather skeptical of this in its current form. Not all commands that\n>> > modify the working tree update the reflog and some commands such as \"git\n>> > cherry-pick A B C\" create multiple reflog entires that are\n>> > indistinguishable from multiple invocations of a single command.\n>>\n>>[snip]\n>>\n>> By the way, I too was annoyed that 'git reflog' defaults to the log\n>> of HEAD a long time ago, but the irritation soon waned once I made\n>> it a habit to always provide @{now} or @{0} to the command, as that\n>> notation refers to the current branch.\n>\n> Thanks Phillip, Ben and Junio for pointing this out!\n>\n> I didn't realize that reflog could be changed to operate only on one\n> branch. And it makes it a lot better! Seems like it should be the\n> default, and I don't think a lot of people would complain, I'll\n> certainly never run the old reflog again.\n\nWell. I wrote in this thread:\n\n> That you rebase many commits and then the reset points (where each one\n> started and ended) get obscured in the reflog is classic for me. And I\n> think many have had the same issue.\n\nBut now I wonder if that was just an illusion:\n\n    $ # The rebase was for five commits\n    $ git reflog --no-decorate test-branch\n    0e77d8619b8 test-branch@{0}: rebase (finish): refs/heads/test-branch onto 7e34d8174dcda3435d1404c9a30bef46afe6c13e\n    d1bcad06e24 test-branch@{1}: branch: Created from HEAD\n\nI have always known about per-branch reflogs and that `HEAD` has its\nown, but maybe I was too much in the habit of running without the branch\nname after rebases?\n\nSo maybe my use case was fine all along. But that still leaves\nPatrick’s[1] problems with it.\n\n[1] https://lore.kernel.org/git/akZfm-igZKeHaDST@pks.im/\n\n>\n> However, the format is quite heavy with the full name references,\n> seems like they could be cleaned up:\n>\n>[snip]\n\n"},{"id":"556224","messageId":"8ffe4cb4-fd89-4086-945b-735316e6d501@app.fastmail.com","threadId":"66545","inReplyTo":"aa5a98b80d2682cc0fa321c091c76e483fb6810f.1792348037.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v2 3/3] reflog: add --historical-branch to name the branch of each entry","fromName":"Kristoffer Haugsbakk","fromEmail":"kristofferhaugsbakk@fastmail.com","sentAt":"2026-10-25T19:18:31Z","receivedAt":"2026-10-25T19:18:31Z","isPatch":true,"sender":{"key":"kristofferhaugsbakk@fastmail.com","avatar":null},"body":"On Sun, Oct 18, 2026, at 20:27, Harald Nordgren via GitGitGadget wrote:\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n>[snip]\n> diff --git a/reflog-walk.c b/reflog-walk.c\n>[snip]\n>  static struct complete_reflogs *read_complete_reflog(const char *ref)\n>  {\n>  \tstruct complete_reflogs *reflogs =\n> @@ -178,13 +257,16 @@ void init_reflog_walk(struct reflog_walk_info **info)\n>  \t(*info)->complete_reflogs.strdup_strings = 1;\n>  }\n>\n> -void reflog_walk_mark(struct reflog_walk_info *info, bool operations)\n> +void reflog_walk_mark(struct reflog_walk_info *info, bool operations,\n> +\t\t      bool historical_branch)\n>  {\n>  \tstruct string_list_item *item;\n>\n>  \tfor_each_string_list_item(item, &info->complete_reflogs) {\n>  \t\tif (operations)\n>  \t\t\tmark_operations(item->util);\n\nThis was from the previous commit. A function that uses a `bool` param,\niterates/walks over a list, and then *if* `operations` is `true` (1)\ncalls a function, that looks weird to me. You’re either going to do it\nto every item or just walk the list without doing anything.\n\n> +\t\tif (historical_branch)\n> +\t\t\tmark_branches(item->util);\n\nBut now you have two `bool` and you do two checks.\n\nThis is less weird to my eyes but these conditions inside a for-each\nconstruct/macro still looks like a bad approach. Can’t you check each\ncondition and then call for-each with an unconditional `mark_branches`\nfor each item?\n\n    if (cond)\n        for-each item in items\n            fun(item)\n\n>  \t}\n>  }\n>\n> @@ -287,6 +369,16 @@ int add_reflog_for_walk(struct reflog_walk_info *info,\n>  \treturn 0;\n>  }\n>[snip]\n\n"},{"id":"556228","messageId":"CAHwyqnVs3+ebj25fkTmeRjDTJsJQh9ECNYMy1xU_yJLDdDeAaQ@mail.gmail.com","threadId":"66545","inReplyTo":"8ffe4cb4-fd89-4086-945b-735316e6d501@app.fastmail.com","subject":"Re: [PATCH v2 3/3] reflog: add --historical-branch to name the branch of each entry","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-25T20:22:38Z","receivedAt":"2026-10-25T20:22:38Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"On Sun, Oct 25, 2026 at 9:18 PM Kristoffer Haugsbakk\n<kristofferhaugsbakk@fastmail.com> wrote:\n>\n> On Sun, Oct 18, 2026, at 20:27, Harald Nordgren via GitGitGadget wrote:\n> > From: Harald Nordgren <haraldnordgren@gmail.com>\n> >[snip]\n> > diff --git a/reflog-walk.c b/reflog-walk.c\n> >[snip]\n> >  static struct complete_reflogs *read_complete_reflog(const char *ref)\n> >  {\n> >       struct complete_reflogs *reflogs =\n> > @@ -178,13 +257,16 @@ void init_reflog_walk(struct reflog_walk_info **info)\n> >       (*info)->complete_reflogs.strdup_strings = 1;\n> >  }\n> >\n> > -void reflog_walk_mark(struct reflog_walk_info *info, bool operations)\n> > +void reflog_walk_mark(struct reflog_walk_info *info, bool operations,\n> > +                   bool historical_branch)\n> >  {\n> >       struct string_list_item *item;\n> >\n> >       for_each_string_list_item(item, &info->complete_reflogs) {\n> >               if (operations)\n> >                       mark_operations(item->util);\n>\n> This was from the previous commit. A function that uses a `bool` param,\n> iterates/walks over a list, and then *if* `operations` is `true` (1)\n> calls a function, that looks weird to me. You’re either going to do it\n> to every item or just walk the list without doing anything.\n>\n> > +             if (historical_branch)\n> > +                     mark_branches(item->util);\n>\n> But now you have two `bool` and you do two checks.\n>\n> This is less weird to my eyes but these conditions inside a for-each\n> construct/macro still looks like a bad approach. Can’t you check each\n> condition and then call for-each with an unconditional `mark_branches`\n> for each item?\n>\n>     if (cond)\n>         for-each item in items\n>             fun(item)\n>\n> >       }\n> >  }\n> >\n> > @@ -287,6 +369,16 @@ int add_reflog_for_walk(struct reflog_walk_info *info,\n> >       return 0;\n> >  }\n> >[snip]\n\nThanks! But maybe not worth reviewing this code now considering that\nwe don't know if we will even do any of this now?\n\n\nHarald\n\n"},{"id":"556281","messageId":"6568dd11-ea74-4e50-912f-a589356cf6b6@app.fastmail.com","threadId":"66545","inReplyTo":"CAHwyqnVs3+ebj25fkTmeRjDTJsJQh9ECNYMy1xU_yJLDdDeAaQ@mail.gmail.com","subject":"Re: [PATCH v2 3/3] reflog: add --historical-branch to name the branch of each entry","fromName":"Kristoffer Haugsbakk","fromEmail":"kristofferhaugsbakk@fastmail.com","sentAt":"2026-10-26T08:39:43Z","receivedAt":"2026-10-26T08:39:43Z","isPatch":true,"sender":{"key":"kristofferhaugsbakk@fastmail.com","avatar":null},"body":"On Sun, Oct 25, 2026, at 21:22, Harald Nordgren wrote:\n> On Sun, Oct 25, 2026 at 9:18 PM Kristoffer Haugsbakk\n> <kristofferhaugsbakk@fastmail.com> wrote:\n>> >\n>> >[snip]\n>\n> Thanks! But maybe not worth reviewing this code now considering that\n> we don't know if we will even do any of this now?\n\nI think reviewing can be about the author and the other person\nlearning something.\n\nIn this case I remembered that part of the code from\nskimming previously.\n\n"},{"id":"556283","messageId":"CAHwyqnUrxRLRJBmV4r+1A8kcUmG9S7v38tX5bDAMCVok+83hag@mail.gmail.com","threadId":"66545","inReplyTo":"6568dd11-ea74-4e50-912f-a589356cf6b6@app.fastmail.com","subject":"Re: [PATCH v2 3/3] reflog: add --historical-branch to name the branch of each entry","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-26T08:42:55Z","receivedAt":"2026-10-26T08:42:55Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"On Mon, Oct 26, 2026 at 10:40 AM Kristoffer Haugsbakk\n<kristofferhaugsbakk@fastmail.com> wrote:\n>\n> On Sun, Oct 25, 2026, at 21:22, Harald Nordgren wrote:\n> > On Sun, Oct 25, 2026 at 9:18 PM Kristoffer Haugsbakk\n> > <kristofferhaugsbakk@fastmail.com> wrote:\n> >> >\n> >> >[snip]\n> >\n> > Thanks! But maybe not worth reviewing this code now considering that\n> > we don't know if we will even do any of this now?\n>\n> I think reviewing can be about the author and the other person\n> learning something.\n>\n> In this case I remembered that part of the code from\n> skimming previously.\n\nI appreciated it, I just didn't want you to waste your time!\n\n\nHarald\n\n"},{"id":"556307","messageId":"1bc5a05e-3d3b-4f56-8b11-2e276cef48ca@gmail.com","threadId":"66545","inReplyTo":"xmqq5wyr128q.fsf@gitster.g","subject":"Re: [PATCH v2 0/3] reflog: add --operations and --historical-branch","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-10-26T10:52:57Z","receivedAt":"2026-10-26T10:52:57Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"On 24/10/2026 17:40, Junio C Hamano wrote:\n> Phillip Wood <phillip.wood123@gmail.com> writes:\n> >> I'm rather skeptical of this in its current form. Not all commands that\n>> modify the working tree update the reflog and some commands such as \"git\n>> cherry-pick A B C\" create multiple reflog entires that are\n>> indistinguishable from multiple invocations of a single command.\n> > I like the direction to make the default 'git reflog' output on a\n> single branch easier to understand without needing any extra\n> filtering in this series.\n> > 'git rebase' doing its work on a detached HEAD was likely the\n> first, and we may want to convert more multi-step commands to do\n> so, including the multiple 'git cherry-pick' you mention. It would certainly be nice to avoid creating lots of entries in the branch reflog when cherry-picking. I'm not sure what users would make of us switching to a detached HEAD as some people seem to find it confusing; if we do decide to do that we should make sure we update \"git status\" to explain what's happening. I'm more relaxed about changing \"git am\", but like you I normally detach HEAD first so maybe I'm not the best person to ask about that.\n\n> By the way, I too was annoyed that 'git reflog' defaults to the log\n> of HEAD a long time ago, but the irritation soon waned once I made\n> it a habit to always provide @{now} or @{0} to the command, as that\n> notation refers to the current branch.\nOh, I hadn't realized that - thanks for the tip\n\nPhillip\n\n"},{"id":"556310","messageId":"CAHwyqnUckb2ccoRToYwk=OVkHtWX1rRNa0z7TwMHcHQufNuNEQ@mail.gmail.com","threadId":"66545","inReplyTo":"1bc5a05e-3d3b-4f56-8b11-2e276cef48ca@gmail.com","subject":"Re: [PATCH v2 0/3] reflog: add --operations and --historical-branch","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-26T11:00:42Z","receivedAt":"2026-10-26T11:00:42Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"> It would certainly be nice to avoid creating lots of entries in the\n> branch reflog when cherry-picking. I'm not sure what users would make of\n> us switching to a detached HEAD as some people seem to find it\n> confusing; if we do decide to do that we should make sure we update \"git\n> status\" to explain what's happening. I'm more relaxed about changing\n> \"git am\", but like you I normally detach HEAD first so maybe I'm not the\n> best person to ask about that.\n\nI think that the fact that all of us have our tricks to work around\nthings is indicative of a real problem.\n\nWe should attack the root cause and simplify git so that even us would\nprefer vanilla git.\n\n\nHarald\n\n"},{"id":"556357","messageId":"xmqq1p9cv6tp.fsf@gitster.g","threadId":"66545","inReplyTo":"CAHwyqnUckb2ccoRToYwk=OVkHtWX1rRNa0z7TwMHcHQufNuNEQ@mail.gmail.com","subject":"Re: [PATCH v2 0/3] reflog: add --operations and --historical-branch","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-26T15:07:30Z","receivedAt":"2026-10-26T15:07:30Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Harald Nordgren <haraldnordgren@gmail.com> writes:\n\n>> It would certainly be nice to avoid creating lots of entries in the\n>> branch reflog when cherry-picking. I'm not sure what users would make of\n>> us switching to a detached HEAD as some people seem to find it\n>> confusing; if we do decide to do that we should make sure we update \"git\n>> status\" to explain what's happening. I'm more relaxed about changing\n>> \"git am\", but like you I normally detach HEAD first so maybe I'm not the\n>> best person to ask about that.\n>\n> I think that the fact that all of us have our tricks to work around\n> things is indicative of a real problem.\n>\n> We should attack the root cause and simplify git so that even us would\n> prefer vanilla git.\n\nI do use and prefer vanilla Git as it was designed to work, though.\n\n\n"},{"id":"556455","messageId":"pull.2448.v3.git.git.1793090327.gitgitgadget@gmail.com","threadId":"66545","inReplyTo":"pull.2448.git.git.1792070038.gitgitgadget@gmail.com","subject":"[PATCH v3 0/2] reflog: add --operations and --historical-branch","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-27T08:38:45Z","receivedAt":"2026-10-27T08:38:45Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"Create a better reflog. Follows the discussion in\nhttps://lore.kernel.org/git/akZfm-igZKeHaDST@pks.im/.\n\nThe reflog of HEAD records the work done on every branch, so \"git reflog\"\nmixes them and does not say which entry was made on which branch. This adds\na \"%gb\" placeholder that names the branch HEAD was on when each entry was\nrecorded:\n\n$ git log -g --format='%h %gd: (%gb) %gs'\n9c8d7e6 HEAD@{0}: (topic) rebase (finish): returning to refs/heads/topic\n1a2b3c4 HEAD@{2}: (topic) rebase (start): checkout main\n1a2b3c4 HEAD@{3}: (main) commit: fix typo\n\n\nIt follows branch switches, branch renames and rebases, including \"git\nrebase \", rebases of a detached HEAD and rebases of a branch that was\nalready up to date. When HEAD was detached, the entry shows \"detached at \".\n\nChanges in v3:\n\n * Dropped --operations.\n * Replaced --historical-branch with a %gb placeholder for --format, e.g.\n   git log -g --format='%h %gd: (%gb) %gs'.\n * %gb names the right branch after git rebase <upstream> <branch>, a rebase\n   of a detached HEAD and an up-to-date rebase.\n * Named the returning to and checkout reflog strings in reflog.h too.\n\nChanges in v2:\n\n * Move the \"historical branch\" rightward in the display for cleaner\n   alignment.\n * Improve commit messages.\n\nHarald Nordgren (2):\n  reflog: name the strings that mark switches, renames and rebase steps\n  pretty: add %gb to name the branch of each reflog entry\n\n Documentation/pretty-formats.adoc |   3 +\n builtin/branch.c                  |   3 +-\n builtin/checkout.c                |   3 +-\n builtin/rebase.c                  |  19 ++--\n object-name.c                     |   3 +-\n pretty.c                          |   4 +\n reflog-walk.c                     | 156 ++++++++++++++++++++++++++++--\n reflog-walk.h                     |   2 +\n reflog.h                          |   8 ++\n sequencer.c                       |  13 +--\n t/t1414-reflog-walk.sh            | 122 +++++++++++++++++++++++\n wt-status.c                       |   3 +-\n 12 files changed, 314 insertions(+), 25 deletions(-)\n\n\nbase-commit: 7e34d8174dcda3435d1404c9a30bef46afe6c13e\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2448%2FHaraldNordgren%2Freflog-operations-v3\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2448/HaraldNordgren/reflog-operations-v3\nPull-Request: https://github.com/git/git/pull/2448\n\nRange-diff vs v2:\n\n 1:  745929bd80 ! 1:  4d779e8da3 reflog: name the strings that mark switches, renames and rebase steps\n     @@ Metadata\n       ## Commit message ##\n          reflog: name the strings that mark switches, renames and rebase steps\n      \n     -    The next commit adds \"git reflog --operations\", which finds branch\n     -    switches, branch renames and the start and end of a rebase by the\n     -    messages they leave in the reflog. The commands that write these\n     -    messages spell out the strings by hand, and \"@{-<n>}\" and \"git status\"\n     -    each keep their own copy of the switch prefix to read it back. Reading\n     -    them in one more place would add yet another copy that has to stay in\n     -    sync.\n     +    The next commit adds a \"%gb\" placeholder that shows which branch HEAD\n     +    was on for each reflog entry. It finds the branch from the messages\n     +    that branch switches, branch renames and rebases leave in the reflog.\n     +    The commands that write these messages spell out the strings by hand,\n     +    and \"@{-<n>}\" and \"git status\" each keep their own copy of the switch\n     +    prefix to read it back. Reading them in one more place would add yet\n     +    another copy that has to stay in sync.\n      \n          Name the strings once in reflog.h and use the names wherever the\n          messages are written or read: the \"checkout: moving from \" prefix\n          written by \"git checkout\", the \"Branch: renamed \" prefix written by\n     -    \"git branch -m\", and the \"start\", \"finish\" and \"abort\" steps written\n     -    by \"git rebase\".\n     +    \"git branch -m\", and the \"start\", \"finish\" and \"abort\" steps, the\n     +    \"returning to \" before the branch a rebase goes back to and the\n     +    \"checkout \" before what it checks out, all written by \"git rebase\".\n      \n          No change in behavior.\n      \n     @@ builtin/rebase.c: static int move_to_original_branch(struct rebase_options *opts\n       \t\t    opts->head_name, oid_to_hex(&opts->onto->object.oid));\n      -\tstrbuf_addf(&head_reflog, \"%s (finish): returning to %s\",\n      -\t\t    opts->reflog_action, opts->head_name);\n     -+\tstrbuf_addf(&head_reflog, \"%s (%s): returning to %s\",\n     ++\tstrbuf_addf(&head_reflog, \"%s (%s): \" REFLOG_RETURNING_TO \"%s\",\n      +\t\t    opts->reflog_action, REFLOG_STEP_FINISH, opts->head_name);\n       \tropts.branch = opts->head_name;\n       \tropts.flags = RESET_WORKING_TREE_REFS_ONLY |\n       \t\t      RESET_WORKING_TREE_UPDATE_HEAD;\n     +@@ builtin/rebase.c: static int checkout_up_to_date(struct rebase_options *options)\n     + \tstruct reset_working_tree_options ropts = { 0 };\n     + \tint ret = 0;\n     + \n     +-\tstrbuf_addf(&buf, \"%s: checkout %s\",\n     ++\tstrbuf_addf(&buf, \"%s: \" REFLOG_CHECKOUT \"%s\",\n     + \t\t    options->reflog_action, options->switch_to);\n     + \tropts.oid = &options->orig_head->object.oid;\n     + \tropts.branch = options->head_name;\n      @@ builtin/rebase.c: int cmd_rebase(int argc,\n       \t\tif (read_basic_state(&options))\n       \t\t\texit(1);\n       \n      -\t\tstrbuf_addf(&head_msg, \"%s (abort): returning to %s\",\n      -\t\t\t    options.reflog_action,\n     -+\t\tstrbuf_addf(&head_msg, \"%s (%s): returning to %s\",\n     ++\t\tstrbuf_addf(&head_msg, \"%s (%s): \" REFLOG_RETURNING_TO \"%s\",\n      +\t\t\t    options.reflog_action, REFLOG_STEP_ABORT,\n       \t\t\t    options.head_name ? options.head_name\n       \t\t\t\t\t      : oid_to_hex(&options.orig_head->object.oid));\n     @@ builtin/rebase.c: int cmd_rebase(int argc,\n       \n      -\tstrbuf_addf(&msg, \"%s (start): checkout %s\",\n      -\t\t    options.reflog_action, options.onto_name);\n     -+\tstrbuf_addf(&msg, \"%s (%s): checkout %s\", options.reflog_action,\n     -+\t\t    REFLOG_STEP_START, options.onto_name);\n     ++\tstrbuf_addf(&msg, \"%s (%s): \" REFLOG_CHECKOUT \"%s\",\n     ++\t\t    options.reflog_action, REFLOG_STEP_START, options.onto_name);\n       \tropts.oid = &options.onto->object.oid;\n       \tropts.orig_head = &options.orig_head->object.oid;\n       \tropts.flags = RESET_WORKING_TREE_DETACH |\n     @@ reflog.h\n      +#define REFLOG_STEP_START \"start\"\n      +#define REFLOG_STEP_FINISH \"finish\"\n      +#define REFLOG_STEP_ABORT \"abort\"\n     ++#define REFLOG_RETURNING_TO \"returning to \"\n     ++#define REFLOG_CHECKOUT \"checkout \"\n      +\n       #define REFLOG_EXPIRE_TOTAL   (1 << 0)\n       #define REFLOG_EXPIRE_UNREACH (1 << 1)\n     @@ sequencer.c: static int checkout_onto(struct repository *r, struct replay_opts *\n      -\t\t.head_msg = reflog_message(opts, \"start\", \"checkout %s\",\n      -\t\t\t\t\t   onto_name),\n      +\t\t.head_msg = reflog_message(opts, REFLOG_STEP_START,\n     -+\t\t\t\t\t   \"checkout %s\", onto_name),\n     ++\t\t\t\t\t   REFLOG_CHECKOUT \"%s\", onto_name),\n       \t\t.default_reflog_action = sequencer_reflog_action(opts)\n       \t};\n       \tif (reset_working_tree(r, &ropts)) {\n     @@ sequencer.c: cleanup_head_ref:\n      -\t\t\tmsg = reflog_message(opts, \"finish\", \"returning to %s\",\n      -\t\t\t\thead_ref.buf);\n      +\t\t\tmsg = reflog_message(opts, REFLOG_STEP_FINISH,\n     -+\t\t\t\t\"returning to %s\", head_ref.buf);\n     ++\t\t\t\tREFLOG_RETURNING_TO \"%s\", head_ref.buf);\n       \t\t\tif (refs_update_symref(get_main_ref_store(the_repository), \"HEAD\", head_ref.buf, msg)) {\n       \t\t\t\tres = error(_(\"could not update HEAD to %s\"),\n       \t\t\t\t\thead_ref.buf);\n 2:  1c47ba225e < -:  ---------- reflog: add --operations to show one entry per command\n 3:  aa5a98b80d ! 2:  dee548f9b1 reflog: add --historical-branch to name the branch of each entry\n     @@ Metadata\n      Author: Harald Nordgren <haraldnordgren@gmail.com>\n      \n       ## Commit message ##\n     -    reflog: add --historical-branch to name the branch of each entry\n     +    pretty: add %gb to name the branch of each reflog entry\n      \n     -    \"git reflog\" does not say which branch HEAD was on when an entry was\n     -    recorded. After switching between a few branches, telling which\n     -    commits and rebases belong to which branch means reading back to the\n     -    last \"checkout: moving from\" entry for each of them. Commit\n     -    decorations do not help either, since they only name branches that\n     -    still point at that commit, and a rebased or amended commit has none.\n     +    The reflog of HEAD records the work done on every branch, so the\n     +    output of \"git reflog\" mixes them and does not say which entry was\n     +    made on which branch. Working that out by hand means looking back for\n     +    the branch switch before each entry, and a rebase can move HEAD to\n     +    another branch without writing one, as \"git rebase <upstream>\n     +    <branch>\" does.\n      \n     -    Teach \"git reflog\" and \"git log -g\" a \"--historical-branch\" option\n     -    that shows the branch each entry was made on before its message, in\n     -    place of the refs that point at the commit now. The hashes and\n     -    selectors stay lined up in their columns, and the name is colored like\n     -    a branch decoration:\n     +    Add a \"%gb\" placeholder that names the branch HEAD was on when each\n     +    entry was recorded:\n      \n     -      $ git reflog --historical-branch\n     -      4e5f6a7 HEAD@{0}: (topic) commit: add tests\n     -      1a2b3c4 HEAD@{1}: (topic) checkout: moving from main to topic\n     -      8c9d0e1 HEAD@{2}: (main) commit: fix typo\n     +        $ git log -g --format='%h %gd: (%gb) %gs'\n     +        9c8d7e6 HEAD@{0}: (topic) rebase (finish): returning to refs/heads/topic\n     +        9c8d7e6 HEAD@{1}: (topic) rebase (pick): add tests\n     +        1a2b3c4 HEAD@{2}: (topic) rebase (start): checkout main\n     +        1a2b3c4 HEAD@{3}: (main) commit: fix typo\n     +        5d6e7f8 HEAD@{4}: (main) checkout: moving from topic to main\n      \n     -    Every entry gets a name, including ones whose commits are no longer on\n     -    any branch. Entries from before a branch was renamed show its old name,\n     -    and entries made on a detached HEAD show \"detached at <commit>\".\n     -    Combined with \"--operations\", which leaves out the branch switches\n     -    themselves, it lists what was done to each branch.\n     +    The branch is read from the entries that branch switches, branch\n     +    renames and rebases leave behind. The entries of a rebase are named\n     +    after the branch being rebased. When HEAD was detached, the entry\n     +    shows \"detached at <commit>\" with the commit HEAD was at when it\n     +    moved on. When nothing older in the reflog says which branch a rebase\n     +    started on, the rebase is taken to have started on the branch it\n     +    rebased. For the reflog of any other ref, \"%gb\" is the name of that\n     +    ref.\n      \n          Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n      \n     - ## Documentation/git-reflog.adoc ##\n     -@@ Documentation/git-reflog.adoc: actions, and in addition the `HEAD` reflog records branch switching.\n     - --pretty=oneline`; see linkgit:git-log[1] for more information.\n     - With `--operations`, branch switches and other entries that change no\n     - branch are left out, and a rebase is shown as a single entry instead\n     --of one entry for each commit it replayed.\n     -+of one entry for each commit it replayed. With `--historical-branch`,\n     -+each entry shows the branch it was made on before its message.\n     - \n     - The \"list\" subcommand lists all refs which have a corresponding reflog.\n     - \n     + ## Documentation/pretty-formats.adoc ##\n     +@@ Documentation/pretty-formats.adoc: endif::git-rev-list[]\n     + +%gE+:: reflog identity email (respecting .mailmap, see\n     + \tlinkgit:git-shortlog[1] or linkgit:git-blame[1])\n     + +%gs+:: reflog subject\n     +++%gb+:: reflog branch, the branch `HEAD` was on when the entry was\n     ++\trecorded, e.g., `topic` or `detached at 1a2b3c4`. For the reflog\n     ++\tof any other ref, this is the shortened refname.\n     + ++%(trailers++`[:<option>,...]`++)++::\n     + display the trailers of the body as interpreted by\n     + linkgit:git-interpret-trailers[1]. The `trailers` string may be followed by\n      \n     - ## Documentation/rev-list-options.adoc ##\n     -@@ Documentation/rev-list-options.adoc: Under `--pretty=reference`, this information will not be shown at all.\n     - \trebase started. It is an error to use this option unless\n     - \t`--walk-reflogs` is in use.\n     - \n     -+ifndef::git-rev-list[]\n     -+`--historical-branch`::\n     -+\tShow the branch `HEAD` was on when each reflog entry was\n     -+\trecorded, such as `(topic)`, before the reflog message, in\n     -+\tplace of the refs that point at the commit now. When `HEAD` was\n     -+\tdetached, the entry shows `(detached at <commit>)`. For the\n     -+\treflog of any other ref, the name of that ref is shown. It is\n     -+\tan error to use this option unless `--walk-reflogs` is in use.\n     -+endif::git-rev-list[]\n     -+\n     - `--merge`::\n     - \tShow commits touching conflicted paths in the range `HEAD...<other>`,\n     - \twhere `<other>` is the first existing pseudoref in `MERGE_HEAD`,\n     -\n     - ## log-tree.c ##\n     -@@ log-tree.c: void format_decorations(struct strbuf *sb,\n     - \t}\n     - }\n     - \n     -+static void format_historical_branch(struct strbuf *sb,\n     -+\t\t\t\t     struct rev_info *opt)\n     -+{\n     -+\tconst char *branch = get_reflog_branch(opt->reflog_info);\n     -+\tenum git_colorbool use_color = opt->diffopt.use_color;\n     -+\tconst char *color_commit, *color_reset;\n     -+\n     -+\tif (!branch)\n     -+\t\treturn;\n     -+\n     -+\tcolor_commit = diff_get_color(use_color, DIFF_COMMIT);\n     -+\tcolor_reset = decorate_get_color(use_color, DECORATION_NONE);\n     -+\tstrbuf_addf(sb, \"%s(%s%s%s%s%s)%s \",\n     -+\t\t    color_commit, color_reset,\n     -+\t\t    decorate_get_color(use_color, DECORATION_REF_LOCAL),\n     -+\t\t    branch, color_reset, color_commit, color_reset);\n     -+}\n     -+\n     - void show_decorations(struct rev_info *opt, struct commit *commit)\n     - {\n     - \tstruct strbuf sb = STRBUF_INIT;\n     -@@ log-tree.c: void show_decorations(struct rev_info *opt, struct commit *commit)\n     - \t\tif (slot && *slot)\n     - \t\t\tfprintf(opt->diffopt.file, \"\\t%s\", *slot);\n     - \t}\n     --\tif (!opt->show_decorations)\n     -+\tif (!opt->show_decorations || opt->historical_branch)\n     - \t\treturn;\n     - \tformat_decorations(&sb, commit, opt->diffopt.use_color, NULL);\n     - \tfputs(sb.buf, opt->diffopt.file);\n     -@@ log-tree.c: void show_log(struct rev_info *opt)\n     - \t\t\tgraph_show_oneline(opt->graph);\n     - \t\t}\n     - \t\tif (opt->reflog_info) {\n     -+\t\t\tstruct strbuf branch = STRBUF_INIT;\n     -+\n     - \t\t\t/*\n     - \t\t\t * setup_revisions() ensures that opt->reflog_info\n     - \t\t\t * and opt->graph cannot both be set,\n     - \t\t\t * so we don't need to worry about printing the\n     - \t\t\t * graph info here.\n     - \t\t\t */\n     -+\t\t\tif (opt->historical_branch)\n     -+\t\t\t\tformat_historical_branch(&branch, opt);\n     - \t\t\tshow_reflog_message(opt->reflog_info,\n     - \t\t\t\t\t    opt->commit_format == CMIT_FMT_ONELINE,\n     - \t\t\t\t\t    opt->date_mode,\n     --\t\t\t\t\t    opt->date_mode_explicit);\n     -+\t\t\t\t\t    opt->date_mode_explicit,\n     -+\t\t\t\t\t    branch.buf);\n     -+\t\t\tstrbuf_release(&branch);\n     - \t\t\tif (opt->commit_format == CMIT_FMT_ONELINE)\n     - \t\t\t\treturn;\n     - \t\t}\n     + ## pretty.c ##\n     +@@ pretty.c: static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */\n     + \t\t\tif (c->pretty_ctx->reflog_info)\n     + \t\t\t\tget_reflog_message(sb, c->pretty_ctx->reflog_info);\n     + \t\t\treturn 2;\n     ++\t\tcase 'b':\t/* reflog branch */\n     ++\t\t\tif (c->pretty_ctx->reflog_info)\n     ++\t\t\t\tget_reflog_branch(sb, c->pretty_ctx->reflog_info);\n     ++\t\t\treturn 2;\n     + \t\tcase 'n':\n     + \t\tcase 'N':\n     + \t\tcase 'e':\n      \n       ## reflog-walk.c ##\n      @@\n     + #include \"git-compat-util.h\"\n     + #include \"commit.h\"\n       #include \"refs.h\"\n     - #include \"reflog.h\"\n     ++#include \"reflog.h\"\n       #include \"diff.h\"\n      +#include \"hex.h\"\n      +#include \"object-name.h\"\n     @@ reflog-walk.c: struct complete_reflogs {\n       \t\tint tz;\n       \t\tchar *message;\n      +\t\tchar *branch;\n     - \t\tbool user_operation;\n       \t} *items;\n       \tint nr, alloc;\n     ++\tbool branches_marked;\n     + };\n     + \n     + static int read_one_reflog(const char *refname UNUSED,\n      @@ reflog-walk.c: static int read_one_reflog(const char *refname UNUSED,\n       \titem->timestamp = timestamp;\n       \titem->tz = tz;\n       \titem->message = xstrdup(message);\n      +\titem->branch = NULL;\n     - \titem->user_operation = true;\n       \tarray->nr++;\n       \treturn 0;\n     + }\n      @@ reflog-walk.c: static void free_complete_reflog(struct complete_reflogs *array)\n       \tfor (i = 0; i < array->nr; i++) {\n       \t\tfree(array->items[i].email);\n     @@ reflog-walk.c: static void free_complete_reflog(struct complete_reflogs *array)\n       \t}\n       \tfree(array->items);\n       \tfree(array->ref);\n     -@@ reflog-walk.c: static void mark_operations(struct complete_reflogs *array)\n     - \t}\n     +@@ reflog-walk.c: static void complete_reflogs_clear(void *util, const char *str UNUSED)\n     + \tfree_complete_reflog(array);\n       }\n       \n     -+static char *branch_label(const char *name, size_t len)\n     ++static char *branch_label(const char *name, size_t len,\n     ++\t\t\t  const struct object_id *oid)\n      +{\n     -+\tstruct object_id oid;\n     -+\tconst char *short_name;\n     ++\tif (name) {\n     ++\t\tstruct object_id hex_oid;\n     ++\t\tchar *branch;\n      +\n     -+\tif (len == the_hash_algo->hexsz && !get_oid_hex(name, &oid))\n     -+\t\treturn xstrfmt(\"detached at %s\",\n     -+\t\t\t       repo_find_unique_abbrev(the_repository, &oid,\n     -+\t\t\t\t\t\t       DEFAULT_ABBREV));\n     -+\tif (skip_prefix(name, \"refs/heads/\", &short_name))\n     -+\t\tlen -= short_name - name;\n     -+\telse\n     -+\t\tshort_name = name;\n     -+\treturn xmemdupz(short_name, len);\n     ++\t\tif (skip_prefix(name, \"refs/heads/\", &name))\n     ++\t\t\tlen -= strlen(\"refs/heads/\");\n     ++\t\tbranch = xmemdupz(name, len);\n     ++\t\tif (strcmp(branch, \"HEAD\") && get_oid_hex(branch, &hex_oid) &&\n     ++\t\t    !check_refname_format(branch, REFNAME_ALLOW_ONELEVEL))\n     ++\t\t\treturn branch;\n     ++\t\tfree(branch);\n     ++\t}\n     ++\treturn xstrfmt(\"detached at %s\",\n     ++\t\t       repo_find_unique_abbrev(the_repository, oid,\n     ++\t\t\t\t\t       DEFAULT_ABBREV));\n     ++}\n     ++\n     ++static const char *skip_rebase_step(const char *message, const char *step)\n     ++{\n     ++\tconst char *p = strstr(message, \" (\");\n     ++\tconst char *colon = strstr(message, \": \");\n     ++\n     ++\tif (!p || (colon && colon < p) ||\n     ++\t    !skip_prefix(p + 2, step, &p) || !skip_prefix(p, \"): \", &p))\n     ++\t\treturn NULL;\n     ++\treturn p;\n      +}\n      +\n      +struct branch_move {\n     @@ reflog-walk.c: static void mark_operations(struct complete_reflogs *array)\n      +{\n      +\tconst char *to;\n      +\n     -+\tif (!skip_prefix(message, REFLOG_SWITCH_PREFIX, &move->from) &&\n     -+\t    !skip_prefix(message, REFLOG_RENAME_PREFIX, &move->from))\n     ++\tmove->from = move->to = NULL;\n     ++\tmove->from_len = move->to_len = 0;\n     ++\tif (skip_prefix(message, REFLOG_SWITCH_PREFIX, &move->from) ||\n     ++\t    skip_prefix(message, REFLOG_RENAME_PREFIX, &move->from)) {\n     ++\t\tto = strstr(move->from, \" to \");\n     ++\t\tif (!to)\n     ++\t\t\treturn false;\n     ++\t\tmove->from_len = to - move->from;\n     ++\t\tmove->to = to + strlen(\" to \");\n     ++\t} else if (((to = skip_rebase_step(message, REFLOG_STEP_FINISH)) ||\n     ++\t\t    (to = skip_rebase_step(message, REFLOG_STEP_ABORT))) &&\n     ++\t\t   skip_prefix(to, REFLOG_RETURNING_TO, &to)) {\n     ++\t\tmove->from = move->to = to;\n     ++\t\tmove->from_len = strcspn(to, \"\\n\");\n     ++\t} else if (skip_prefix(message, \"rebase: \" REFLOG_CHECKOUT, &to)) {\n     ++\t\tmove->to = to;\n     ++\t} else if (!skip_rebase_step(message, REFLOG_STEP_START)) {\n      +\t\treturn false;\n     -+\tto = strstr(move->from, \" to \");\n     -+\tif (!to)\n     -+\t\treturn false;\n     -+\tmove->from_len = to - move->from;\n     -+\tmove->to = to + strlen(\" to \");\n     -+\tmove->to_len = strcspn(move->to, \"\\n\");\n     ++\t}\n     ++\tif (move->to)\n     ++\t\tmove->to_len = strcspn(move->to, \"\\n\");\n      +\treturn true;\n      +}\n      +\n      +static void mark_branches(struct complete_reflogs *array)\n      +{\n     ++\tstruct ref_store *refs = get_main_ref_store(the_repository);\n     ++\tstruct object_id oid;\n     ++\tconst char *head = refs_resolve_ref_unsafe(refs, \"HEAD\", 0, &oid, NULL);\n      +\tchar *branch = NULL;\n     ++\tint unknown = array->nr;\n      +\n     -+\tif (strcmp(array->ref, \"HEAD\"))\n     -+\t\treturn;\n     ++\tif (head)\n     ++\t\tbranch = branch_label(head, strlen(head), &oid);\n      +\n      +\tfor (int i = array->nr - 1; i >= 0; i--) {\n      +\t\tstruct reflog_info *item = &array->items[i];\n     @@ reflog-walk.c: static void mark_operations(struct complete_reflogs *array)\n      +\t\t\tcontinue;\n      +\t\t}\n      +\n     -+\t\tif (!branch)\n     -+\t\t\tfor (int j = i + 1; j < array->nr; j++)\n     -+\t\t\t\tarray->items[j].branch =\n     -+\t\t\t\t\tbranch_label(move.to, move.to_len);\n     -+\t\titem->branch = branch_label(move.to, move.to_len);\n     -+\t\tfree(branch);\n     -+\t\tbranch = branch_label(move.from, move.from_len);\n     ++\t\tif (!branch) {\n     ++\t\t\tbranch = branch_label(move.to, move.to_len,\n     ++\t\t\t\t\t      &array->items[unknown - 1].noid);\n     ++\t\t\tfor (int j = i + 1; j < unknown; j++)\n     ++\t\t\t\tarray->items[j].branch = xstrdup(branch);\n     ++\t\t}\n     ++\t\titem->branch = branch;\n     ++\t\tif (move.from) {\n     ++\t\t\tbranch = branch_label(move.from, move.from_len,\n     ++\t\t\t\t\t      &item->ooid);\n     ++\t\t} else {\n     ++\t\t\tbranch = NULL;\n     ++\t\t\tunknown = i;\n     ++\t\t}\n      +\t}\n      +\n     -+\tif (!branch) {\n     -+\t\tstruct ref_store *refs = get_main_ref_store(the_repository);\n     -+\t\tconst char *head = refs_resolve_ref_unsafe(refs, \"HEAD\", 0,\n     -+\t\t\t\t\t\t\t   NULL, NULL);\n     -+\n     -+\t\tif (head && skip_prefix(head, \"refs/heads/\", &head))\n     -+\t\t\tfor (int i = 0; i < array->nr; i++)\n     -+\t\t\t\tarray->items[i].branch = xstrdup(head);\n     -+\t}\n     ++\tif (!branch && unknown < array->nr)\n     ++\t\tfor (int i = 0; i < unknown; i++)\n     ++\t\t\tarray->items[i].branch =\n     ++\t\t\t\txstrdup(array->items[unknown].branch);\n      +\tfree(branch);\n      +}\n      +\n       static struct complete_reflogs *read_complete_reflog(const char *ref)\n       {\n       \tstruct complete_reflogs *reflogs =\n     -@@ reflog-walk.c: void init_reflog_walk(struct reflog_walk_info **info)\n     - \t(*info)->complete_reflogs.strdup_strings = 1;\n     - }\n     - \n     --void reflog_walk_mark(struct reflog_walk_info *info, bool operations)\n     -+void reflog_walk_mark(struct reflog_walk_info *info, bool operations,\n     -+\t\t      bool historical_branch)\n     - {\n     - \tstruct string_list_item *item;\n     - \n     - \tfor_each_string_list_item(item, &info->complete_reflogs) {\n     - \t\tif (operations)\n     - \t\t\tmark_operations(item->util);\n     -+\t\tif (historical_branch)\n     -+\t\t\tmark_branches(item->util);\n     - \t}\n     - }\n     - \n      @@ reflog-walk.c: int add_reflog_for_walk(struct reflog_walk_info *info,\n       \treturn 0;\n       }\n     @@ reflog-walk.c: void get_reflog_message(struct strbuf *sb,\n       \tstrbuf_add(sb, info->message, len);\n       }\n       \n     -+const char *get_reflog_branch(struct reflog_walk_info *reflog_info)\n     ++void get_reflog_branch(struct strbuf *sb,\n     ++\t\t       struct reflog_walk_info *reflog_info)\n      +{\n      +\tstruct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;\n     ++\tstruct complete_reflogs *reflogs;\n     ++\tconst char *branch;\n      +\n      +\tif (!commit_reflog)\n     -+\t\treturn NULL;\n     ++\t\treturn;\n      +\n     -+\tif (strcmp(commit_reflog->reflogs->ref, \"HEAD\"))\n     -+\t\treturn short_ref(commit_reflog->reflogs);\n     -+\treturn commit_reflog->reflogs->items[commit_reflog->recno+1].branch;\n     ++\treflogs = commit_reflog->reflogs;\n     ++\tif (strcmp(reflogs->ref, \"HEAD\")) {\n     ++\t\tstrbuf_addstr(sb, short_ref(reflogs));\n     ++\t\treturn;\n     ++\t}\n     ++\n     ++\tif (!reflogs->branches_marked) {\n     ++\t\tmark_branches(reflogs);\n     ++\t\treflogs->branches_marked = true;\n     ++\t}\n     ++\tbranch = reflogs->items[commit_reflog->recno+1].branch;\n     ++\tif (branch)\n     ++\t\tstrbuf_addstr(sb, branch);\n      +}\n      +\n       const char *get_reflog_ident(struct reflog_walk_info *reflog_info)\n       {\n       \tstruct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;\n     -@@ reflog-walk.c: timestamp_t get_reflog_timestamp(struct reflog_walk_info *reflog_info)\n     - }\n     - \n     - void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,\n     --\t\t\t struct date_mode dmode, int force_date)\n     -+\t\t\t struct date_mode dmode, int force_date,\n     -+\t\t\t const char *branch)\n     - {\n     - \tif (reflog_info && reflog_info->last_commit_reflog) {\n     - \t\tstruct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;\n     -@@ reflog-walk.c: void show_reflog_message(struct reflog_walk_info *reflog_info, int oneline,\n     - \t\tinfo = &commit_reflog->reflogs->items[commit_reflog->recno+1];\n     - \t\tget_reflog_selector(&selector, reflog_info, dmode, force_date, 0);\n     - \t\tif (oneline) {\n     --\t\t\tprintf(\"%s: %s\", selector.buf, info->message);\n     -+\t\t\tprintf(\"%s: %s%s\", selector.buf, branch,\n     -+\t\t\t       info->message);\n     - \t\t}\n     - \t\telse {\n     --\t\t\tprintf(\"Reflog: %s (%s)\\nReflog message: %s\",\n     --\t\t\t       selector.buf, info->email, info->message);\n     -+\t\t\tprintf(\"Reflog: %s (%s)\\nReflog message: %s%s\",\n     -+\t\t\t       selector.buf, info->email, branch,\n     -+\t\t\t       info->message);\n     - \t\t}\n     - \n     - \t\tstrbuf_release(&selector);\n      \n       ## reflog-walk.h ##\n     -@@ reflog-walk.h: struct date_mode;\n     - \n     - void init_reflog_walk(struct reflog_walk_info **info);\n     - void reflog_walk_info_release(struct reflog_walk_info *info);\n     --void reflog_walk_mark(struct reflog_walk_info *info, bool operations);\n     -+void reflog_walk_mark(struct reflog_walk_info *info, bool operations,\n     -+\t\t      bool historical_branch);\n     - int add_reflog_for_walk(struct reflog_walk_info *info,\n     - \t\t\tstruct commit *commit, const char *name);\n     - void show_reflog_message(struct reflog_walk_info *info, int,\n     --\t\t\t struct date_mode, int force_date);\n     -+\t\t\t struct date_mode, int force_date,\n     -+\t\t\t const char *branch);\n     +@@ reflog-walk.h: void show_reflog_message(struct reflog_walk_info *info, int,\n     + \t\t\t struct date_mode, int force_date);\n       void get_reflog_message(struct strbuf *sb,\n       \t\t\tstruct reflog_walk_info *reflog_info);\n     -+const char *get_reflog_branch(struct reflog_walk_info *reflog_info);\n     ++void get_reflog_branch(struct strbuf *sb,\n     ++\t\t       struct reflog_walk_info *reflog_info);\n       const char *get_reflog_ident(struct reflog_walk_info *reflog_info);\n       timestamp_t get_reflog_timestamp(struct reflog_walk_info *reflog_info);\n       void get_reflog_selector(struct strbuf *sb,\n      \n     - ## revision.c ##\n     -@@ revision.c: static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg\n     - \t\trevs->show_signature = 1;\n     - \t} else if (!strcmp(arg, \"--no-show-signature\")) {\n     - \t\trevs->show_signature = 0;\n     -+\t} else if (!strcmp(arg, \"--historical-branch\")) {\n     -+\t\trevs->historical_branch = 1;\n     - \t} else if (!strcmp(arg, \"--show-linear-break\")) {\n     - \t\trevs->break_bar = \"                    ..........\";\n     - \t\trevs->track_linear = 1;\n     -@@ revision.c: int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s\n     - \tif (!revs->reflog_info && revs->grep_filter.use_reflog_filter)\n     - \t\tdie(_(\"the option '%s' requires '%s'\"), \"--grep-reflog\", \"--walk-reflogs\");\n     - \n     -+\tif (!revs->reflog_info && revs->historical_branch)\n     -+\t\tdie(_(\"the option '%s' requires '%s'\"), \"--historical-branch\", \"--walk-reflogs\");\n     -+\n     - \tif (!revs->reflog_info && revs->reflog_operations)\n     - \t\tdie(_(\"the option '%s' requires '%s'\"), \"--operations\", \"--walk-reflogs\");\n     - \n     - \tif (revs->reflog_info)\n     --\t\treflog_walk_mark(revs->reflog_info, revs->reflog_operations);\n     -+\t\treflog_walk_mark(revs->reflog_info, revs->reflog_operations,\n     -+\t\t\t\t revs->historical_branch);\n     - \n     - \tif (revs->line_level_traverse &&\n     - \t    (revs->full_diff ||\n     -\n     - ## revision.h ##\n     -@@ revision.h: struct rev_info {\n     - \t\t\tshow_notes_given:1,\n     - \t\t\tshow_notes_by_default:1,\n     - \t\t\tshow_signature:1,\n     -+\t\t\thistorical_branch:1,\n     - \t\t\tpretty_given:1,\n     - \t\t\tabbrev_commit:1,\n     - \t\t\tabbrev_commit_given:1,\n     -\n       ## t/t1414-reflog-walk.sh ##\n     -@@ t/t1414-reflog-walk.sh: test_expect_success '--operations shows a rebase as one entry' '\n     - \t)\n     +@@ t/t1414-reflog-walk.sh: test_expect_success 'walk prefers reflog to ref tip' '\n     + \ttest_cmp expect actual\n       '\n       \n     -+test_expect_success '--historical-branch names the branch of each entry' '\n     ++test_expect_success '%gb names the branch HEAD was on' '\n     ++\tgit init branches &&\n      +\t(\n     -+\t\tcd operations &&\n     ++\t\tcd branches &&\n     ++\t\ttest_commit base &&\n     ++\t\tgit switch -c topic &&\n     ++\t\ttest_commit A &&\n     ++\t\ttest_commit B &&\n     ++\t\tgit switch main &&\n     ++\t\ttest_commit upstream &&\n     ++\t\tgit switch topic &&\n     ++\t\tgit rebase main &&\n      +\n      +\t\tcat >expect <<-\\EOF &&\n     -+\t\tHEAD@{0}: (topic) rebase (finish): returning to refs/heads/topic\n     -+\t\tHEAD@{1}: (topic) rebase (pick): C\n     -+\t\tHEAD@{2}: (topic) rebase (pick): B\n     -+\t\tHEAD@{3}: (topic) rebase (pick): A\n     -+\t\tHEAD@{4}: (topic) rebase (start): checkout main\n     -+\t\tHEAD@{5}: (topic) checkout: moving from main to topic\n     -+\t\tHEAD@{6}: (main) commit: upstream\n     -+\t\tHEAD@{7}: (main) checkout: moving from topic to main\n     -+\t\tHEAD@{8}: (topic) commit: C\n     -+\t\tHEAD@{9}: (topic) commit: B\n     -+\t\tHEAD@{10}: (topic) commit: A\n     -+\t\tHEAD@{11}: (topic) checkout: moving from main to topic\n     -+\t\tHEAD@{12}: (main) commit (initial): base\n     ++\t\t(topic) rebase (finish): returning to refs/heads/topic\n     ++\t\t(topic) rebase (pick): B\n     ++\t\t(topic) rebase (pick): A\n     ++\t\t(topic) rebase (start): checkout main\n     ++\t\t(topic) checkout: moving from main to topic\n     ++\t\t(main) commit: upstream\n     ++\t\t(main) checkout: moving from topic to main\n     ++\t\t(topic) commit: B\n     ++\t\t(topic) commit: A\n     ++\t\t(topic) checkout: moving from main to topic\n     ++\t\t(main) commit (initial): base\n      +\t\tEOF\n     -+\t\tgit reflog --historical-branch >out &&\n     -+\t\tcut -d \" \" -f 2- out >actual &&\n     ++\t\tgit log -g --format=\"(%gb) %gs\" >actual &&\n      +\t\ttest_cmp expect actual\n      +\t)\n      +'\n      +\n     -+test_expect_success '--historical-branch takes the place of decorations' '\n     ++test_expect_success '%gb names the ref of other reflogs' '\n      +\t(\n     -+\t\tcd operations &&\n     -+\t\thash=$(git rev-parse --short HEAD) &&\n     -+\t\techo \"<YELLOW>$hash<RESET> HEAD@{0}: <YELLOW>(<RESET><BOLD;GREEN>topic<RESET><YELLOW>)<RESET> rebase (finish): returning to refs/heads/topic\" >expect &&\n     -+\t\tgit reflog --historical-branch --decorate --color -1 >out &&\n     -+\t\ttest_decode_color <out >actual &&\n     ++\t\tcd branches &&\n     ++\t\techo main >expect &&\n     ++\t\tgit log -g -1 --format=%gb main >actual &&\n      +\t\ttest_cmp expect actual\n      +\t)\n      +'\n      +\n     -+test_expect_success '--historical-branch names the ref of other reflogs' '\n     ++test_expect_success '%gb follows \"git rebase <upstream> <branch>\"' '\n      +\t(\n     -+\t\tcd operations &&\n     -+\t\techo \"main@{0}: (main) commit: upstream\" >expect &&\n     -+\t\tgit reflog -1 main --historical-branch >out &&\n     -+\t\tcut -d \" \" -f 2- out >actual &&\n     ++\t\tcd branches &&\n     ++\t\tgit switch main &&\n     ++\t\ttest_commit main-change &&\n     ++\t\tgit rebase main topic &&\n     ++\t\tgit switch main &&\n     ++\t\tgit rebase main topic &&\n     ++\n     ++\t\tcat >expect <<-\\EOF &&\n     ++\t\t(topic) rebase: checkout topic\n     ++\t\t(main) checkout: moving from topic to main\n     ++\t\t(topic) rebase (finish): returning to refs/heads/topic\n     ++\t\t(topic) rebase (pick): B\n     ++\t\t(topic) rebase (pick): A\n     ++\t\t(topic) rebase (start): checkout main\n     ++\t\t(main) commit: main-change\n     ++\t\t(main) checkout: moving from topic to main\n     ++\t\tEOF\n     ++\t\tgit log -g -8 --format=\"(%gb) %gs\" >actual &&\n      +\t\ttest_cmp expect actual\n      +\t)\n      +'\n      +\n     - test_expect_success '--operations shows a stopped rebase as one entry once it finishes' '\n     - \tset_fake_editor &&\n     - \t(\n     -@@ t/t1414-reflog-walk.sh: test_expect_success '--operations shows a pull --rebase as one entry' '\n     - \t)\n     - '\n     - \n     --test_expect_success '--operations requires --walk-reflogs' '\n     -+test_expect_success '--historical-branch names the current branch when HEAD never switched' '\n     ++test_expect_success '%gb names a detached HEAD by the commit it was left at' '\n      +\t(\n     -+\t\tcd downstream &&\n     -+\t\techo \"(topic)\" >expect &&\n     -+\t\tgit reflog --historical-branch >out &&\n     -+\t\tcut -d \" \" -f 3 out | sort -u >actual &&\n     ++\t\tcd branches &&\n     ++\t\tgit switch --detach topic~1 &&\n     ++\t\ttest_commit detached &&\n     ++\t\tgit rebase topic &&\n     ++\t\trebased=$(git rev-parse HEAD) &&\n     ++\t\tgit switch topic &&\n     ++\n     ++\t\tleft_before=$(git rev-parse --short detached) &&\n     ++\t\tleft_after=$(git rev-parse --short $rebased) &&\n     ++\t\tcat >expect <<-EOF &&\n     ++\t\t(topic) checkout: moving from $rebased to topic\n     ++\t\t(detached at $left_after) rebase (pick): detached\n     ++\t\t(detached at $left_after) rebase (start): checkout topic\n     ++\t\t(detached at $left_before) commit: detached\n     ++\t\t(detached at $left_before) checkout: moving from topic to topic~1\n     ++\t\tEOF\n     ++\t\tgit log -g -5 --format=\"(%gb) %gs\" >actual &&\n      +\t\ttest_cmp expect actual\n      +\t)\n      +'\n      +\n     -+test_expect_success '--historical-branch follows a renamed branch' '\n     ++test_expect_success '%gb names the branch of a \"git pull --rebase\" in a fresh clone' '\n     ++\tgit clone branches downstream &&\n      +\t(\n     -+\t\tcd operations &&\n     -+\t\ttest_commit D &&\n     -+\t\tgit branch -m topic renamed &&\n     -+\t\ttest_commit E &&\n     ++\t\tcd branches &&\n     ++\t\ttest_commit upstream-change\n     ++\t) &&\n     ++\t(\n     ++\t\tcd downstream &&\n     ++\t\ttest_commit local-change &&\n     ++\t\tgit pull --rebase &&\n      +\n     -+\t\tcat >expect <<-\\EOF &&\n     -+\t\tHEAD@{0}: (renamed) commit: E\n     -+\t\tHEAD@{1}: (renamed) Branch: renamed refs/heads/topic to refs/heads/renamed\n     -+\t\tHEAD@{3}: (topic) commit: D\n     -+\t\tEOF\n     -+\t\tgit reflog --historical-branch -3 >out &&\n     -+\t\tcut -d \" \" -f 2- out >actual &&\n     ++\t\techo \"(topic)\" >expect &&\n     ++\t\tgit log -g --format=\"(%gb)\" >out &&\n     ++\t\tsort -u out >actual &&\n      +\t\ttest_cmp expect actual\n      +\t)\n      +'\n      +\n     -+test_expect_success '--historical-branch shows where HEAD was detached' '\n     ++test_expect_success '%gb follows a renamed branch' '\n      +\t(\n     -+\t\tcd operations &&\n     -+\t\tgit checkout --detach &&\n     -+\t\ttest_commit F &&\n     -+\t\tgit checkout renamed &&\n     ++\t\tcd branches &&\n     ++\t\ttest_commit before-rename &&\n     ++\t\tgit branch -m topic renamed &&\n     ++\t\ttest_commit after-rename &&\n      +\n     -+\t\tcat >expect <<-EOF &&\n     -+\t\tHEAD@{0}: (renamed) checkout: moving from $(git rev-parse F) to renamed\n     -+\t\tHEAD@{1}: (detached at $(git rev-parse --short F)) commit: F\n     ++\t\tcat >expect <<-\\EOF &&\n     ++\t\t(renamed) commit: after-rename\n     ++\t\t(renamed) Branch: renamed refs/heads/topic to refs/heads/renamed\n     ++\t\t(topic) commit: before-rename\n      +\t\tEOF\n     -+\t\tgit reflog --historical-branch -2 >out &&\n     -+\t\tcut -d \" \" -f 2- out >actual &&\n     ++\t\tgit log -g -3 --format=\"(%gb) %gs\" >actual &&\n      +\t\ttest_cmp expect actual\n      +\t)\n      +'\n      +\n     -+test_expect_success '--operations and --historical-branch require --walk-reflogs' '\n     - \ttest_must_fail git log --operations 2>err &&\n     -+\ttest_grep \"requires .--walk-reflogs.\" err &&\n     -+\ttest_must_fail git log --historical-branch 2>err &&\n     - \ttest_grep \"requires .--walk-reflogs.\" err\n     + test_expect_success 'rev-list -g complains when there are no reflogs' '\n     + \ttest_must_fail git rev-list -g\n       '\n     - \n\n-- \ngitgitgadget\n\n"},{"id":"556456","messageId":"4d779e8da37228567669af19fc0adbcf69adb13f.1793090327.git.gitgitgadget@gmail.com","threadId":"66545","inReplyTo":"pull.2448.v3.git.git.1793090327.gitgitgadget@gmail.com","subject":"[PATCH v3 1/2] reflog: name the strings that mark switches, renames and rebase steps","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-27T08:38:46Z","receivedAt":"2026-10-27T08:38:46Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nThe next commit adds a \"%gb\" placeholder that shows which branch HEAD\nwas on for each reflog entry. It finds the branch from the messages\nthat branch switches, branch renames and rebases leave in the reflog.\nThe commands that write these messages spell out the strings by hand,\nand \"@{-<n>}\" and \"git status\" each keep their own copy of the switch\nprefix to read it back. Reading them in one more place would add yet\nanother copy that has to stay in sync.\n\nName the strings once in reflog.h and use the names wherever the\nmessages are written or read: the \"checkout: moving from \" prefix\nwritten by \"git checkout\", the \"Branch: renamed \" prefix written by\n\"git branch -m\", and the \"start\", \"finish\" and \"abort\" steps, the\n\"returning to \" before the branch a rebase goes back to and the\n\"checkout \" before what it checks out, all written by \"git rebase\".\n\nNo change in behavior.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n builtin/branch.c   |  3 ++-\n builtin/checkout.c |  3 ++-\n builtin/rebase.c   | 19 ++++++++++---------\n object-name.c      |  3 ++-\n reflog.h           |  8 ++++++++\n sequencer.c        | 13 +++++++------\n wt-status.c        |  3 ++-\n 7 files changed, 33 insertions(+), 19 deletions(-)\n\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex a613148fc7..30062ceb7c 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -13,6 +13,7 @@\n #include \"editor.h\"\n #include \"environment.h\"\n #include \"refs.h\"\n+#include \"reflog.h\"\n #include \"commit.h\"\n #include \"gettext.h\"\n #include \"object-name.h\"\n@@ -669,7 +670,7 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int\n \t\tstrbuf_addf(&logmsg, \"Branch: copied %s to %s\",\n \t\t\t    oldref.buf, newref.buf);\n \telse\n-\t\tstrbuf_addf(&logmsg, \"Branch: renamed %s to %s\",\n+\t\tstrbuf_addf(&logmsg, REFLOG_RENAME_PREFIX \"%s to %s\",\n \t\t\t    oldref.buf, newref.buf);\n \n \tif (!copy && !(oldref_usage & IS_ORPHAN) &&\ndiff --git a/builtin/checkout.c b/builtin/checkout.c\nindex 91e0332ec2..1e06586e67 100644\n--- a/builtin/checkout.c\n+++ b/builtin/checkout.c\n@@ -26,6 +26,7 @@\n #include \"preload-index.h\"\n #include \"read-cache.h\"\n #include \"refs.h\"\n+#include \"reflog.h\"\n #include \"remote.h\"\n #include \"repo-settings.h\"\n #include \"resolve-undo.h\"\n@@ -1037,7 +1038,7 @@ static void update_refs_for_switch(const struct checkout_opts *opts,\n \n \treflog_msg = getenv(\"GIT_REFLOG_ACTION\");\n \tif (!reflog_msg)\n-\t\tstrbuf_addf(&msg, \"checkout: moving from %s to %s\",\n+\t\tstrbuf_addf(&msg, REFLOG_SWITCH_PREFIX \"%s to %s\",\n \t\t\told_desc ? old_desc : \"(invalid)\", new_branch_info->name);\n \telse\n \t\tstrbuf_insertstr(&msg, 0, reflog_msg);\ndiff --git a/builtin/rebase.c b/builtin/rebase.c\nindex 535db60181..47a7b2fd3c 100644\n--- a/builtin/rebase.c\n+++ b/builtin/rebase.c\n@@ -17,6 +17,7 @@\n #include \"strvec.h\"\n #include \"dir.h\"\n #include \"refs.h\"\n+#include \"reflog.h\"\n #include \"config.h\"\n #include \"unpack-trees.h\"\n #include \"lockfile.h\"\n@@ -601,11 +602,11 @@ static int move_to_original_branch(struct rebase_options *opts)\n \tif (!opts->onto)\n \t\tBUG(\"move_to_original_branch without onto\");\n \n-\tstrbuf_addf(&branch_reflog, \"%s (finish): %s onto %s\",\n-\t\t    opts->reflog_action,\n+\tstrbuf_addf(&branch_reflog, \"%s (%s): %s onto %s\",\n+\t\t    opts->reflog_action, REFLOG_STEP_FINISH,\n \t\t    opts->head_name, oid_to_hex(&opts->onto->object.oid));\n-\tstrbuf_addf(&head_reflog, \"%s (finish): returning to %s\",\n-\t\t    opts->reflog_action, opts->head_name);\n+\tstrbuf_addf(&head_reflog, \"%s (%s): \" REFLOG_RETURNING_TO \"%s\",\n+\t\t    opts->reflog_action, REFLOG_STEP_FINISH, opts->head_name);\n \tropts.branch = opts->head_name;\n \tropts.flags = RESET_WORKING_TREE_REFS_ONLY |\n \t\t      RESET_WORKING_TREE_UPDATE_HEAD;\n@@ -867,7 +868,7 @@ static int checkout_up_to_date(struct rebase_options *options)\n \tstruct reset_working_tree_options ropts = { 0 };\n \tint ret = 0;\n \n-\tstrbuf_addf(&buf, \"%s: checkout %s\",\n+\tstrbuf_addf(&buf, \"%s: \" REFLOG_CHECKOUT \"%s\",\n \t\t    options->reflog_action, options->switch_to);\n \tropts.oid = &options->orig_head->object.oid;\n \tropts.branch = options->head_name;\n@@ -1413,8 +1414,8 @@ int cmd_rebase(int argc,\n \t\tif (read_basic_state(&options))\n \t\t\texit(1);\n \n-\t\tstrbuf_addf(&head_msg, \"%s (abort): returning to %s\",\n-\t\t\t    options.reflog_action,\n+\t\tstrbuf_addf(&head_msg, \"%s (%s): \" REFLOG_RETURNING_TO \"%s\",\n+\t\t\t    options.reflog_action, REFLOG_STEP_ABORT,\n \t\t\t    options.head_name ? options.head_name\n \t\t\t\t\t      : oid_to_hex(&options.orig_head->object.oid));\n \t\tropts.oid = &options.orig_head->object.oid;\n@@ -1884,8 +1885,8 @@ int cmd_rebase(int argc,\n \t\tprintf(_(\"First, rewinding head to replay your work on top of \"\n \t\t\t \"it...\\n\"));\n \n-\tstrbuf_addf(&msg, \"%s (start): checkout %s\",\n-\t\t    options.reflog_action, options.onto_name);\n+\tstrbuf_addf(&msg, \"%s (%s): \" REFLOG_CHECKOUT \"%s\",\n+\t\t    options.reflog_action, REFLOG_STEP_START, options.onto_name);\n \tropts.oid = &options.onto->object.oid;\n \tropts.orig_head = &options.orig_head->object.oid;\n \tropts.flags = RESET_WORKING_TREE_DETACH |\ndiff --git a/object-name.c b/object-name.c\nindex 6546685760..5b6ce8e0e0 100644\n--- a/object-name.c\n+++ b/object-name.c\n@@ -13,6 +13,7 @@\n #include \"tree.h\"\n #include \"tree-walk.h\"\n #include \"refs.h\"\n+#include \"reflog.h\"\n #include \"remote.h\"\n #include \"dir.h\"\n #include \"odb.h\"\n@@ -1251,7 +1252,7 @@ static int grab_nth_branch_switch(const char *refname UNUSED,\n \tconst char *match = NULL, *target = NULL;\n \tsize_t len;\n \n-\tif (skip_prefix(message, \"checkout: moving from \", &match))\n+\tif (skip_prefix(message, REFLOG_SWITCH_PREFIX, &match))\n \t\ttarget = strstr(match, \" to \");\n \n \tif (!match || !target)\ndiff --git a/reflog.h b/reflog.h\nindex 3bdd1ca8a5..f5586242bf 100644\n--- a/reflog.h\n+++ b/reflog.h\n@@ -2,6 +2,14 @@\n #define REFLOG_H\n #include \"refs.h\"\n \n+#define REFLOG_SWITCH_PREFIX \"checkout: moving from \"\n+#define REFLOG_RENAME_PREFIX \"Branch: renamed \"\n+#define REFLOG_STEP_START \"start\"\n+#define REFLOG_STEP_FINISH \"finish\"\n+#define REFLOG_STEP_ABORT \"abort\"\n+#define REFLOG_RETURNING_TO \"returning to \"\n+#define REFLOG_CHECKOUT \"checkout \"\n+\n #define REFLOG_EXPIRE_TOTAL   (1 << 0)\n #define REFLOG_EXPIRE_UNREACH (1 << 1)\n \ndiff --git a/sequencer.c b/sequencer.c\nindex 2ac78e15b8..b2c6d8072a 100644\n--- a/sequencer.c\n+++ b/sequencer.c\n@@ -31,6 +31,7 @@\n #include \"merge-ort.h\"\n #include \"merge-ort-wrappers.h\"\n #include \"refs.h\"\n+#include \"reflog.h\"\n #include \"sparse-index.h\"\n #include \"strvec.h\"\n #include \"quote.h\"\n@@ -5011,8 +5012,8 @@ static int checkout_onto(struct repository *r, struct replay_opts *opts,\n \t\t\t RESET_WORKING_TREE_UPDATE_HEAD |\n \t\t\t RESET_WORKING_TREE_UPDATE_ORIG_HEAD |\n \t\t\t RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK,\n-\t\t.head_msg = reflog_message(opts, \"start\", \"checkout %s\",\n-\t\t\t\t\t   onto_name),\n+\t\t.head_msg = reflog_message(opts, REFLOG_STEP_START,\n+\t\t\t\t\t   REFLOG_CHECKOUT \"%s\", onto_name),\n \t\t.default_reflog_action = sequencer_reflog_action(opts)\n \t};\n \tif (reset_working_tree(r, &ropts)) {\n@@ -5311,16 +5312,16 @@ cleanup_head_ref:\n \t\t\t\tres = error(_(\"could not read 'onto'\"));\n \t\t\t\tgoto cleanup_head_ref;\n \t\t\t}\n-\t\t\tmsg = reflog_message(opts, \"finish\", \"%s onto %s\",\n-\t\t\t\thead_ref.buf, buf.buf);\n+\t\t\tmsg = reflog_message(opts, REFLOG_STEP_FINISH,\n+\t\t\t\t\"%s onto %s\", head_ref.buf, buf.buf);\n \t\t\tif (refs_update_ref(get_main_ref_store(the_repository), msg, head_ref.buf, &head, &orig,\n \t\t\t\t\t    REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) {\n \t\t\t\tres = error(_(\"could not update %s\"),\n \t\t\t\t\thead_ref.buf);\n \t\t\t\tgoto cleanup_head_ref;\n \t\t\t}\n-\t\t\tmsg = reflog_message(opts, \"finish\", \"returning to %s\",\n-\t\t\t\thead_ref.buf);\n+\t\t\tmsg = reflog_message(opts, REFLOG_STEP_FINISH,\n+\t\t\t\tREFLOG_RETURNING_TO \"%s\", head_ref.buf);\n \t\t\tif (refs_update_symref(get_main_ref_store(the_repository), \"HEAD\", head_ref.buf, msg)) {\n \t\t\t\tres = error(_(\"could not update HEAD to %s\"),\n \t\t\t\t\thead_ref.buf);\ndiff --git a/wt-status.c b/wt-status.c\nindex 238bb48643..3516432338 100644\n--- a/wt-status.c\n+++ b/wt-status.c\n@@ -22,6 +22,7 @@\n #include \"strvec.h\"\n #include \"remote.h\"\n #include \"refs.h\"\n+#include \"reflog.h\"\n #include \"submodule.h\"\n #include \"column.h\"\n #include \"read-cache.h\"\n@@ -1790,7 +1791,7 @@ static int grab_1st_switch(const char *refname UNUSED,\n \tstruct grab_1st_switch_cbdata *cb = cb_data;\n \tconst char *target = NULL, *end;\n \n-\tif (!skip_prefix(message, \"checkout: moving from \", &message))\n+\tif (!skip_prefix(message, REFLOG_SWITCH_PREFIX, &message))\n \t\treturn 0;\n \ttarget = strstr(message, \" to \");\n \tif (!target)\n-- \ngitgitgadget\n\n\n"},{"id":"556457","messageId":"dee548f9b1ca11feef2d74d0384c5c3c2e6c20d7.1793090327.git.gitgitgadget@gmail.com","threadId":"66545","inReplyTo":"pull.2448.v3.git.git.1793090327.gitgitgadget@gmail.com","subject":"[PATCH v3 2/2] pretty: add %gb to name the branch of each reflog entry","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-10-27T08:38:47Z","receivedAt":"2026-10-27T08:38:47Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nThe reflog of HEAD records the work done on every branch, so the\noutput of \"git reflog\" mixes them and does not say which entry was\nmade on which branch. Working that out by hand means looking back for\nthe branch switch before each entry, and a rebase can move HEAD to\nanother branch without writing one, as \"git rebase <upstream>\n<branch>\" does.\n\nAdd a \"%gb\" placeholder that names the branch HEAD was on when each\nentry was recorded:\n\n    $ git log -g --format='%h %gd: (%gb) %gs'\n    9c8d7e6 HEAD@{0}: (topic) rebase (finish): returning to refs/heads/topic\n    9c8d7e6 HEAD@{1}: (topic) rebase (pick): add tests\n    1a2b3c4 HEAD@{2}: (topic) rebase (start): checkout main\n    1a2b3c4 HEAD@{3}: (main) commit: fix typo\n    5d6e7f8 HEAD@{4}: (main) checkout: moving from topic to main\n\nThe branch is read from the entries that branch switches, branch\nrenames and rebases leave behind. The entries of a rebase are named\nafter the branch being rebased. When HEAD was detached, the entry\nshows \"detached at <commit>\" with the commit HEAD was at when it\nmoved on. When nothing older in the reflog says which branch a rebase\nstarted on, the rebase is taken to have started on the branch it\nrebased. For the reflog of any other ref, \"%gb\" is the name of that\nref.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/pretty-formats.adoc |   3 +\n pretty.c                          |   4 +\n reflog-walk.c                     | 156 ++++++++++++++++++++++++++++--\n reflog-walk.h                     |   2 +\n t/t1414-reflog-walk.sh            | 122 +++++++++++++++++++++++\n 5 files changed, 281 insertions(+), 6 deletions(-)\n\ndiff --git a/Documentation/pretty-formats.adoc b/Documentation/pretty-formats.adoc\nindex 2ae0eb11a9..a8d00397c4 100644\n--- a/Documentation/pretty-formats.adoc\n+++ b/Documentation/pretty-formats.adoc\n@@ -334,6 +334,9 @@ endif::git-rev-list[]\n +%gE+:: reflog identity email (respecting .mailmap, see\n \tlinkgit:git-shortlog[1] or linkgit:git-blame[1])\n +%gs+:: reflog subject\n++%gb+:: reflog branch, the branch `HEAD` was on when the entry was\n+\trecorded, e.g., `topic` or `detached at 1a2b3c4`. For the reflog\n+\tof any other ref, this is the shortened refname.\n ++%(trailers++`[:<option>,...]`++)++::\n display the trailers of the body as interpreted by\n linkgit:git-interpret-trailers[1]. The `trailers` string may be followed by\ndiff --git a/pretty.c b/pretty.c\nindex d8a9f370f6..1b3f7e2dc3 100644\n--- a/pretty.c\n+++ b/pretty.c\n@@ -1637,6 +1637,10 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */\n \t\t\tif (c->pretty_ctx->reflog_info)\n \t\t\t\tget_reflog_message(sb, c->pretty_ctx->reflog_info);\n \t\t\treturn 2;\n+\t\tcase 'b':\t/* reflog branch */\n+\t\t\tif (c->pretty_ctx->reflog_info)\n+\t\t\t\tget_reflog_branch(sb, c->pretty_ctx->reflog_info);\n+\t\t\treturn 2;\n \t\tcase 'n':\n \t\tcase 'N':\n \t\tcase 'e':\ndiff --git a/reflog-walk.c b/reflog-walk.c\nindex 4dbeaa93a7..45b5dc09b5 100644\n--- a/reflog-walk.c\n+++ b/reflog-walk.c\n@@ -3,7 +3,10 @@\n #include \"git-compat-util.h\"\n #include \"commit.h\"\n #include \"refs.h\"\n+#include \"reflog.h\"\n #include \"diff.h\"\n+#include \"hex.h\"\n+#include \"object-name.h\"\n #include \"repository.h\"\n #include \"revision.h\"\n #include \"string-list.h\"\n@@ -18,8 +21,10 @@ struct complete_reflogs {\n \t\ttimestamp_t timestamp;\n \t\tint tz;\n \t\tchar *message;\n+\t\tchar *branch;\n \t} *items;\n \tint nr, alloc;\n+\tbool branches_marked;\n };\n \n static int read_one_reflog(const char *refname UNUSED,\n@@ -38,6 +43,7 @@ static int read_one_reflog(const char *refname UNUSED,\n \titem->timestamp = timestamp;\n \titem->tz = tz;\n \titem->message = xstrdup(message);\n+\titem->branch = NULL;\n \tarray->nr++;\n \treturn 0;\n }\n@@ -52,6 +58,7 @@ static void free_complete_reflog(struct complete_reflogs *array)\n \tfor (i = 0; i < array->nr; i++) {\n \t\tfree(array->items[i].email);\n \t\tfree(array->items[i].message);\n+\t\tfree(array->items[i].branch);\n \t}\n \tfree(array->items);\n \tfree(array->ref);\n@@ -65,6 +72,113 @@ static void complete_reflogs_clear(void *util, const char *str UNUSED)\n \tfree_complete_reflog(array);\n }\n \n+static char *branch_label(const char *name, size_t len,\n+\t\t\t  const struct object_id *oid)\n+{\n+\tif (name) {\n+\t\tstruct object_id hex_oid;\n+\t\tchar *branch;\n+\n+\t\tif (skip_prefix(name, \"refs/heads/\", &name))\n+\t\t\tlen -= strlen(\"refs/heads/\");\n+\t\tbranch = xmemdupz(name, len);\n+\t\tif (strcmp(branch, \"HEAD\") && get_oid_hex(branch, &hex_oid) &&\n+\t\t    !check_refname_format(branch, REFNAME_ALLOW_ONELEVEL))\n+\t\t\treturn branch;\n+\t\tfree(branch);\n+\t}\n+\treturn xstrfmt(\"detached at %s\",\n+\t\t       repo_find_unique_abbrev(the_repository, oid,\n+\t\t\t\t\t       DEFAULT_ABBREV));\n+}\n+\n+static const char *skip_rebase_step(const char *message, const char *step)\n+{\n+\tconst char *p = strstr(message, \" (\");\n+\tconst char *colon = strstr(message, \": \");\n+\n+\tif (!p || (colon && colon < p) ||\n+\t    !skip_prefix(p + 2, step, &p) || !skip_prefix(p, \"): \", &p))\n+\t\treturn NULL;\n+\treturn p;\n+}\n+\n+struct branch_move {\n+\tconst char *from, *to;\n+\tsize_t from_len, to_len;\n+};\n+\n+static bool parse_branch_move(const char *message, struct branch_move *move)\n+{\n+\tconst char *to;\n+\n+\tmove->from = move->to = NULL;\n+\tmove->from_len = move->to_len = 0;\n+\tif (skip_prefix(message, REFLOG_SWITCH_PREFIX, &move->from) ||\n+\t    skip_prefix(message, REFLOG_RENAME_PREFIX, &move->from)) {\n+\t\tto = strstr(move->from, \" to \");\n+\t\tif (!to)\n+\t\t\treturn false;\n+\t\tmove->from_len = to - move->from;\n+\t\tmove->to = to + strlen(\" to \");\n+\t} else if (((to = skip_rebase_step(message, REFLOG_STEP_FINISH)) ||\n+\t\t    (to = skip_rebase_step(message, REFLOG_STEP_ABORT))) &&\n+\t\t   skip_prefix(to, REFLOG_RETURNING_TO, &to)) {\n+\t\tmove->from = move->to = to;\n+\t\tmove->from_len = strcspn(to, \"\\n\");\n+\t} else if (skip_prefix(message, \"rebase: \" REFLOG_CHECKOUT, &to)) {\n+\t\tmove->to = to;\n+\t} else if (!skip_rebase_step(message, REFLOG_STEP_START)) {\n+\t\treturn false;\n+\t}\n+\tif (move->to)\n+\t\tmove->to_len = strcspn(move->to, \"\\n\");\n+\treturn true;\n+}\n+\n+static void mark_branches(struct complete_reflogs *array)\n+{\n+\tstruct ref_store *refs = get_main_ref_store(the_repository);\n+\tstruct object_id oid;\n+\tconst char *head = refs_resolve_ref_unsafe(refs, \"HEAD\", 0, &oid, NULL);\n+\tchar *branch = NULL;\n+\tint unknown = array->nr;\n+\n+\tif (head)\n+\t\tbranch = branch_label(head, strlen(head), &oid);\n+\n+\tfor (int i = array->nr - 1; i >= 0; i--) {\n+\t\tstruct reflog_info *item = &array->items[i];\n+\t\tstruct branch_move move;\n+\n+\t\tif (!parse_branch_move(item->message, &move)) {\n+\t\t\titem->branch = xstrdup_or_null(branch);\n+\t\t\tcontinue;\n+\t\t}\n+\n+\t\tif (!branch) {\n+\t\t\tbranch = branch_label(move.to, move.to_len,\n+\t\t\t\t\t      &array->items[unknown - 1].noid);\n+\t\t\tfor (int j = i + 1; j < unknown; j++)\n+\t\t\t\tarray->items[j].branch = xstrdup(branch);\n+\t\t}\n+\t\titem->branch = branch;\n+\t\tif (move.from) {\n+\t\t\tbranch = branch_label(move.from, move.from_len,\n+\t\t\t\t\t      &item->ooid);\n+\t\t} else {\n+\t\t\tbranch = NULL;\n+\t\t\tunknown = i;\n+\t\t}\n+\t}\n+\n+\tif (!branch && unknown < array->nr)\n+\t\tfor (int i = 0; i < unknown; i++)\n+\t\t\tarray->items[i].branch =\n+\t\t\t\txstrdup(array->items[unknown].branch);\n+\tfree(branch);\n+}\n+\n static struct complete_reflogs *read_complete_reflog(const char *ref)\n {\n \tstruct complete_reflogs *reflogs =\n@@ -234,6 +348,16 @@ int add_reflog_for_walk(struct reflog_walk_info *info,\n \treturn 0;\n }\n \n+static const char *short_ref(struct complete_reflogs *reflogs)\n+{\n+\tstruct ref_store *refs = get_main_ref_store(the_repository);\n+\n+\tif (!reflogs->short_ref)\n+\t\treflogs->short_ref =\n+\t\t\trefs_shorten_unambiguous_ref(refs, reflogs->ref, 0);\n+\treturn reflogs->short_ref;\n+}\n+\n void get_reflog_selector(struct strbuf *sb,\n \t\t\t struct reflog_walk_info *reflog_info,\n \t\t\t struct date_mode dmode, int force_date,\n@@ -247,12 +371,7 @@ void get_reflog_selector(struct strbuf *sb,\n \t\treturn;\n \n \tif (shorten) {\n-\t\tif (!commit_reflog->reflogs->short_ref)\n-\t\t\tcommit_reflog->reflogs->short_ref\n-\t\t\t\t= refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t\t       commit_reflog->reflogs->ref,\n-\t\t\t\t\t\t\t       0);\n-\t\tprinted_ref = commit_reflog->reflogs->short_ref;\n+\t\tprinted_ref = short_ref(commit_reflog->reflogs);\n \t} else {\n \t\tprinted_ref = commit_reflog->reflogs->ref;\n \t}\n@@ -287,6 +406,31 @@ void get_reflog_message(struct strbuf *sb,\n \tstrbuf_add(sb, info->message, len);\n }\n \n+void get_reflog_branch(struct strbuf *sb,\n+\t\t       struct reflog_walk_info *reflog_info)\n+{\n+\tstruct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;\n+\tstruct complete_reflogs *reflogs;\n+\tconst char *branch;\n+\n+\tif (!commit_reflog)\n+\t\treturn;\n+\n+\treflogs = commit_reflog->reflogs;\n+\tif (strcmp(reflogs->ref, \"HEAD\")) {\n+\t\tstrbuf_addstr(sb, short_ref(reflogs));\n+\t\treturn;\n+\t}\n+\n+\tif (!reflogs->branches_marked) {\n+\t\tmark_branches(reflogs);\n+\t\treflogs->branches_marked = true;\n+\t}\n+\tbranch = reflogs->items[commit_reflog->recno+1].branch;\n+\tif (branch)\n+\t\tstrbuf_addstr(sb, branch);\n+}\n+\n const char *get_reflog_ident(struct reflog_walk_info *reflog_info)\n {\n \tstruct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;\ndiff --git a/reflog-walk.h b/reflog-walk.h\nindex 989583dc55..fc1f344ce6 100644\n--- a/reflog-walk.h\n+++ b/reflog-walk.h\n@@ -13,6 +13,8 @@ void show_reflog_message(struct reflog_walk_info *info, int,\n \t\t\t struct date_mode, int force_date);\n void get_reflog_message(struct strbuf *sb,\n \t\t\tstruct reflog_walk_info *reflog_info);\n+void get_reflog_branch(struct strbuf *sb,\n+\t\t       struct reflog_walk_info *reflog_info);\n const char *get_reflog_ident(struct reflog_walk_info *reflog_info);\n timestamp_t get_reflog_timestamp(struct reflog_walk_info *reflog_info);\n void get_reflog_selector(struct strbuf *sb,\ndiff --git a/t/t1414-reflog-walk.sh b/t/t1414-reflog-walk.sh\nindex be6c3f472c..0e821922ca 100755\n--- a/t/t1414-reflog-walk.sh\n+++ b/t/t1414-reflog-walk.sh\n@@ -131,6 +131,128 @@ test_expect_success 'walk prefers reflog to ref tip' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success '%gb names the branch HEAD was on' '\n+\tgit init branches &&\n+\t(\n+\t\tcd branches &&\n+\t\ttest_commit base &&\n+\t\tgit switch -c topic &&\n+\t\ttest_commit A &&\n+\t\ttest_commit B &&\n+\t\tgit switch main &&\n+\t\ttest_commit upstream &&\n+\t\tgit switch topic &&\n+\t\tgit rebase main &&\n+\n+\t\tcat >expect <<-\\EOF &&\n+\t\t(topic) rebase (finish): returning to refs/heads/topic\n+\t\t(topic) rebase (pick): B\n+\t\t(topic) rebase (pick): A\n+\t\t(topic) rebase (start): checkout main\n+\t\t(topic) checkout: moving from main to topic\n+\t\t(main) commit: upstream\n+\t\t(main) checkout: moving from topic to main\n+\t\t(topic) commit: B\n+\t\t(topic) commit: A\n+\t\t(topic) checkout: moving from main to topic\n+\t\t(main) commit (initial): base\n+\t\tEOF\n+\t\tgit log -g --format=\"(%gb) %gs\" >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '%gb names the ref of other reflogs' '\n+\t(\n+\t\tcd branches &&\n+\t\techo main >expect &&\n+\t\tgit log -g -1 --format=%gb main >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '%gb follows \"git rebase <upstream> <branch>\"' '\n+\t(\n+\t\tcd branches &&\n+\t\tgit switch main &&\n+\t\ttest_commit main-change &&\n+\t\tgit rebase main topic &&\n+\t\tgit switch main &&\n+\t\tgit rebase main topic &&\n+\n+\t\tcat >expect <<-\\EOF &&\n+\t\t(topic) rebase: checkout topic\n+\t\t(main) checkout: moving from topic to main\n+\t\t(topic) rebase (finish): returning to refs/heads/topic\n+\t\t(topic) rebase (pick): B\n+\t\t(topic) rebase (pick): A\n+\t\t(topic) rebase (start): checkout main\n+\t\t(main) commit: main-change\n+\t\t(main) checkout: moving from topic to main\n+\t\tEOF\n+\t\tgit log -g -8 --format=\"(%gb) %gs\" >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '%gb names a detached HEAD by the commit it was left at' '\n+\t(\n+\t\tcd branches &&\n+\t\tgit switch --detach topic~1 &&\n+\t\ttest_commit detached &&\n+\t\tgit rebase topic &&\n+\t\trebased=$(git rev-parse HEAD) &&\n+\t\tgit switch topic &&\n+\n+\t\tleft_before=$(git rev-parse --short detached) &&\n+\t\tleft_after=$(git rev-parse --short $rebased) &&\n+\t\tcat >expect <<-EOF &&\n+\t\t(topic) checkout: moving from $rebased to topic\n+\t\t(detached at $left_after) rebase (pick): detached\n+\t\t(detached at $left_after) rebase (start): checkout topic\n+\t\t(detached at $left_before) commit: detached\n+\t\t(detached at $left_before) checkout: moving from topic to topic~1\n+\t\tEOF\n+\t\tgit log -g -5 --format=\"(%gb) %gs\" >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '%gb names the branch of a \"git pull --rebase\" in a fresh clone' '\n+\tgit clone branches downstream &&\n+\t(\n+\t\tcd branches &&\n+\t\ttest_commit upstream-change\n+\t) &&\n+\t(\n+\t\tcd downstream &&\n+\t\ttest_commit local-change &&\n+\t\tgit pull --rebase &&\n+\n+\t\techo \"(topic)\" >expect &&\n+\t\tgit log -g --format=\"(%gb)\" >out &&\n+\t\tsort -u out >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '%gb follows a renamed branch' '\n+\t(\n+\t\tcd branches &&\n+\t\ttest_commit before-rename &&\n+\t\tgit branch -m topic renamed &&\n+\t\ttest_commit after-rename &&\n+\n+\t\tcat >expect <<-\\EOF &&\n+\t\t(renamed) commit: after-rename\n+\t\t(renamed) Branch: renamed refs/heads/topic to refs/heads/renamed\n+\t\t(topic) commit: before-rename\n+\t\tEOF\n+\t\tgit log -g -3 --format=\"(%gb) %gs\" >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n test_expect_success 'rev-list -g complains when there are no reflogs' '\n \ttest_must_fail git rev-list -g\n '\n-- \ngitgitgadget\n\n"},{"id":"556507","messageId":"B734A6B4-CEA6-4CF2-8BA0-A4D8CC4125C1@gmail.com","threadId":"66545","inReplyTo":"1bc5a05e-3d3b-4f56-8b11-2e276cef48ca@gmail.com","subject":"Re: [PATCH v2 0/3] reflog: add --operations and --historical-branch","fromName":"Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-10-27T13:37:16Z","receivedAt":"2026-10-27T13:37:16Z","isPatch":true,"sender":{"key":"ben.knoble@gmail.com","avatar":"https://avatars.githubusercontent.com/u/22802209?v=4"},"body":"\n> Le 26 oct. 2026 à 06:53, Phillip Wood <phillip.wood123@gmail.com> a écrit :\n> \n> ﻿On 24/10/2026 17:40, Junio C Hamano wrote:\n>> Phillip Wood <phillip.wood123@gmail.com> writes:\n>>> I'm rather skeptical of this in its current form. Not all commands that\n>>> modify the working tree update the reflog and some commands such as \"git\n>>> cherry-pick A B C\" create multiple reflog entires that are\n>>> indistinguishable from multiple invocations of a single command.\n>> I like the direction to make the default 'git reflog' output on a\n>> single branch easier to understand without needing any extra\n>> filtering in this series.\n>> 'git rebase' doing its work on a detached HEAD was likely the\n>> first, and we may want to convert more multi-step commands to do\n>> so, including the multiple 'git cherry-pick' you mention.\n> \n> It would certainly be nice to avoid creating lots of entries in the branch reflog when cherry-picking. I'm not sure what users would make of us switching to a detached HEAD as some people seem to find it confusing; if we do decide to do that we should make sure we update \"git status\" to explain what's happening. I'm more relaxed about changing \"git am\", but like you I normally detach HEAD first so maybe I'm not the best person to ask about that.\n\nI sometimes forget that Git detaches HEAD while rebasing! \nThe prompt script shows the branch info, and git status (I think?)\ndoesn’t indicate it.\n\nAll this to say, I think rebase’s presentation of a detached HEAD\nthat is somewhat « hidden » might make such a change more\ninvisible to cherry-pickers?\n"},{"id":"556508","messageId":"C1614059-9903-4F95-8570-90EC5DEFAE8F@gmail.com","threadId":"66545","inReplyTo":"xmqq1p9cv6tp.fsf@gitster.g","subject":"Re: [PATCH v2 0/3] reflog: add --operations and --historical-branch","fromName":"Ben Knoble","fromEmail":"ben.knoble@gmail.com","sentAt":"2026-10-27T13:40:32Z","receivedAt":"2026-10-27T13:40:32Z","isPatch":true,"sender":{"key":"ben.knoble@gmail.com","avatar":"https://avatars.githubusercontent.com/u/22802209?v=4"},"body":"\n> Le 26 oct. 2026 à 11:07, Junio C Hamano <gitster@pobox.com> a écrit :\n> \n> ﻿Harald Nordgren <haraldnordgren@gmail.com> writes:\n> \n>>> It would certainly be nice to avoid creating lots of entries in the\n>>> branch reflog when cherry-picking. I'm not sure what users would make of\n>>> us switching to a detached HEAD as some people seem to find it\n>>> confusing; if we do decide to do that we should make sure we update \"git\n>>> status\" to explain what's happening. I'm more relaxed about changing\n>>> \"git am\", but like you I normally detach HEAD first so maybe I'm not the\n>>> best person to ask about that.\n>> I think that the fact that all of us have our tricks to work around\n>> things is indicative of a real problem.\n>> We should attack the root cause and simplify git so that even us would\n>> prefer vanilla git.\n> \n> I do use and prefer vanilla Git as it was designed to work, though.\n\nBut you also workaround limitations in am by detaching ;)\nwhich I think was Harald’s bigger point: let’s eliminate the need for such. \n"},{"id":"556563","messageId":"xmqqcxsvj6au.fsf@gitster.g","threadId":"66545","inReplyTo":"C1614059-9903-4F95-8570-90EC5DEFAE8F@gmail.com","subject":"Re: [PATCH v2 0/3] reflog: add --operations and --historical-branch","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-27T19:24:09Z","receivedAt":"2026-10-27T19:24:09Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Ben Knoble <ben.knoble@gmail.com> writes:\n\n>> I do use and prefer vanilla Git as it was designed to work, though.\n>\n> But you also workaround limitations in am by detaching ;)\n\nNot at all.  That is not a limitation.  It is a flexibility.  I do\napply patches directly on my branches when that is the behaviou I\nwant.\n\n\n"},{"id":"556567","messageId":"CAHwyqnW=bKY2SHMgMNav0gK+dOA4gUv5rm-=CvOeVZYTHCN4JA@mail.gmail.com","threadId":"66545","inReplyTo":"C1614059-9903-4F95-8570-90EC5DEFAE8F@gmail.com","subject":"Re: [PATCH v2 0/3] reflog: add --operations and --historical-branch","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-10-27T19:33:39Z","receivedAt":"2026-10-27T19:33:39Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"On Tue, Oct 27, 2026 at 3:40 PM Ben Knoble <ben.knoble@gmail.com> wrote:\n>\n>\n> > Le 26 oct. 2026 à 11:07, Junio C Hamano <gitster@pobox.com> a écrit :\n> >\n> > ﻿Harald Nordgren <haraldnordgren@gmail.com> writes:\n> >\n> >>> It would certainly be nice to avoid creating lots of entries in the\n> >>> branch reflog when cherry-picking. I'm not sure what users would make of\n> >>> us switching to a detached HEAD as some people seem to find it\n> >>> confusing; if we do decide to do that we should make sure we update \"git\n> >>> status\" to explain what's happening. I'm more relaxed about changing\n> >>> \"git am\", but like you I normally detach HEAD first so maybe I'm not the\n> >>> best person to ask about that.\n> >> I think that the fact that all of us have our tricks to work around\n> >> things is indicative of a real problem.\n> >> We should attack the root cause and simplify git so that even us would\n> >> prefer vanilla git.\n> >\n> > I do use and prefer vanilla Git as it was designed to work, though.\n>\n> But you also workaround limitations in am by detaching ;)\n> which I think was Harald’s bigger point: let’s eliminate the need for such.\n\nYes, on the bigger picture.\n\nThe specific example I had in mind was using 'git reflog @{0}', which\nI never knew about and will use from now on. I'm looking into maybe\nmaking this the default as well.\n\n\nHarald\n\n"},{"id":"557332","messageId":"49144eb0-9755-4843-930e-ae7b3b519080@gmail.com","threadId":"66545","inReplyTo":"dee548f9b1ca11feef2d74d0384c5c3c2e6c20d7.1793090327.git.gitgitgadget@gmail.com","subject":"Re: [PATCH v3 2/2] pretty: add %gb to name the branch of each reflog entry","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-11-03T16:16:54Z","receivedAt":"2026-11-03T16:16:54Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Harald\n\nOn 27/10/2026 08:38, Harald Nordgren via GitGitGadget wrote:\n> From: Harald Nordgren <haraldnordgren@gmail.com>\n> > Add a \"%gb\" placeholder that names the branch HEAD was on when each\n> entry was recorded:\n> >      $ git log -g --format='%h %gd: (%gb) %gs'\n>      9c8d7e6 HEAD@{0}: (topic) rebase (finish): returning to refs/heads/topic\n>      9c8d7e6 HEAD@{1}: (topic) rebase (pick): add tests\n>      1a2b3c4 HEAD@{2}: (topic) rebase (start): checkout main\n>      1a2b3c4 HEAD@{3}: (main) commit: fix typo\n>      5d6e7f8 HEAD@{4}: (main) checkout: moving from topic to main\n\nLooks good\n\n> The branch is read from the entries that branch switches, branch\n> renames and rebases leave behind.\n\n\"the entries created by ...\"?\n> The entries of a rebase are named\n> after the branch being rebased.\n\nThis sounds very LLM, \"labelled with the branch being rebased\"?\n\n> When HEAD was detached, the entry\n> shows \"detached at <commit>\" with the commit HEAD was at when it\n> moved on.\n\nI wondered if that was a bit verbose, but having tried it, it is no longer than some of my branch names.\n\n> When nothing older in the reflog says which branch a rebase\n> started on, the rebase is taken to have started on the branch it\n> rebased. For the reflog of any other ref, \"%gb\" is the name of that\n> ref.\n\nI'm seeing some strange behavior when GIT_REFLOG_ACTION is set, below is the output of \"git reflog --format='%h %gd %gs (%gb)'\" for my local HEAD. I've added comments above the lines where the branch name is incorrect. The \"sync switch\" entries come from running \"git rebase\" with GIT_REFLOG_ACTION set.\n\nbe8e0d55ec HEAD@{0} reset: moving to HEAD (sequencer-delete-REBASE_HEAD)\nbe8e0d55ec HEAD@{1} sync switch: checkout sequencer-delete-REBASE_HEAD (sequencer-delete-REBASE_HEAD)\n# should be fix-nth-prior-checkout\n64822b4a3b HEAD@{2} sync switch: checkout fix-nth-prior-checkout (sequencer-delete-REBASE_HEAD)\n# should be detached at 64882b4a3b\n64822b4a3b HEAD@{3} rebase: fast-forward (sequencer-delete-REBASE_HEAD)\n# should be detached at 64822b4a3b\nbf19b60d89 HEAD@{4} rebase (start): checkout @^ (sequencer-delete-REBASE_HEAD)\n64822b4a3b HEAD@{5} rebase: fast-forward (detached at 64822b4a3b)\nbf19b60d89 HEAD@{6} rebase (start): checkout fix-nth-prior-checkout^ (detached at 64822b4a3b)\nbe8e0d55ec HEAD@{7} rebase: checkout sequencer-delete-REBASE_HEAD (sequencer-delete-REBASE_HEAD)\nbe8e0d55ec HEAD@{8} rebase (start): checkout sequencer-delete-REBASE_HEAD (detached at be8e0d55ec)\n64822b4a3b HEAD@{9} checkout: moving from 64822b4a3ba71e5a823002cd5ebe0fa5fb4b3a0c to fix-nth-prior-checkout (fix-nth-prior-checkout)\n\nThanks\n\nPhillip\n\n> Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n> ---\n>   Documentation/pretty-formats.adoc |   3 +\n>   pretty.c                          |   4 +\n>   reflog-walk.c                     | 156 ++++++++++++++++++++++++++++--\n>   reflog-walk.h                     |   2 +\n>   t/t1414-reflog-walk.sh            | 122 +++++++++++++++++++++++\n>   5 files changed, 281 insertions(+), 6 deletions(-)\n> > diff --git a/Documentation/pretty-formats.adoc b/Documentation/pretty-formats.adoc\n> index 2ae0eb11a9..a8d00397c4 100644\n> --- a/Documentation/pretty-formats.adoc\n> +++ b/Documentation/pretty-formats.adoc\n> @@ -334,6 +334,9 @@ endif::git-rev-list[]\n>   +%gE+:: reflog identity email (respecting .mailmap, see\n>   \tlinkgit:git-shortlog[1] or linkgit:git-blame[1])\n>   +%gs+:: reflog subject\n> ++%gb+:: reflog branch, the branch `HEAD` was on when the entry was\n> +\trecorded, e.g., `topic` or `detached at 1a2b3c4`. For the reflog\n> +\tof any other ref, this is the shortened refname.\n>   ++%(trailers++`[:<option>,...]`++)++::\n>   display the trailers of the body as interpreted by\n>   linkgit:git-interpret-trailers[1]. The `trailers` string may be followed by\n> diff --git a/pretty.c b/pretty.c\n> index d8a9f370f6..1b3f7e2dc3 100644\n> --- a/pretty.c\n> +++ b/pretty.c\n> @@ -1637,6 +1637,10 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */\n>   \t\t\tif (c->pretty_ctx->reflog_info)\n>   \t\t\t\tget_reflog_message(sb, c->pretty_ctx->reflog_info);\n>   \t\t\treturn 2;\n> +\t\tcase 'b':\t/* reflog branch */\n> +\t\t\tif (c->pretty_ctx->reflog_info)\n> +\t\t\t\tget_reflog_branch(sb, c->pretty_ctx->reflog_info);\n> +\t\t\treturn 2;\n>   \t\tcase 'n':\n>   \t\tcase 'N':\n>   \t\tcase 'e':\n> diff --git a/reflog-walk.c b/reflog-walk.c\n> index 4dbeaa93a7..45b5dc09b5 100644\n> --- a/reflog-walk.c\n> +++ b/reflog-walk.c\n> @@ -3,7 +3,10 @@\n>   #include \"git-compat-util.h\"\n>   #include \"commit.h\"\n>   #include \"refs.h\"\n> +#include \"reflog.h\"\n>   #include \"diff.h\"\n> +#include \"hex.h\"\n> +#include \"object-name.h\"\n>   #include \"repository.h\"\n>   #include \"revision.h\"\n>   #include \"string-list.h\"\n> @@ -18,8 +21,10 @@ struct complete_reflogs {\n>   \t\ttimestamp_t timestamp;\n>   \t\tint tz;\n>   \t\tchar *message;\n> +\t\tchar *branch;\n>   \t} *items;\n>   \tint nr, alloc;\n> +\tbool branches_marked;\n>   };\n>   >   static int read_one_reflog(const char *refname UNUSED,\n> @@ -38,6 +43,7 @@ static int read_one_reflog(const char *refname UNUSED,\n>   \titem->timestamp = timestamp;\n>   \titem->tz = tz;\n>   \titem->message = xstrdup(message);\n> +\titem->branch = NULL;\n>   \tarray->nr++;\n>   \treturn 0;\n>   }\n> @@ -52,6 +58,7 @@ static void free_complete_reflog(struct complete_reflogs *array)\n>   \tfor (i = 0; i < array->nr; i++) {\n>   \t\tfree(array->items[i].email);\n>   \t\tfree(array->items[i].message);\n> +\t\tfree(array->items[i].branch);\n>   \t}\n>   \tfree(array->items);\n>   \tfree(array->ref);\n> @@ -65,6 +72,113 @@ static void complete_reflogs_clear(void *util, const char *str UNUSED)\n>   \tfree_complete_reflog(array);\n>   }\n>   > +static char *branch_label(const char *name, size_t len,\n> +\t\t\t  const struct object_id *oid)\n> +{\n> +\tif (name) {\n> +\t\tstruct object_id hex_oid;\n> +\t\tchar *branch;\n> +\n> +\t\tif (skip_prefix(name, \"refs/heads/\", &name))\n> +\t\t\tlen -= strlen(\"refs/heads/\");\n> +\t\tbranch = xmemdupz(name, len);\n> +\t\tif (strcmp(branch, \"HEAD\") && get_oid_hex(branch, &hex_oid) &&\n> +\t\t    !check_refname_format(branch, REFNAME_ALLOW_ONELEVEL))\n> +\t\t\treturn branch;\n> +\t\tfree(branch);\n> +\t}\n> +\treturn xstrfmt(\"detached at %s\",\n> +\t\t       repo_find_unique_abbrev(the_repository, oid,\n> +\t\t\t\t\t       DEFAULT_ABBREV));\n> +}\n> +\n> +static const char *skip_rebase_step(const char *message, const char *step)\n> +{\n> +\tconst char *p = strstr(message, \" (\");\n> +\tconst char *colon = strstr(message, \": \");\n> +\n> +\tif (!p || (colon && colon < p) ||\n> +\t    !skip_prefix(p + 2, step, &p) || !skip_prefix(p, \"): \", &p))\n> +\t\treturn NULL;\n> +\treturn p;\n> +}\n> +\n> +struct branch_move {\n> +\tconst char *from, *to;\n> +\tsize_t from_len, to_len;\n> +};\n> +\n> +static bool parse_branch_move(const char *message, struct branch_move *move)\n> +{\n> +\tconst char *to;\n> +\n> +\tmove->from = move->to = NULL;\n> +\tmove->from_len = move->to_len = 0;\n> +\tif (skip_prefix(message, REFLOG_SWITCH_PREFIX, &move->from) ||\n> +\t    skip_prefix(message, REFLOG_RENAME_PREFIX, &move->from)) {\n> +\t\tto = strstr(move->from, \" to \");\n> +\t\tif (!to)\n> +\t\t\treturn false;\n> +\t\tmove->from_len = to - move->from;\n> +\t\tmove->to = to + strlen(\" to \");\n> +\t} else if (((to = skip_rebase_step(message, REFLOG_STEP_FINISH)) ||\n> +\t\t    (to = skip_rebase_step(message, REFLOG_STEP_ABORT))) &&\n> +\t\t   skip_prefix(to, REFLOG_RETURNING_TO, &to)) {\n> +\t\tmove->from = move->to = to;\n> +\t\tmove->from_len = strcspn(to, \"\\n\");\n> +\t} else if (skip_prefix(message, \"rebase: \" REFLOG_CHECKOUT, &to)) {\n> +\t\tmove->to = to;\n> +\t} else if (!skip_rebase_step(message, REFLOG_STEP_START)) {\n> +\t\treturn false;\n> +\t}\n> +\tif (move->to)\n> +\t\tmove->to_len = strcspn(move->to, \"\\n\");\n> +\treturn true;\n> +}\n> +\n> +static void mark_branches(struct complete_reflogs *array)\n> +{\n> +\tstruct ref_store *refs = get_main_ref_store(the_repository);\n> +\tstruct object_id oid;\n> +\tconst char *head = refs_resolve_ref_unsafe(refs, \"HEAD\", 0, &oid, NULL);\n> +\tchar *branch = NULL;\n> +\tint unknown = array->nr;\n> +\n> +\tif (head)\n> +\t\tbranch = branch_label(head, strlen(head), &oid);\n> +\n> +\tfor (int i = array->nr - 1; i >= 0; i--) {\n> +\t\tstruct reflog_info *item = &array->items[i];\n> +\t\tstruct branch_move move;\n> +\n> +\t\tif (!parse_branch_move(item->message, &move)) {\n> +\t\t\titem->branch = xstrdup_or_null(branch);\n> +\t\t\tcontinue;\n> +\t\t}\n> +\n> +\t\tif (!branch) {\n> +\t\t\tbranch = branch_label(move.to, move.to_len,\n> +\t\t\t\t\t      &array->items[unknown - 1].noid);\n> +\t\t\tfor (int j = i + 1; j < unknown; j++)\n> +\t\t\t\tarray->items[j].branch = xstrdup(branch);\n> +\t\t}\n> +\t\titem->branch = branch;\n> +\t\tif (move.from) {\n> +\t\t\tbranch = branch_label(move.from, move.from_len,\n> +\t\t\t\t\t      &item->ooid);\n> +\t\t} else {\n> +\t\t\tbranch = NULL;\n> +\t\t\tunknown = i;\n> +\t\t}\n> +\t}\n> +\n> +\tif (!branch && unknown < array->nr)\n> +\t\tfor (int i = 0; i < unknown; i++)\n> +\t\t\tarray->items[i].branch =\n> +\t\t\t\txstrdup(array->items[unknown].branch);\n> +\tfree(branch);\n> +}\n> +\n>   static struct complete_reflogs *read_complete_reflog(const char *ref)\n>   {\n>   \tstruct complete_reflogs *reflogs =\n> @@ -234,6 +348,16 @@ int add_reflog_for_walk(struct reflog_walk_info *info,\n>   \treturn 0;\n>   }\n>   > +static const char *short_ref(struct complete_reflogs *reflogs)\n> +{\n> +\tstruct ref_store *refs = get_main_ref_store(the_repository);\n> +\n> +\tif (!reflogs->short_ref)\n> +\t\treflogs->short_ref =\n> +\t\t\trefs_shorten_unambiguous_ref(refs, reflogs->ref, 0);\n> +\treturn reflogs->short_ref;\n> +}\n> +\n>   void get_reflog_selector(struct strbuf *sb,\n>   \t\t\t struct reflog_walk_info *reflog_info,\n>   \t\t\t struct date_mode dmode, int force_date,\n> @@ -247,12 +371,7 @@ void get_reflog_selector(struct strbuf *sb,\n>   \t\treturn;\n>   >   \tif (shorten) {\n> -\t\tif (!commit_reflog->reflogs->short_ref)\n> -\t\t\tcommit_reflog->reflogs->short_ref\n> -\t\t\t\t= refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),\n> -\t\t\t\t\t\t\t       commit_reflog->reflogs->ref,\n> -\t\t\t\t\t\t\t       0);\n> -\t\tprinted_ref = commit_reflog->reflogs->short_ref;\n> +\t\tprinted_ref = short_ref(commit_reflog->reflogs);\n>   \t} else {\n>   \t\tprinted_ref = commit_reflog->reflogs->ref;\n>   \t}\n> @@ -287,6 +406,31 @@ void get_reflog_message(struct strbuf *sb,\n>   \tstrbuf_add(sb, info->message, len);\n>   }\n>   > +void get_reflog_branch(struct strbuf *sb,\n> +\t\t       struct reflog_walk_info *reflog_info)\n> +{\n> +\tstruct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;\n> +\tstruct complete_reflogs *reflogs;\n> +\tconst char *branch;\n> +\n> +\tif (!commit_reflog)\n> +\t\treturn;\n> +\n> +\treflogs = commit_reflog->reflogs;\n> +\tif (strcmp(reflogs->ref, \"HEAD\")) {\n> +\t\tstrbuf_addstr(sb, short_ref(reflogs));\n> +\t\treturn;\n> +\t}\n> +\n> +\tif (!reflogs->branches_marked) {\n> +\t\tmark_branches(reflogs);\n> +\t\treflogs->branches_marked = true;\n> +\t}\n> +\tbranch = reflogs->items[commit_reflog->recno+1].branch;\n> +\tif (branch)\n> +\t\tstrbuf_addstr(sb, branch);\n> +}\n> +\n>   const char *get_reflog_ident(struct reflog_walk_info *reflog_info)\n>   {\n>   \tstruct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;\n> diff --git a/reflog-walk.h b/reflog-walk.h\n> index 989583dc55..fc1f344ce6 100644\n> --- a/reflog-walk.h\n> +++ b/reflog-walk.h\n> @@ -13,6 +13,8 @@ void show_reflog_message(struct reflog_walk_info *info, int,\n>   \t\t\t struct date_mode, int force_date);\n>   void get_reflog_message(struct strbuf *sb,\n>   \t\t\tstruct reflog_walk_info *reflog_info);\n> +void get_reflog_branch(struct strbuf *sb,\n> +\t\t       struct reflog_walk_info *reflog_info);\n>   const char *get_reflog_ident(struct reflog_walk_info *reflog_info);\n>   timestamp_t get_reflog_timestamp(struct reflog_walk_info *reflog_info);\n>   void get_reflog_selector(struct strbuf *sb,\n> diff --git a/t/t1414-reflog-walk.sh b/t/t1414-reflog-walk.sh\n> index be6c3f472c..0e821922ca 100755\n> --- a/t/t1414-reflog-walk.sh\n> +++ b/t/t1414-reflog-walk.sh\n> @@ -131,6 +131,128 @@ test_expect_success 'walk prefers reflog to ref tip' '\n>   \ttest_cmp expect actual\n>   '\n>   > +test_expect_success '%gb names the branch HEAD was on' '\n> +\tgit init branches &&\n> +\t(\n> +\t\tcd branches &&\n> +\t\ttest_commit base &&\n> +\t\tgit switch -c topic &&\n> +\t\ttest_commit A &&\n> +\t\ttest_commit B &&\n> +\t\tgit switch main &&\n> +\t\ttest_commit upstream &&\n> +\t\tgit switch topic &&\n> +\t\tgit rebase main &&\n> +\n> +\t\tcat >expect <<-\\EOF &&\n> +\t\t(topic) rebase (finish): returning to refs/heads/topic\n> +\t\t(topic) rebase (pick): B\n> +\t\t(topic) rebase (pick): A\n> +\t\t(topic) rebase (start): checkout main\n> +\t\t(topic) checkout: moving from main to topic\n> +\t\t(main) commit: upstream\n> +\t\t(main) checkout: moving from topic to main\n> +\t\t(topic) commit: B\n> +\t\t(topic) commit: A\n> +\t\t(topic) checkout: moving from main to topic\n> +\t\t(main) commit (initial): base\n> +\t\tEOF\n> +\t\tgit log -g --format=\"(%gb) %gs\" >actual &&\n> +\t\ttest_cmp expect actual\n> +\t)\n> +'\n> +\n> +test_expect_success '%gb names the ref of other reflogs' '\n> +\t(\n> +\t\tcd branches &&\n> +\t\techo main >expect &&\n> +\t\tgit log -g -1 --format=%gb main >actual &&\n> +\t\ttest_cmp expect actual\n> +\t)\n> +'\n> +\n> +test_expect_success '%gb follows \"git rebase <upstream> <branch>\"' '\n> +\t(\n> +\t\tcd branches &&\n> +\t\tgit switch main &&\n> +\t\ttest_commit main-change &&\n> +\t\tgit rebase main topic &&\n> +\t\tgit switch main &&\n> +\t\tgit rebase main topic &&\n> +\n> +\t\tcat >expect <<-\\EOF &&\n> +\t\t(topic) rebase: checkout topic\n> +\t\t(main) checkout: moving from topic to main\n> +\t\t(topic) rebase (finish): returning to refs/heads/topic\n> +\t\t(topic) rebase (pick): B\n> +\t\t(topic) rebase (pick): A\n> +\t\t(topic) rebase (start): checkout main\n> +\t\t(main) commit: main-change\n> +\t\t(main) checkout: moving from topic to main\n> +\t\tEOF\n> +\t\tgit log -g -8 --format=\"(%gb) %gs\" >actual &&\n> +\t\ttest_cmp expect actual\n> +\t)\n> +'\n> +\n> +test_expect_success '%gb names a detached HEAD by the commit it was left at' '\n> +\t(\n> +\t\tcd branches &&\n> +\t\tgit switch --detach topic~1 &&\n> +\t\ttest_commit detached &&\n> +\t\tgit rebase topic &&\n> +\t\trebased=$(git rev-parse HEAD) &&\n> +\t\tgit switch topic &&\n> +\n> +\t\tleft_before=$(git rev-parse --short detached) &&\n> +\t\tleft_after=$(git rev-parse --short $rebased) &&\n> +\t\tcat >expect <<-EOF &&\n> +\t\t(topic) checkout: moving from $rebased to topic\n> +\t\t(detached at $left_after) rebase (pick): detached\n> +\t\t(detached at $left_after) rebase (start): checkout topic\n> +\t\t(detached at $left_before) commit: detached\n> +\t\t(detached at $left_before) checkout: moving from topic to topic~1\n> +\t\tEOF\n> +\t\tgit log -g -5 --format=\"(%gb) %gs\" >actual &&\n> +\t\ttest_cmp expect actual\n> +\t)\n> +'\n> +\n> +test_expect_success '%gb names the branch of a \"git pull --rebase\" in a fresh clone' '\n> +\tgit clone branches downstream &&\n> +\t(\n> +\t\tcd branches &&\n> +\t\ttest_commit upstream-change\n> +\t) &&\n> +\t(\n> +\t\tcd downstream &&\n> +\t\ttest_commit local-change &&\n> +\t\tgit pull --rebase &&\n> +\n> +\t\techo \"(topic)\" >expect &&\n> +\t\tgit log -g --format=\"(%gb)\" >out &&\n> +\t\tsort -u out >actual &&\n> +\t\ttest_cmp expect actual\n> +\t)\n> +'\n> +\n> +test_expect_success '%gb follows a renamed branch' '\n> +\t(\n> +\t\tcd branches &&\n> +\t\ttest_commit before-rename &&\n> +\t\tgit branch -m topic renamed &&\n> +\t\ttest_commit after-rename &&\n> +\n> +\t\tcat >expect <<-\\EOF &&\n> +\t\t(renamed) commit: after-rename\n> +\t\t(renamed) Branch: renamed refs/heads/topic to refs/heads/renamed\n> +\t\t(topic) commit: before-rename\n> +\t\tEOF\n> +\t\tgit log -g -3 --format=\"(%gb) %gs\" >actual &&\n> +\t\ttest_cmp expect actual\n> +\t)\n> +'\n> +\n>   test_expect_success 'rev-list -g complains when there are no reflogs' '\n>   \ttest_must_fail git rev-list -g\n>   '\n\n\n"},{"id":"557502","messageId":"pull.2448.v4.git.git.1793826938.gitgitgadget@gmail.com","threadId":"66545","inReplyTo":"pull.2448.git.git.1792070038.gitgitgadget@gmail.com","subject":"[PATCH v4 0/2] reflog: add format %gb","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-04T21:15:36Z","receivedAt":"2026-11-04T21:15:36Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"Create a better reflog. Follows the discussion in\nhttps://lore.kernel.org/git/akZfm-igZKeHaDST@pks.im/.\n\nThe reflog of HEAD records the work done on every branch, so \"git reflog\"\nmixes them and does not say which entry was made on which branch. This adds\na \"%gb\" placeholder that names the branch HEAD was on when each entry was\nrecorded:\n\n$ git log -g --format='%h %gd: (%gb) %gs'\n9c8d7e6 HEAD@{0}: (topic) rebase (finish): returning to refs/heads/topic\n1a2b3c4 HEAD@{2}: (topic) rebase (start): checkout main\n1a2b3c4 HEAD@{3}: (main) commit: fix typo\n\n\nIt follows branch switches, branch renames and rebases, including \"git\nrebase \", rebases of a detached HEAD and rebases of a branch that was\nalready up to date. When HEAD was detached, the entry shows \"detached at \".\n\nChanges in v4:\n\n * %gb now names the right branch when git rebase <upstream> <branch> runs\n   with GIT_REFLOG_ACTION set.\n * Reworded the %gb commit message.\n\nChanges in v3:\n\n * Dropped --operations.\n * Replaced --historical-branch with a %gb placeholder for --format, e.g.\n   git log -g --format='%h %gd: (%gb) %gs'.\n * %gb names the right branch after git rebase <upstream> <branch>, a rebase\n   of a detached HEAD and an up-to-date rebase.\n * Named the returning to and checkout reflog strings in reflog.h too.\n\nChanges in v2:\n\n * Move the \"historical branch\" rightward in the display for cleaner\n   alignment.\n * Improve commit messages.\n\nHarald Nordgren (2):\n  reflog: name the strings that mark switches, renames and rebase steps\n  pretty: add %gb to name the branch of each reflog entry\n\n Documentation/pretty-formats.adoc |   3 +\n builtin/branch.c                  |   3 +-\n builtin/checkout.c                |   3 +-\n builtin/rebase.c                  |  19 ++--\n object-name.c                     |   3 +-\n pretty.c                          |   4 +\n reflog-walk.c                     | 167 ++++++++++++++++++++++++++++--\n reflog-walk.h                     |   2 +\n reflog.h                          |   8 ++\n sequencer.c                       |  13 +--\n t/t1414-reflog-walk.sh            | 137 ++++++++++++++++++++++++\n wt-status.c                       |   3 +-\n 12 files changed, 340 insertions(+), 25 deletions(-)\n\n\nbase-commit: 750f5de7dfe9a4062f95088212b063f189d50e91\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2448%2FHaraldNordgren%2Freflog-operations-v4\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2448/HaraldNordgren/reflog-operations-v4\nPull-Request: https://github.com/git/git/pull/2448\n\nRange-diff vs v3:\n\n 1:  4d779e8da3 = 1:  f960a24b8b reflog: name the strings that mark switches, renames and rebase steps\n 2:  dee548f9b1 ! 2:  99a1047702 pretty: add %gb to name the branch of each reflog entry\n     @@ Commit message\n              1a2b3c4 HEAD@{3}: (main) commit: fix typo\n              5d6e7f8 HEAD@{4}: (main) checkout: moving from topic to main\n      \n     -    The branch is read from the entries that branch switches, branch\n     -    renames and rebases leave behind. The entries of a rebase are named\n     -    after the branch being rebased. When HEAD was detached, the entry\n     -    shows \"detached at <commit>\" with the commit HEAD was at when it\n     -    moved on. When nothing older in the reflog says which branch a rebase\n     -    started on, the rebase is taken to have started on the branch it\n     -    rebased. For the reflog of any other ref, \"%gb\" is the name of that\n     -    ref.\n     +    The branch is read from the entries created by branch switches,\n     +    branch renames and rebases. The entries of a rebase are labeled with\n     +    the branch being rebased. When HEAD was detached, the entry shows\n     +    \"detached at <commit>\" with the commit HEAD was at when it moved on.\n     +    When nothing older in the reflog says which branch a rebase started\n     +    on, the rebase is taken to have started on the branch it rebased. For\n     +    the reflog of any other ref, \"%gb\" is the name of that ref.\n      \n          Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n      \n     @@ reflog-walk.c: static void complete_reflogs_clear(void *util, const char *str UN\n      +\treturn p;\n      +}\n      +\n     ++static const char *skip_rebase_checkout(const char *message)\n     ++{\n     ++\tconst char *p = strstr(message, \" (\");\n     ++\tconst char *colon = strstr(message, \": \");\n     ++\n     ++\tif (!colon || (p && p < colon) ||\n     ++\t    !skip_prefix(colon + 2, REFLOG_CHECKOUT, &p))\n     ++\t\treturn NULL;\n     ++\treturn p;\n     ++}\n     ++\n      +struct branch_move {\n      +\tconst char *from, *to;\n      +\tsize_t from_len, to_len;\n     @@ reflog-walk.c: static void complete_reflogs_clear(void *util, const char *str UN\n      +\t\t   skip_prefix(to, REFLOG_RETURNING_TO, &to)) {\n      +\t\tmove->from = move->to = to;\n      +\t\tmove->from_len = strcspn(to, \"\\n\");\n     -+\t} else if (skip_prefix(message, \"rebase: \" REFLOG_CHECKOUT, &to)) {\n     ++\t} else if ((to = skip_rebase_checkout(message))) {\n      +\t\tmove->to = to;\n      +\t} else if (!skip_rebase_step(message, REFLOG_STEP_START)) {\n      +\t\treturn false;\n     @@ t/t1414-reflog-walk.sh: test_expect_success 'walk prefers reflog to ref tip' '\n      +\t)\n      +'\n      +\n     ++test_expect_success '%gb follows \"git rebase <upstream> <branch>\" with GIT_REFLOG_ACTION' '\n     ++\t(\n     ++\t\tcd branches &&\n     ++\t\tgit switch main &&\n     ++\t\tGIT_REFLOG_ACTION=\"sync switch\" git rebase main topic &&\n     ++\n     ++\t\tcat >expect <<-\\EOF &&\n     ++\t\t(topic) sync switch: checkout topic\n     ++\t\t(main) checkout: moving from topic to main\n     ++\t\tEOF\n     ++\t\tgit log -g -2 --format=\"(%gb) %gs\" >actual &&\n     ++\t\ttest_cmp expect actual\n     ++\t)\n     ++'\n     ++\n      +test_expect_success '%gb names a detached HEAD by the commit it was left at' '\n      +\t(\n      +\t\tcd branches &&\n\n-- \ngitgitgadget\n\n"},{"id":"557503","messageId":"f960a24b8bcf0ea4e77e20437cd436cc1732683f.1793826938.git.gitgitgadget@gmail.com","threadId":"66545","inReplyTo":"pull.2448.v4.git.git.1793826938.gitgitgadget@gmail.com","subject":"[PATCH v4 1/2] reflog: name the strings that mark switches, renames and rebase steps","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-04T21:15:37Z","receivedAt":"2026-11-04T21:15:37Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nThe next commit adds a \"%gb\" placeholder that shows which branch HEAD\nwas on for each reflog entry. It finds the branch from the messages\nthat branch switches, branch renames and rebases leave in the reflog.\nThe commands that write these messages spell out the strings by hand,\nand \"@{-<n>}\" and \"git status\" each keep their own copy of the switch\nprefix to read it back. Reading them in one more place would add yet\nanother copy that has to stay in sync.\n\nName the strings once in reflog.h and use the names wherever the\nmessages are written or read: the \"checkout: moving from \" prefix\nwritten by \"git checkout\", the \"Branch: renamed \" prefix written by\n\"git branch -m\", and the \"start\", \"finish\" and \"abort\" steps, the\n\"returning to \" before the branch a rebase goes back to and the\n\"checkout \" before what it checks out, all written by \"git rebase\".\n\nNo change in behavior.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n builtin/branch.c   |  3 ++-\n builtin/checkout.c |  3 ++-\n builtin/rebase.c   | 19 ++++++++++---------\n object-name.c      |  3 ++-\n reflog.h           |  8 ++++++++\n sequencer.c        | 13 +++++++------\n wt-status.c        |  3 ++-\n 7 files changed, 33 insertions(+), 19 deletions(-)\n\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex b536b619bb..f7acb0f349 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -13,6 +13,7 @@\n #include \"editor.h\"\n #include \"environment.h\"\n #include \"refs.h\"\n+#include \"reflog.h\"\n #include \"commit.h\"\n #include \"gettext.h\"\n #include \"object-name.h\"\n@@ -669,7 +670,7 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int\n \t\tstrbuf_addf(&logmsg, \"Branch: copied %s to %s\",\n \t\t\t    oldref.buf, newref.buf);\n \telse\n-\t\tstrbuf_addf(&logmsg, \"Branch: renamed %s to %s\",\n+\t\tstrbuf_addf(&logmsg, REFLOG_RENAME_PREFIX \"%s to %s\",\n \t\t\t    oldref.buf, newref.buf);\n \n \tif (!copy && !(oldref_usage & IS_ORPHAN) &&\ndiff --git a/builtin/checkout.c b/builtin/checkout.c\nindex f28467f6df..cbdd78dfc8 100644\n--- a/builtin/checkout.c\n+++ b/builtin/checkout.c\n@@ -26,6 +26,7 @@\n #include \"preload-index.h\"\n #include \"read-cache.h\"\n #include \"refs.h\"\n+#include \"reflog.h\"\n #include \"remote.h\"\n #include \"repo-settings.h\"\n #include \"resolve-undo.h\"\n@@ -1043,7 +1044,7 @@ static void update_refs_for_switch(const struct checkout_opts *opts,\n \n \treflog_msg = getenv(\"GIT_REFLOG_ACTION\");\n \tif (!reflog_msg)\n-\t\tstrbuf_addf(&msg, \"checkout: moving from %s to %s\",\n+\t\tstrbuf_addf(&msg, REFLOG_SWITCH_PREFIX \"%s to %s\",\n \t\t\told_desc ? old_desc : \"(invalid)\", new_branch_info->name);\n \telse\n \t\tstrbuf_insertstr(&msg, 0, reflog_msg);\ndiff --git a/builtin/rebase.c b/builtin/rebase.c\nindex 535db60181..47a7b2fd3c 100644\n--- a/builtin/rebase.c\n+++ b/builtin/rebase.c\n@@ -17,6 +17,7 @@\n #include \"strvec.h\"\n #include \"dir.h\"\n #include \"refs.h\"\n+#include \"reflog.h\"\n #include \"config.h\"\n #include \"unpack-trees.h\"\n #include \"lockfile.h\"\n@@ -601,11 +602,11 @@ static int move_to_original_branch(struct rebase_options *opts)\n \tif (!opts->onto)\n \t\tBUG(\"move_to_original_branch without onto\");\n \n-\tstrbuf_addf(&branch_reflog, \"%s (finish): %s onto %s\",\n-\t\t    opts->reflog_action,\n+\tstrbuf_addf(&branch_reflog, \"%s (%s): %s onto %s\",\n+\t\t    opts->reflog_action, REFLOG_STEP_FINISH,\n \t\t    opts->head_name, oid_to_hex(&opts->onto->object.oid));\n-\tstrbuf_addf(&head_reflog, \"%s (finish): returning to %s\",\n-\t\t    opts->reflog_action, opts->head_name);\n+\tstrbuf_addf(&head_reflog, \"%s (%s): \" REFLOG_RETURNING_TO \"%s\",\n+\t\t    opts->reflog_action, REFLOG_STEP_FINISH, opts->head_name);\n \tropts.branch = opts->head_name;\n \tropts.flags = RESET_WORKING_TREE_REFS_ONLY |\n \t\t      RESET_WORKING_TREE_UPDATE_HEAD;\n@@ -867,7 +868,7 @@ static int checkout_up_to_date(struct rebase_options *options)\n \tstruct reset_working_tree_options ropts = { 0 };\n \tint ret = 0;\n \n-\tstrbuf_addf(&buf, \"%s: checkout %s\",\n+\tstrbuf_addf(&buf, \"%s: \" REFLOG_CHECKOUT \"%s\",\n \t\t    options->reflog_action, options->switch_to);\n \tropts.oid = &options->orig_head->object.oid;\n \tropts.branch = options->head_name;\n@@ -1413,8 +1414,8 @@ int cmd_rebase(int argc,\n \t\tif (read_basic_state(&options))\n \t\t\texit(1);\n \n-\t\tstrbuf_addf(&head_msg, \"%s (abort): returning to %s\",\n-\t\t\t    options.reflog_action,\n+\t\tstrbuf_addf(&head_msg, \"%s (%s): \" REFLOG_RETURNING_TO \"%s\",\n+\t\t\t    options.reflog_action, REFLOG_STEP_ABORT,\n \t\t\t    options.head_name ? options.head_name\n \t\t\t\t\t      : oid_to_hex(&options.orig_head->object.oid));\n \t\tropts.oid = &options.orig_head->object.oid;\n@@ -1884,8 +1885,8 @@ int cmd_rebase(int argc,\n \t\tprintf(_(\"First, rewinding head to replay your work on top of \"\n \t\t\t \"it...\\n\"));\n \n-\tstrbuf_addf(&msg, \"%s (start): checkout %s\",\n-\t\t    options.reflog_action, options.onto_name);\n+\tstrbuf_addf(&msg, \"%s (%s): \" REFLOG_CHECKOUT \"%s\",\n+\t\t    options.reflog_action, REFLOG_STEP_START, options.onto_name);\n \tropts.oid = &options.onto->object.oid;\n \tropts.orig_head = &options.orig_head->object.oid;\n \tropts.flags = RESET_WORKING_TREE_DETACH |\ndiff --git a/object-name.c b/object-name.c\nindex 6546685760..5b6ce8e0e0 100644\n--- a/object-name.c\n+++ b/object-name.c\n@@ -13,6 +13,7 @@\n #include \"tree.h\"\n #include \"tree-walk.h\"\n #include \"refs.h\"\n+#include \"reflog.h\"\n #include \"remote.h\"\n #include \"dir.h\"\n #include \"odb.h\"\n@@ -1251,7 +1252,7 @@ static int grab_nth_branch_switch(const char *refname UNUSED,\n \tconst char *match = NULL, *target = NULL;\n \tsize_t len;\n \n-\tif (skip_prefix(message, \"checkout: moving from \", &match))\n+\tif (skip_prefix(message, REFLOG_SWITCH_PREFIX, &match))\n \t\ttarget = strstr(match, \" to \");\n \n \tif (!match || !target)\ndiff --git a/reflog.h b/reflog.h\nindex 3bdd1ca8a5..f5586242bf 100644\n--- a/reflog.h\n+++ b/reflog.h\n@@ -2,6 +2,14 @@\n #define REFLOG_H\n #include \"refs.h\"\n \n+#define REFLOG_SWITCH_PREFIX \"checkout: moving from \"\n+#define REFLOG_RENAME_PREFIX \"Branch: renamed \"\n+#define REFLOG_STEP_START \"start\"\n+#define REFLOG_STEP_FINISH \"finish\"\n+#define REFLOG_STEP_ABORT \"abort\"\n+#define REFLOG_RETURNING_TO \"returning to \"\n+#define REFLOG_CHECKOUT \"checkout \"\n+\n #define REFLOG_EXPIRE_TOTAL   (1 << 0)\n #define REFLOG_EXPIRE_UNREACH (1 << 1)\n \ndiff --git a/sequencer.c b/sequencer.c\nindex 508ec71792..da6f61a0e6 100644\n--- a/sequencer.c\n+++ b/sequencer.c\n@@ -31,6 +31,7 @@\n #include \"merge-ort.h\"\n #include \"merge-ort-wrappers.h\"\n #include \"refs.h\"\n+#include \"reflog.h\"\n #include \"sparse-index.h\"\n #include \"strvec.h\"\n #include \"quote.h\"\n@@ -5011,8 +5012,8 @@ static int checkout_onto(struct repository *r, struct replay_opts *opts,\n \t\t\t RESET_WORKING_TREE_UPDATE_HEAD |\n \t\t\t RESET_WORKING_TREE_UPDATE_ORIG_HEAD |\n \t\t\t RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK,\n-\t\t.head_msg = reflog_message(opts, \"start\", \"checkout %s\",\n-\t\t\t\t\t   onto_name),\n+\t\t.head_msg = reflog_message(opts, REFLOG_STEP_START,\n+\t\t\t\t\t   REFLOG_CHECKOUT \"%s\", onto_name),\n \t\t.default_reflog_action = sequencer_reflog_action(opts)\n \t};\n \tif (reset_working_tree(r, &ropts)) {\n@@ -5311,16 +5312,16 @@ cleanup_head_ref:\n \t\t\t\tres = error(_(\"could not read 'onto'\"));\n \t\t\t\tgoto cleanup_head_ref;\n \t\t\t}\n-\t\t\tmsg = reflog_message(opts, \"finish\", \"%s onto %s\",\n-\t\t\t\thead_ref.buf, buf.buf);\n+\t\t\tmsg = reflog_message(opts, REFLOG_STEP_FINISH,\n+\t\t\t\t\"%s onto %s\", head_ref.buf, buf.buf);\n \t\t\tif (refs_update_ref(get_main_ref_store(the_repository), msg, head_ref.buf, &head, &orig,\n \t\t\t\t\t    REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) {\n \t\t\t\tres = error(_(\"could not update %s\"),\n \t\t\t\t\thead_ref.buf);\n \t\t\t\tgoto cleanup_head_ref;\n \t\t\t}\n-\t\t\tmsg = reflog_message(opts, \"finish\", \"returning to %s\",\n-\t\t\t\thead_ref.buf);\n+\t\t\tmsg = reflog_message(opts, REFLOG_STEP_FINISH,\n+\t\t\t\tREFLOG_RETURNING_TO \"%s\", head_ref.buf);\n \t\t\tif (refs_update_symref(get_main_ref_store(the_repository), \"HEAD\", head_ref.buf, msg)) {\n \t\t\t\tres = error(_(\"could not update HEAD to %s\"),\n \t\t\t\t\thead_ref.buf);\ndiff --git a/wt-status.c b/wt-status.c\nindex 238bb48643..3516432338 100644\n--- a/wt-status.c\n+++ b/wt-status.c\n@@ -22,6 +22,7 @@\n #include \"strvec.h\"\n #include \"remote.h\"\n #include \"refs.h\"\n+#include \"reflog.h\"\n #include \"submodule.h\"\n #include \"column.h\"\n #include \"read-cache.h\"\n@@ -1790,7 +1791,7 @@ static int grab_1st_switch(const char *refname UNUSED,\n \tstruct grab_1st_switch_cbdata *cb = cb_data;\n \tconst char *target = NULL, *end;\n \n-\tif (!skip_prefix(message, \"checkout: moving from \", &message))\n+\tif (!skip_prefix(message, REFLOG_SWITCH_PREFIX, &message))\n \t\treturn 0;\n \ttarget = strstr(message, \" to \");\n \tif (!target)\n-- \ngitgitgadget\n\n\n"},{"id":"557504","messageId":"99a104770226c4cb1a05846c21e52484f701bb2b.1793826938.git.gitgitgadget@gmail.com","threadId":"66545","inReplyTo":"pull.2448.v4.git.git.1793826938.gitgitgadget@gmail.com","subject":"[PATCH v4 2/2] pretty: add %gb to name the branch of each reflog entry","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-04T21:15:38Z","receivedAt":"2026-11-04T21:15:38Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nThe reflog of HEAD records the work done on every branch, so the\noutput of \"git reflog\" mixes them and does not say which entry was\nmade on which branch. Working that out by hand means looking back for\nthe branch switch before each entry, and a rebase can move HEAD to\nanother branch without writing one, as \"git rebase <upstream>\n<branch>\" does.\n\nAdd a \"%gb\" placeholder that names the branch HEAD was on when each\nentry was recorded:\n\n    $ git log -g --format='%h %gd: (%gb) %gs'\n    9c8d7e6 HEAD@{0}: (topic) rebase (finish): returning to refs/heads/topic\n    9c8d7e6 HEAD@{1}: (topic) rebase (pick): add tests\n    1a2b3c4 HEAD@{2}: (topic) rebase (start): checkout main\n    1a2b3c4 HEAD@{3}: (main) commit: fix typo\n    5d6e7f8 HEAD@{4}: (main) checkout: moving from topic to main\n\nThe branch is read from the entries created by branch switches,\nbranch renames and rebases. The entries of a rebase are labeled with\nthe branch being rebased. When HEAD was detached, the entry shows\n\"detached at <commit>\" with the commit HEAD was at when it moved on.\nWhen nothing older in the reflog says which branch a rebase started\non, the rebase is taken to have started on the branch it rebased. For\nthe reflog of any other ref, \"%gb\" is the name of that ref.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/pretty-formats.adoc |   3 +\n pretty.c                          |   4 +\n reflog-walk.c                     | 167 ++++++++++++++++++++++++++++--\n reflog-walk.h                     |   2 +\n t/t1414-reflog-walk.sh            | 137 ++++++++++++++++++++++++\n 5 files changed, 307 insertions(+), 6 deletions(-)\n\ndiff --git a/Documentation/pretty-formats.adoc b/Documentation/pretty-formats.adoc\nindex 2ae0eb11a9..a8d00397c4 100644\n--- a/Documentation/pretty-formats.adoc\n+++ b/Documentation/pretty-formats.adoc\n@@ -334,6 +334,9 @@ endif::git-rev-list[]\n +%gE+:: reflog identity email (respecting .mailmap, see\n \tlinkgit:git-shortlog[1] or linkgit:git-blame[1])\n +%gs+:: reflog subject\n++%gb+:: reflog branch, the branch `HEAD` was on when the entry was\n+\trecorded, e.g., `topic` or `detached at 1a2b3c4`. For the reflog\n+\tof any other ref, this is the shortened refname.\n ++%(trailers++`[:<option>,...]`++)++::\n display the trailers of the body as interpreted by\n linkgit:git-interpret-trailers[1]. The `trailers` string may be followed by\ndiff --git a/pretty.c b/pretty.c\nindex d8a9f370f6..1b3f7e2dc3 100644\n--- a/pretty.c\n+++ b/pretty.c\n@@ -1637,6 +1637,10 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */\n \t\t\tif (c->pretty_ctx->reflog_info)\n \t\t\t\tget_reflog_message(sb, c->pretty_ctx->reflog_info);\n \t\t\treturn 2;\n+\t\tcase 'b':\t/* reflog branch */\n+\t\t\tif (c->pretty_ctx->reflog_info)\n+\t\t\t\tget_reflog_branch(sb, c->pretty_ctx->reflog_info);\n+\t\t\treturn 2;\n \t\tcase 'n':\n \t\tcase 'N':\n \t\tcase 'e':\ndiff --git a/reflog-walk.c b/reflog-walk.c\nindex 4dbeaa93a7..9246ab3208 100644\n--- a/reflog-walk.c\n+++ b/reflog-walk.c\n@@ -3,7 +3,10 @@\n #include \"git-compat-util.h\"\n #include \"commit.h\"\n #include \"refs.h\"\n+#include \"reflog.h\"\n #include \"diff.h\"\n+#include \"hex.h\"\n+#include \"object-name.h\"\n #include \"repository.h\"\n #include \"revision.h\"\n #include \"string-list.h\"\n@@ -18,8 +21,10 @@ struct complete_reflogs {\n \t\ttimestamp_t timestamp;\n \t\tint tz;\n \t\tchar *message;\n+\t\tchar *branch;\n \t} *items;\n \tint nr, alloc;\n+\tbool branches_marked;\n };\n \n static int read_one_reflog(const char *refname UNUSED,\n@@ -38,6 +43,7 @@ static int read_one_reflog(const char *refname UNUSED,\n \titem->timestamp = timestamp;\n \titem->tz = tz;\n \titem->message = xstrdup(message);\n+\titem->branch = NULL;\n \tarray->nr++;\n \treturn 0;\n }\n@@ -52,6 +58,7 @@ static void free_complete_reflog(struct complete_reflogs *array)\n \tfor (i = 0; i < array->nr; i++) {\n \t\tfree(array->items[i].email);\n \t\tfree(array->items[i].message);\n+\t\tfree(array->items[i].branch);\n \t}\n \tfree(array->items);\n \tfree(array->ref);\n@@ -65,6 +72,124 @@ static void complete_reflogs_clear(void *util, const char *str UNUSED)\n \tfree_complete_reflog(array);\n }\n \n+static char *branch_label(const char *name, size_t len,\n+\t\t\t  const struct object_id *oid)\n+{\n+\tif (name) {\n+\t\tstruct object_id hex_oid;\n+\t\tchar *branch;\n+\n+\t\tif (skip_prefix(name, \"refs/heads/\", &name))\n+\t\t\tlen -= strlen(\"refs/heads/\");\n+\t\tbranch = xmemdupz(name, len);\n+\t\tif (strcmp(branch, \"HEAD\") && get_oid_hex(branch, &hex_oid) &&\n+\t\t    !check_refname_format(branch, REFNAME_ALLOW_ONELEVEL))\n+\t\t\treturn branch;\n+\t\tfree(branch);\n+\t}\n+\treturn xstrfmt(\"detached at %s\",\n+\t\t       repo_find_unique_abbrev(the_repository, oid,\n+\t\t\t\t\t       DEFAULT_ABBREV));\n+}\n+\n+static const char *skip_rebase_step(const char *message, const char *step)\n+{\n+\tconst char *p = strstr(message, \" (\");\n+\tconst char *colon = strstr(message, \": \");\n+\n+\tif (!p || (colon && colon < p) ||\n+\t    !skip_prefix(p + 2, step, &p) || !skip_prefix(p, \"): \", &p))\n+\t\treturn NULL;\n+\treturn p;\n+}\n+\n+static const char *skip_rebase_checkout(const char *message)\n+{\n+\tconst char *p = strstr(message, \" (\");\n+\tconst char *colon = strstr(message, \": \");\n+\n+\tif (!colon || (p && p < colon) ||\n+\t    !skip_prefix(colon + 2, REFLOG_CHECKOUT, &p))\n+\t\treturn NULL;\n+\treturn p;\n+}\n+\n+struct branch_move {\n+\tconst char *from, *to;\n+\tsize_t from_len, to_len;\n+};\n+\n+static bool parse_branch_move(const char *message, struct branch_move *move)\n+{\n+\tconst char *to;\n+\n+\tmove->from = move->to = NULL;\n+\tmove->from_len = move->to_len = 0;\n+\tif (skip_prefix(message, REFLOG_SWITCH_PREFIX, &move->from) ||\n+\t    skip_prefix(message, REFLOG_RENAME_PREFIX, &move->from)) {\n+\t\tto = strstr(move->from, \" to \");\n+\t\tif (!to)\n+\t\t\treturn false;\n+\t\tmove->from_len = to - move->from;\n+\t\tmove->to = to + strlen(\" to \");\n+\t} else if (((to = skip_rebase_step(message, REFLOG_STEP_FINISH)) ||\n+\t\t    (to = skip_rebase_step(message, REFLOG_STEP_ABORT))) &&\n+\t\t   skip_prefix(to, REFLOG_RETURNING_TO, &to)) {\n+\t\tmove->from = move->to = to;\n+\t\tmove->from_len = strcspn(to, \"\\n\");\n+\t} else if ((to = skip_rebase_checkout(message))) {\n+\t\tmove->to = to;\n+\t} else if (!skip_rebase_step(message, REFLOG_STEP_START)) {\n+\t\treturn false;\n+\t}\n+\tif (move->to)\n+\t\tmove->to_len = strcspn(move->to, \"\\n\");\n+\treturn true;\n+}\n+\n+static void mark_branches(struct complete_reflogs *array)\n+{\n+\tstruct ref_store *refs = get_main_ref_store(the_repository);\n+\tstruct object_id oid;\n+\tconst char *head = refs_resolve_ref_unsafe(refs, \"HEAD\", 0, &oid, NULL);\n+\tchar *branch = NULL;\n+\tint unknown = array->nr;\n+\n+\tif (head)\n+\t\tbranch = branch_label(head, strlen(head), &oid);\n+\n+\tfor (int i = array->nr - 1; i >= 0; i--) {\n+\t\tstruct reflog_info *item = &array->items[i];\n+\t\tstruct branch_move move;\n+\n+\t\tif (!parse_branch_move(item->message, &move)) {\n+\t\t\titem->branch = xstrdup_or_null(branch);\n+\t\t\tcontinue;\n+\t\t}\n+\n+\t\tif (!branch) {\n+\t\t\tbranch = branch_label(move.to, move.to_len,\n+\t\t\t\t\t      &array->items[unknown - 1].noid);\n+\t\t\tfor (int j = i + 1; j < unknown; j++)\n+\t\t\t\tarray->items[j].branch = xstrdup(branch);\n+\t\t}\n+\t\titem->branch = branch;\n+\t\tif (move.from) {\n+\t\t\tbranch = branch_label(move.from, move.from_len,\n+\t\t\t\t\t      &item->ooid);\n+\t\t} else {\n+\t\t\tbranch = NULL;\n+\t\t\tunknown = i;\n+\t\t}\n+\t}\n+\n+\tif (!branch && unknown < array->nr)\n+\t\tfor (int i = 0; i < unknown; i++)\n+\t\t\tarray->items[i].branch =\n+\t\t\t\txstrdup(array->items[unknown].branch);\n+\tfree(branch);\n+}\n+\n static struct complete_reflogs *read_complete_reflog(const char *ref)\n {\n \tstruct complete_reflogs *reflogs =\n@@ -234,6 +359,16 @@ int add_reflog_for_walk(struct reflog_walk_info *info,\n \treturn 0;\n }\n \n+static const char *short_ref(struct complete_reflogs *reflogs)\n+{\n+\tstruct ref_store *refs = get_main_ref_store(the_repository);\n+\n+\tif (!reflogs->short_ref)\n+\t\treflogs->short_ref =\n+\t\t\trefs_shorten_unambiguous_ref(refs, reflogs->ref, 0);\n+\treturn reflogs->short_ref;\n+}\n+\n void get_reflog_selector(struct strbuf *sb,\n \t\t\t struct reflog_walk_info *reflog_info,\n \t\t\t struct date_mode dmode, int force_date,\n@@ -247,12 +382,7 @@ void get_reflog_selector(struct strbuf *sb,\n \t\treturn;\n \n \tif (shorten) {\n-\t\tif (!commit_reflog->reflogs->short_ref)\n-\t\t\tcommit_reflog->reflogs->short_ref\n-\t\t\t\t= refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t\t       commit_reflog->reflogs->ref,\n-\t\t\t\t\t\t\t       0);\n-\t\tprinted_ref = commit_reflog->reflogs->short_ref;\n+\t\tprinted_ref = short_ref(commit_reflog->reflogs);\n \t} else {\n \t\tprinted_ref = commit_reflog->reflogs->ref;\n \t}\n@@ -287,6 +417,31 @@ void get_reflog_message(struct strbuf *sb,\n \tstrbuf_add(sb, info->message, len);\n }\n \n+void get_reflog_branch(struct strbuf *sb,\n+\t\t       struct reflog_walk_info *reflog_info)\n+{\n+\tstruct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;\n+\tstruct complete_reflogs *reflogs;\n+\tconst char *branch;\n+\n+\tif (!commit_reflog)\n+\t\treturn;\n+\n+\treflogs = commit_reflog->reflogs;\n+\tif (strcmp(reflogs->ref, \"HEAD\")) {\n+\t\tstrbuf_addstr(sb, short_ref(reflogs));\n+\t\treturn;\n+\t}\n+\n+\tif (!reflogs->branches_marked) {\n+\t\tmark_branches(reflogs);\n+\t\treflogs->branches_marked = true;\n+\t}\n+\tbranch = reflogs->items[commit_reflog->recno+1].branch;\n+\tif (branch)\n+\t\tstrbuf_addstr(sb, branch);\n+}\n+\n const char *get_reflog_ident(struct reflog_walk_info *reflog_info)\n {\n \tstruct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;\ndiff --git a/reflog-walk.h b/reflog-walk.h\nindex 989583dc55..fc1f344ce6 100644\n--- a/reflog-walk.h\n+++ b/reflog-walk.h\n@@ -13,6 +13,8 @@ void show_reflog_message(struct reflog_walk_info *info, int,\n \t\t\t struct date_mode, int force_date);\n void get_reflog_message(struct strbuf *sb,\n \t\t\tstruct reflog_walk_info *reflog_info);\n+void get_reflog_branch(struct strbuf *sb,\n+\t\t       struct reflog_walk_info *reflog_info);\n const char *get_reflog_ident(struct reflog_walk_info *reflog_info);\n timestamp_t get_reflog_timestamp(struct reflog_walk_info *reflog_info);\n void get_reflog_selector(struct strbuf *sb,\ndiff --git a/t/t1414-reflog-walk.sh b/t/t1414-reflog-walk.sh\nindex be6c3f472c..21b0ae3856 100755\n--- a/t/t1414-reflog-walk.sh\n+++ b/t/t1414-reflog-walk.sh\n@@ -131,6 +131,143 @@ test_expect_success 'walk prefers reflog to ref tip' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success '%gb names the branch HEAD was on' '\n+\tgit init branches &&\n+\t(\n+\t\tcd branches &&\n+\t\ttest_commit base &&\n+\t\tgit switch -c topic &&\n+\t\ttest_commit A &&\n+\t\ttest_commit B &&\n+\t\tgit switch main &&\n+\t\ttest_commit upstream &&\n+\t\tgit switch topic &&\n+\t\tgit rebase main &&\n+\n+\t\tcat >expect <<-\\EOF &&\n+\t\t(topic) rebase (finish): returning to refs/heads/topic\n+\t\t(topic) rebase (pick): B\n+\t\t(topic) rebase (pick): A\n+\t\t(topic) rebase (start): checkout main\n+\t\t(topic) checkout: moving from main to topic\n+\t\t(main) commit: upstream\n+\t\t(main) checkout: moving from topic to main\n+\t\t(topic) commit: B\n+\t\t(topic) commit: A\n+\t\t(topic) checkout: moving from main to topic\n+\t\t(main) commit (initial): base\n+\t\tEOF\n+\t\tgit log -g --format=\"(%gb) %gs\" >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '%gb names the ref of other reflogs' '\n+\t(\n+\t\tcd branches &&\n+\t\techo main >expect &&\n+\t\tgit log -g -1 --format=%gb main >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '%gb follows \"git rebase <upstream> <branch>\"' '\n+\t(\n+\t\tcd branches &&\n+\t\tgit switch main &&\n+\t\ttest_commit main-change &&\n+\t\tgit rebase main topic &&\n+\t\tgit switch main &&\n+\t\tgit rebase main topic &&\n+\n+\t\tcat >expect <<-\\EOF &&\n+\t\t(topic) rebase: checkout topic\n+\t\t(main) checkout: moving from topic to main\n+\t\t(topic) rebase (finish): returning to refs/heads/topic\n+\t\t(topic) rebase (pick): B\n+\t\t(topic) rebase (pick): A\n+\t\t(topic) rebase (start): checkout main\n+\t\t(main) commit: main-change\n+\t\t(main) checkout: moving from topic to main\n+\t\tEOF\n+\t\tgit log -g -8 --format=\"(%gb) %gs\" >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '%gb follows \"git rebase <upstream> <branch>\" with GIT_REFLOG_ACTION' '\n+\t(\n+\t\tcd branches &&\n+\t\tgit switch main &&\n+\t\tGIT_REFLOG_ACTION=\"sync switch\" git rebase main topic &&\n+\n+\t\tcat >expect <<-\\EOF &&\n+\t\t(topic) sync switch: checkout topic\n+\t\t(main) checkout: moving from topic to main\n+\t\tEOF\n+\t\tgit log -g -2 --format=\"(%gb) %gs\" >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '%gb names a detached HEAD by the commit it was left at' '\n+\t(\n+\t\tcd branches &&\n+\t\tgit switch --detach topic~1 &&\n+\t\ttest_commit detached &&\n+\t\tgit rebase topic &&\n+\t\trebased=$(git rev-parse HEAD) &&\n+\t\tgit switch topic &&\n+\n+\t\tleft_before=$(git rev-parse --short detached) &&\n+\t\tleft_after=$(git rev-parse --short $rebased) &&\n+\t\tcat >expect <<-EOF &&\n+\t\t(topic) checkout: moving from $rebased to topic\n+\t\t(detached at $left_after) rebase (pick): detached\n+\t\t(detached at $left_after) rebase (start): checkout topic\n+\t\t(detached at $left_before) commit: detached\n+\t\t(detached at $left_before) checkout: moving from topic to topic~1\n+\t\tEOF\n+\t\tgit log -g -5 --format=\"(%gb) %gs\" >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '%gb names the branch of a \"git pull --rebase\" in a fresh clone' '\n+\tgit clone branches downstream &&\n+\t(\n+\t\tcd branches &&\n+\t\ttest_commit upstream-change\n+\t) &&\n+\t(\n+\t\tcd downstream &&\n+\t\ttest_commit local-change &&\n+\t\tgit pull --rebase &&\n+\n+\t\techo \"(topic)\" >expect &&\n+\t\tgit log -g --format=\"(%gb)\" >out &&\n+\t\tsort -u out >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '%gb follows a renamed branch' '\n+\t(\n+\t\tcd branches &&\n+\t\ttest_commit before-rename &&\n+\t\tgit branch -m topic renamed &&\n+\t\ttest_commit after-rename &&\n+\n+\t\tcat >expect <<-\\EOF &&\n+\t\t(renamed) commit: after-rename\n+\t\t(renamed) Branch: renamed refs/heads/topic to refs/heads/renamed\n+\t\t(topic) commit: before-rename\n+\t\tEOF\n+\t\tgit log -g -3 --format=\"(%gb) %gs\" >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n test_expect_success 'rev-list -g complains when there are no reflogs' '\n \ttest_must_fail git rev-list -g\n '\n-- \ngitgitgadget\n\n"},{"id":"557663","messageId":"edd1c36a-bd2a-4bcf-a6b1-3e08dfcda344@gmail.com","threadId":"66545","inReplyTo":"pull.2448.v4.git.git.1793826938.gitgitgadget@gmail.com","subject":"Re: [PATCH v4 0/2] reflog: add format %gb","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-11-06T11:08:05Z","receivedAt":"2026-11-06T11:08:05Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Harald\n\nOn 04/11/2026 21:15, Harald Nordgren via GitGitGadget wrote:\n> > It follows branch switches, branch renames and rebases, including \"git\n> rebase \", rebases of a detached HEAD and rebases of a branch that was\n> already up to date. When HEAD was detached, the entry shows \"detached at \".\n\nThe \"detached at \" labels show the commit before we switch away from a\ndetached HEAD, not the commit where HEAD was detached. The parsing of\n\"rebase: checkout <revision>\" is still not quite right - it treats\n\"origin/main\" as a branch name and is confused by commits where the\nsubject starts with \"checkout \" (I think the way to avoid that is to\ncheck that the reflog message does not match the commit subject [1]).\nHere is a failing test for those issues which also checks we handle\n':' in a url in $GIT_REFLOG_ACTION correctly.\n\ntest_expect_success '%gb labels branches and detached HEADs' '\n\ttest_create_repo origin &&\n\t(\n\t\tcd origin &&\n\t\ttest_commit O1 &&\n\t\ttest_commit O2\n\t) &&\n\tgit clone origin downstream &&\n\t(\n\t\tcd origin &&\n\t\ttest_commit O3\n\t) &&\n\t(\n\t\tcd downstream &&\n\t\ttest_commit L1 &&\n\t\tgit branch side &&\n\t\ttest_commit L2 &&\n\t\tgit pull -r \"file://$TRASH_DIRECTORY/origin\" &&\n\t\ttest_commit L3 &&\n\t\tGIT_REFLOG_ACTION=\"sync switch\" git rebase -i FETCH_HEAD side^0 &&\n\t\tH1=$(git rev-parse --short HEAD) &&\n\t\ttest_commit \"checkout main\" L4.t L4 L4 &&\n\t\tgit rebase FETCH_HEAD side &&\n\t\ttest_commit L5 &&\n\t\tgit rebase origin/main origin/main &&\n\t\tH2=$(git rev-parse --short HEAD) &&\n\t\ttest_commit L6 &&\n\t\tgit rebase main refs/heads/main &&\n\t\tH3=$(git rev-parse --short HEAD) &&\n\t\ttest_commit L7 &&\n\t\tgit switch side &&\n\t\ttest_commit L8 &&\n\t\tgit checkout origin/main &&\n\t\tH4=$(git rev-parse --short HEAD) &&\n\t\ttest_commit L9 &&\n\n\t\tcat >expect <<-EOF &&\n\t\tcommit: l9 (detached at $H4) &&\n\t\tcheckout: moving from side to origin/main (detached at $H4)\n\t\tcommit: L8 (side)\n\t\tcheckout: moving from $(git rev-parse L7) to side (side)\n\t\tcommit: L7 (detached at $H3)\n\t\trebase: checkout refs/heads/main (detached at $H3)\n\t\tcommit: L6 (detached at $H2)\n\t\trebase: checkout origin/main (detached at $H2)\n\t\tcommit: L5 (side)\n\t\trebase (finish): returning to refs/heads/side (side)\n\t\trebase (pick): L1 (side)\n\t\trebase (start): checkout FETCH_HEAD (side)\n\t\tcommit: checkout main (detached at $H1)\n\t\tsync switch (pick): L1 (detached at $H1)\n\t\tsync switch (start): checkout FETCH_HEAD (detached at $H1)\n\t\tcommit: L3 (main)\n\t\tpull -r file://$TRASH_DIRECTORY/origin (finish): returning to refs/heads/main (main)\n\t\tpull -r file://$TRASH_DIRECTORY/origin (pick): L2 (main)\n\t\tpull -r file://$TRASH_DIRECTORY/origin (pick): L1 (main)\n\t\tpull -r file://$TRASH_DIRECTORY/origin (start): checkout $(git rev-parse FETCH_HEAD) (main)\n\t\tcommit: L2 (main)\n\t\tcommit: L1 (main)\n\t\tclone: from $TRASH_DIRECTORY/origin (main)\n\t\tEOF\n\n\t\tgit log -g --format=\"%gs (%gb)\" >actual &&\n\t\ttest_cmp expect actual\n\t)\n'\n\nThanks\n\nPhillip\n\n[1] https://github.com/phillipwood/git/blob/c11b0dcb38d4dbe26c41053569d4fae27fdf0de6/object-name.c#L1301\n\n> > Changes in v4:\n> >   * %gb now names the right branch when git rebase <upstream> <branch> runs\n>     with GIT_REFLOG_ACTION set.\n>   * Reworded the %gb commit message.\n> > Changes in v3:\n> >   * Dropped --operations.\n>   * Replaced --historical-branch with a %gb placeholder for --format, e.g.\n>     git log -g --format='%h %gd: (%gb) %gs'.\n>   * %gb names the right branch after git rebase <upstream> <branch>, a rebase\n>     of a detached HEAD and an up-to-date rebase.\n>   * Named the returning to and checkout reflog strings in reflog.h too.\n> > Changes in v2:\n> >   * Move the \"historical branch\" rightward in the display for cleaner\n>     alignment.\n>   * Improve commit messages.\n> > Harald Nordgren (2):\n>    reflog: name the strings that mark switches, renames and rebase steps\n>    pretty: add %gb to name the branch of each reflog entry\n> >   Documentation/pretty-formats.adoc |   3 +\n>   builtin/branch.c                  |   3 +-\n>   builtin/checkout.c                |   3 +-\n>   builtin/rebase.c                  |  19 ++--\n>   object-name.c                     |   3 +-\n>   pretty.c                          |   4 +\n>   reflog-walk.c                     | 167 ++++++++++++++++++++++++++++--\n>   reflog-walk.h                     |   2 +\n>   reflog.h                          |   8 ++\n>   sequencer.c                       |  13 +--\n>   t/t1414-reflog-walk.sh            | 137 ++++++++++++++++++++++++\n>   wt-status.c                       |   3 +-\n>   12 files changed, 340 insertions(+), 25 deletions(-)\n> > > base-commit: 750f5de7dfe9a4062f95088212b063f189d50e91\n> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2448%2FHaraldNordgren%2Freflog-operations-v4\n> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2448/HaraldNordgren/reflog-operations-v4\n> Pull-Request: https://github.com/git/git/pull/2448\n> > Range-diff vs v3:\n> >   1:  4d779e8da3 = 1:  f960a24b8b reflog: name the strings that mark switches, renames and rebase steps\n>   2:  dee548f9b1 ! 2:  99a1047702 pretty: add %gb to name the branch of each reflog entry\n>       @@ Commit message\n>                1a2b3c4 HEAD@{3}: (main) commit: fix typo\n>                5d6e7f8 HEAD@{4}: (main) checkout: moving from topic to main\n>        >       -    The branch is read from the entries that branch switches, branch\n>       -    renames and rebases leave behind. The entries of a rebase are named\n>       -    after the branch being rebased. When HEAD was detached, the entry\n>       -    shows \"detached at <commit>\" with the commit HEAD was at when it\n>       -    moved on. When nothing older in the reflog says which branch a rebase\n>       -    started on, the rebase is taken to have started on the branch it\n>       -    rebased. For the reflog of any other ref, \"%gb\" is the name of that\n>       -    ref.\n>       +    The branch is read from the entries created by branch switches,\n>       +    branch renames and rebases. The entries of a rebase are labeled with\n>       +    the branch being rebased. When HEAD was detached, the entry shows\n>       +    \"detached at <commit>\" with the commit HEAD was at when it moved on.\n>       +    When nothing older in the reflog says which branch a rebase started\n>       +    on, the rebase is taken to have started on the branch it rebased. For\n>       +    the reflog of any other ref, \"%gb\" is the name of that ref.\n>        >            Signed-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n>        >       @@ reflog-walk.c: static void complete_reflogs_clear(void *util, const char *str UN\n>        +\treturn p;\n>        +}\n>        +\n>       ++static const char *skip_rebase_checkout(const char *message)\n>       ++{\n>       ++\tconst char *p = strstr(message, \" (\");\n>       ++\tconst char *colon = strstr(message, \": \");\n>       ++\n>       ++\tif (!colon || (p && p < colon) ||\n>       ++\t    !skip_prefix(colon + 2, REFLOG_CHECKOUT, &p))\n>       ++\t\treturn NULL;\n>       ++\treturn p;\n>       ++}\n>       ++\n>        +struct branch_move {\n>        +\tconst char *from, *to;\n>        +\tsize_t from_len, to_len;\n>       @@ reflog-walk.c: static void complete_reflogs_clear(void *util, const char *str UN\n>        +\t\t   skip_prefix(to, REFLOG_RETURNING_TO, &to)) {\n>        +\t\tmove->from = move->to = to;\n>        +\t\tmove->from_len = strcspn(to, \"\\n\");\n>       -+\t} else if (skip_prefix(message, \"rebase: \" REFLOG_CHECKOUT, &to)) {\n>       ++\t} else if ((to = skip_rebase_checkout(message))) {\n>        +\t\tmove->to = to;\n>        +\t} else if (!skip_rebase_step(message, REFLOG_STEP_START)) {\n>        +\t\treturn false;\n>       @@ t/t1414-reflog-walk.sh: test_expect_success 'walk prefers reflog to ref tip' '\n>        +\t)\n>        +'\n>        +\n>       ++test_expect_success '%gb follows \"git rebase <upstream> <branch>\" with GIT_REFLOG_ACTION' '\n>       ++\t(\n>       ++\t\tcd branches &&\n>       ++\t\tgit switch main &&\n>       ++\t\tGIT_REFLOG_ACTION=\"sync switch\" git rebase main topic &&\n>       ++\n>       ++\t\tcat >expect <<-\\EOF &&\n>       ++\t\t(topic) sync switch: checkout topic\n>       ++\t\t(main) checkout: moving from topic to main\n>       ++\t\tEOF\n>       ++\t\tgit log -g -2 --format=\"(%gb) %gs\" >actual &&\n>       ++\t\ttest_cmp expect actual\n>       ++\t)\n>       ++'\n>       ++\n>        +test_expect_success '%gb names a detached HEAD by the commit it was left at' '\n>        +\t(\n>        +\t\tcd branches &&\n> "},{"id":"557786","messageId":"CAHwyqnXNjM03XgFXtbLq-QH=ABdy2T2U7tfLDucezkY2EjTECQ@mail.gmail.com","threadId":"66545","inReplyTo":"edd1c36a-bd2a-4bcf-a6b1-3e08dfcda344@gmail.com","subject":"Re: [PATCH v4 0/2] reflog: add format %gb","fromName":"Harald Nordgren","fromEmail":"haraldnordgren@gmail.com","sentAt":"2026-11-07T09:59:39Z","receivedAt":"2026-11-07T09:59:39Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"Good point, and thanks a lot for the failing test!\n\n\nHarald\n\n"},{"id":"557787","messageId":"pull.2448.v5.git.git.1794052536.gitgitgadget@gmail.com","threadId":"66545","inReplyTo":"pull.2448.git.git.1792070038.gitgitgadget@gmail.com","subject":"[PATCH v5 0/2] reflog: add format %gb","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-07T11:55:34Z","receivedAt":"2026-11-07T11:55:34Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"Create a better reflog. Follows the discussion in\nhttps://lore.kernel.org/git/akZfm-igZKeHaDST@pks.im/.\n\nThe reflog of HEAD records the work done on every branch, so \"git reflog\"\nmixes them and does not say which entry was made on which branch. This adds\na \"%gb\" placeholder that names the branch HEAD was on when each entry was\nrecorded:\n\n$ git log -g --format='%h %gd: (%gb) %gs'\n9c8d7e6 HEAD@{0}: (topic) rebase (finish): returning to refs/heads/topic\n1a2b3c4 HEAD@{2}: (topic) rebase (start): checkout main\n1a2b3c4 HEAD@{3}: (main) commit: fix typo\n\n\nIt follows branch switches, branch renames and rebases, including \"git\nrebase \", rebases of a detached HEAD and rebases of a branch that was\nalready up to date. When HEAD was detached, the entry shows \"detached at \".\n\nChanges in v5:\n\n * detached at <commit> now names the commit HEAD was detached at, or for a\n   detached rebase, the commit it ended on.\n * git rebase <upstream> <rev> counts <rev> as a branch only if\n   refs/heads/<rev> exists, so origin/main and refs/heads/main show as\n   detached.\n * A commit whose subject starts with checkout is no longer taken for a\n   rebase checkout.\n * Added Phillip's test, and tests for a detached rebase that fast-forwards\n   and one that is aborted.\n\nChanges in v4:\n\n * %gb now names the right branch when git rebase <upstream> <branch> runs\n   with GIT_REFLOG_ACTION set.\n * Reworded the %gb commit message.\n\nChanges in v3:\n\n * Dropped --operations.\n * Replaced --historical-branch with a %gb placeholder for --format, e.g.\n   git log -g --format='%h %gd: (%gb) %gs'.\n * %gb names the right branch after git rebase <upstream> <branch>, a rebase\n   of a detached HEAD and an up-to-date rebase.\n * Named the returning to and checkout reflog strings in reflog.h too.\n\nChanges in v2:\n\n * Move the \"historical branch\" rightward in the display for cleaner\n   alignment.\n * Improve commit messages.\n\nHarald Nordgren (2):\n  reflog: name the strings that mark switches, renames and rebase steps\n  pretty: add %gb to name the branch of each reflog entry\n\n Documentation/pretty-formats.adoc |   3 +\n builtin/branch.c                  |   3 +-\n builtin/checkout.c                |   3 +-\n builtin/rebase.c                  |  19 +--\n object-name.c                     |   3 +-\n pretty.c                          |   4 +\n reflog-walk.c                     | 217 +++++++++++++++++++++++++++-\n reflog-walk.h                     |   2 +\n reflog.h                          |   9 ++\n sequencer.c                       |  15 +-\n t/t1414-reflog-walk.sh            | 229 ++++++++++++++++++++++++++++++\n wt-status.c                       |   3 +-\n 12 files changed, 484 insertions(+), 26 deletions(-)\n\n\nbase-commit: fb255012acbcbe81189cb8014da0280b09c66ecc\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2448%2FHaraldNordgren%2Freflog-operations-v5\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2448/HaraldNordgren/reflog-operations-v5\nPull-Request: https://github.com/git/git/pull/2448\n\nRange-diff vs v4:\n\n 1:  f960a24b8b ! 1:  115b5a81b7 reflog: name the strings that mark switches, renames and rebase steps\n     @@ Commit message\n          messages are written or read: the \"checkout: moving from \" prefix\n          written by \"git checkout\", the \"Branch: renamed \" prefix written by\n          \"git branch -m\", and the \"start\", \"finish\" and \"abort\" steps, the\n     -    \"returning to \" before the branch a rebase goes back to and the\n     -    \"checkout \" before what it checks out, all written by \"git rebase\".\n     +    \"returning to \" before the branch a rebase goes back to, the\n     +    \"checkout \" before what it checks out and the \"fast-forward\" it\n     +    records when a commit can be reused, all written by \"git rebase\".\n      \n          No change in behavior.\n      \n     @@ reflog.h\n      +#define REFLOG_STEP_ABORT \"abort\"\n      +#define REFLOG_RETURNING_TO \"returning to \"\n      +#define REFLOG_CHECKOUT \"checkout \"\n     ++#define REFLOG_FAST_FORWARD \"fast-forward\"\n      +\n       #define REFLOG_EXPIRE_TOTAL   (1 << 0)\n       #define REFLOG_EXPIRE_UNREACH (1 << 1)\n     @@ sequencer.c\n       #include \"sparse-index.h\"\n       #include \"strvec.h\"\n       #include \"quote.h\"\n     +@@ sequencer.c: static int fast_forward_to(struct repository *r,\n     + \tif (checkout_fast_forward(r, from, to, 1))\n     + \t\treturn -1; /* the callee should have complained already */\n     + \n     +-\tstrbuf_addf(&sb, \"%s: fast-forward\", action_name(opts));\n     ++\tstrbuf_addf(&sb, \"%s: \" REFLOG_FAST_FORWARD, action_name(opts));\n     + \n     + \ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n     + \t\t\t\t\t\t  0, &err);\n      @@ sequencer.c: static int checkout_onto(struct repository *r, struct replay_opts *opts,\n       \t\t\t RESET_WORKING_TREE_UPDATE_HEAD |\n       \t\t\t RESET_WORKING_TREE_UPDATE_ORIG_HEAD |\n 2:  99a1047702 ! 2:  34bb930908 pretty: add %gb to name the branch of each reflog entry\n     @@ Commit message\n          The branch is read from the entries created by branch switches,\n          branch renames and rebases. The entries of a rebase are labeled with\n          the branch being rebased. When HEAD was detached, the entry shows\n     -    \"detached at <commit>\" with the commit HEAD was at when it moved on.\n     +    \"detached at <commit>\" with the commit HEAD was detached at, or for a\n     +    rebase of a detached HEAD the commit the rebase ended on. These\n     +    entries only record what was typed, so when no newer entry says\n     +    otherwise, a name counts as a branch if a branch of that name exists.\n          When nothing older in the reflog says which branch a rebase started\n          on, the rebase is taken to have started on the branch it rebased. For\n          the reflog of any other ref, \"%gb\" is the name of that ref.\n     @@ reflog-walk.c: static void complete_reflogs_clear(void *util, const char *str UN\n       \tfree_complete_reflog(array);\n       }\n       \n     -+static char *branch_label(const char *name, size_t len,\n     -+\t\t\t  const struct object_id *oid)\n     -+{\n     -+\tif (name) {\n     -+\t\tstruct object_id hex_oid;\n     -+\t\tchar *branch;\n     -+\n     -+\t\tif (skip_prefix(name, \"refs/heads/\", &name))\n     -+\t\t\tlen -= strlen(\"refs/heads/\");\n     -+\t\tbranch = xmemdupz(name, len);\n     -+\t\tif (strcmp(branch, \"HEAD\") && get_oid_hex(branch, &hex_oid) &&\n     -+\t\t    !check_refname_format(branch, REFNAME_ALLOW_ONELEVEL))\n     -+\t\t\treturn branch;\n     -+\t\tfree(branch);\n     -+\t}\n     -+\treturn xstrfmt(\"detached at %s\",\n     -+\t\t       repo_find_unique_abbrev(the_repository, oid,\n     -+\t\t\t\t\t       DEFAULT_ABBREV));\n     -+}\n     -+\n      +static const char *skip_rebase_step(const char *message, const char *step)\n      +{\n      +\tconst char *p = strstr(message, \" (\");\n     @@ reflog-walk.c: static void complete_reflogs_clear(void *util, const char *str UN\n      +\treturn p;\n      +}\n      +\n     -+static const char *skip_rebase_checkout(const char *message)\n     ++static bool is_commit_subject(const char *text, const struct object_id *oid)\n      +{\n     -+\tconst char *p = strstr(message, \" (\");\n     -+\tconst char *colon = strstr(message, \": \");\n     ++\tstruct commit *commit = lookup_commit_reference_gently(the_repository,\n     ++\t\t\t\t\t\t\t       oid, 1);\n     ++\tconst char *buffer, *subject;\n     ++\tsize_t len;\n     ++\tbool ret;\n     ++\n     ++\tif (!commit)\n     ++\t\treturn false;\n     ++\tbuffer = repo_get_commit_buffer(the_repository, commit, NULL);\n     ++\tlen = find_commit_subject(buffer, &subject);\n     ++\tret = strcspn(text, \"\\n\") == len && !memcmp(text, subject, len);\n     ++\trepo_unuse_commit_buffer(the_repository, commit, buffer);\n     ++\treturn ret;\n     ++}\n      +\n     -+\tif (!colon || (p && p < colon) ||\n     -+\t    !skip_prefix(colon + 2, REFLOG_CHECKOUT, &p))\n     ++static const char *skip_rebase_checkout(const struct reflog_info *item)\n     ++{\n     ++\tconst char *colon = strstr(item->message, \": \");\n     ++\tconst char *rev;\n     ++\n     ++\tif (!colon || !skip_prefix(colon + 2, REFLOG_CHECKOUT, &rev) ||\n     ++\t    is_commit_subject(colon + 2, &item->noid))\n      +\t\treturn NULL;\n     -+\treturn p;\n     ++\treturn rev;\n     ++}\n     ++\n     ++static const char *existing_branch(const char *name)\n     ++{\n     ++\tchar *ref = xstrfmt(\"refs/heads/%.*s\", (int)strcspn(name, \"\\n\"), name);\n     ++\tbool exists = refs_ref_exists(get_main_ref_store(the_repository), ref);\n     ++\n     ++\tfree(ref);\n     ++\treturn exists ? name : NULL;\n      +}\n      +\n      +struct branch_move {\n      +\tconst char *from, *to;\n      +\tsize_t from_len, to_len;\n     ++\tbool from_detached;\n      +};\n      +\n     -+static bool parse_branch_move(const char *message, struct branch_move *move)\n     ++static bool parse_branch_move(const struct reflog_info *item,\n     ++\t\t\t      struct branch_move *move)\n      +{\n     -+\tconst char *to;\n     ++\tconst char *message = item->message;\n     ++\tconst char *from, *to, *end;\n     ++\tstruct object_id oid;\n      +\n     -+\tmove->from = move->to = NULL;\n     -+\tmove->from_len = move->to_len = 0;\n     -+\tif (skip_prefix(message, REFLOG_SWITCH_PREFIX, &move->from) ||\n     -+\t    skip_prefix(message, REFLOG_RENAME_PREFIX, &move->from)) {\n     -+\t\tto = strstr(move->from, \" to \");\n     ++\tmemset(move, 0, sizeof(*move));\n     ++\tif (skip_prefix(message, REFLOG_SWITCH_PREFIX, &from)) {\n     ++\t\tto = strstr(from, \" to \");\n      +\t\tif (!to)\n      +\t\t\treturn false;\n     -+\t\tmove->from_len = to - move->from;\n     -+\t\tmove->to = to + strlen(\" to \");\n     ++\t\tif (!parse_oid_hex(from, &oid, &end) && end == to) {\n     ++\t\t\tmove->from_detached = true;\n     ++\t\t} else {\n     ++\t\t\tmove->from = from;\n     ++\t\t\tmove->from_len = to - from;\n     ++\t\t}\n     ++\t\tmove->to = existing_branch(to + strlen(\" to \"));\n     ++\t} else if (skip_prefix(message, REFLOG_RENAME_PREFIX \"refs/heads/\",\n     ++\t\t\t       &from)) {\n     ++\t\tto = strstr(from, \" to refs/heads/\");\n     ++\t\tif (!to)\n     ++\t\t\treturn false;\n     ++\t\tmove->from = from;\n     ++\t\tmove->from_len = to - from;\n     ++\t\tmove->to = to + strlen(\" to refs/heads/\");\n      +\t} else if (((to = skip_rebase_step(message, REFLOG_STEP_FINISH)) ||\n      +\t\t    (to = skip_rebase_step(message, REFLOG_STEP_ABORT))) &&\n     -+\t\t   skip_prefix(to, REFLOG_RETURNING_TO, &to)) {\n     ++\t\t   skip_prefix(to, REFLOG_RETURNING_TO \"refs/heads/\", &to)) {\n      +\t\tmove->from = move->to = to;\n      +\t\tmove->from_len = strcspn(to, \"\\n\");\n     -+\t} else if ((to = skip_rebase_checkout(message))) {\n     -+\t\tmove->to = to;\n     -+\t} else if (!skip_rebase_step(message, REFLOG_STEP_START)) {\n     ++\t} else if (skip_rebase_step(message, REFLOG_STEP_START)) {\n     ++\t\treturn true;\n     ++\t} else if ((to = skip_rebase_checkout(item))) {\n     ++\t\tmove->to = existing_branch(to);\n     ++\t} else {\n      +\t\treturn false;\n      +\t}\n      +\tif (move->to)\n     @@ reflog-walk.c: static void complete_reflogs_clear(void *util, const char *str UN\n      +\treturn true;\n      +}\n      +\n     ++static char *detached_label(const struct complete_reflogs *array,\n     ++\t\t\t    int i, int end)\n     ++{\n     ++\tconst struct reflog_info *item = &array->items[i];\n     ++\n     ++\tif (skip_rebase_step(item->message, REFLOG_STEP_START)) {\n     ++\t\tsize_t len = strstr(item->message, \" (\") + 2 - item->message;\n     ++\n     ++\t\tfor (int j = end - 1; j > i; j--) {\n     ++\t\t\tconst char *message = array->items[j].message;\n     ++\n     ++\t\t\tif (!strncmp(message, item->message, len) ||\n     ++\t\t\t    starts_with(message,\n     ++\t\t\t\t\t\"rebase: \" REFLOG_FAST_FORWARD)) {\n     ++\t\t\t\titem = &array->items[j];\n     ++\t\t\t\tbreak;\n     ++\t\t\t}\n     ++\t\t}\n     ++\t}\n     ++\treturn xstrfmt(\"detached at %s\",\n     ++\t\t       repo_find_unique_abbrev(the_repository, &item->noid,\n     ++\t\t\t\t\t       DEFAULT_ABBREV));\n     ++}\n     ++\n      +static void mark_branches(struct complete_reflogs *array)\n      +{\n      +\tstruct ref_store *refs = get_main_ref_store(the_repository);\n     -+\tstruct object_id oid;\n     -+\tconst char *head = refs_resolve_ref_unsafe(refs, \"HEAD\", 0, &oid, NULL);\n     ++\tconst char *head = refs_resolve_ref_unsafe(refs, \"HEAD\", 0, NULL, NULL);\n      +\tchar *branch = NULL;\n     -+\tint unknown = array->nr;\n     ++\tbool detached = false;\n     ++\tint unlabeled = array->nr;\n      +\n     -+\tif (head)\n     -+\t\tbranch = branch_label(head, strlen(head), &oid);\n     ++\tif (head && skip_prefix(head, \"refs/heads/\", &head))\n     ++\t\tbranch = xstrdup(head);\n     ++\telse if (head)\n     ++\t\tdetached = true;\n      +\n      +\tfor (int i = array->nr - 1; i >= 0; i--) {\n      +\t\tstruct reflog_info *item = &array->items[i];\n      +\t\tstruct branch_move move;\n      +\n     -+\t\tif (!parse_branch_move(item->message, &move)) {\n     ++\t\tif (!parse_branch_move(item, &move)) {\n      +\t\t\titem->branch = xstrdup_or_null(branch);\n      +\t\t\tcontinue;\n      +\t\t}\n      +\n      +\t\tif (!branch) {\n     -+\t\t\tbranch = branch_label(move.to, move.to_len,\n     -+\t\t\t\t\t      &array->items[unknown - 1].noid);\n     -+\t\t\tfor (int j = i + 1; j < unknown; j++)\n     ++\t\t\tbranch = move.to && !detached ?\n     ++\t\t\t\txmemdupz(move.to, move.to_len) :\n     ++\t\t\t\tdetached_label(array, i, unlabeled);\n     ++\t\t\tfor (int j = i + 1; j < unlabeled; j++)\n      +\t\t\t\tarray->items[j].branch = xstrdup(branch);\n      +\t\t}\n      +\t\titem->branch = branch;\n     -+\t\tif (move.from) {\n     -+\t\t\tbranch = branch_label(move.from, move.from_len,\n     -+\t\t\t\t\t      &item->ooid);\n     -+\t\t} else {\n     -+\t\t\tbranch = NULL;\n     -+\t\t\tunknown = i;\n     -+\t\t}\n     ++\t\tbranch = move.from ? xmemdupz(move.from, move.from_len) : NULL;\n     ++\t\tdetached = move.from_detached;\n     ++\t\tunlabeled = i;\n      +\t}\n      +\n     -+\tif (!branch && unknown < array->nr)\n     -+\t\tfor (int i = 0; i < unknown; i++)\n     -+\t\t\tarray->items[i].branch =\n     -+\t\t\t\txstrdup(array->items[unknown].branch);\n     ++\tif (!branch) {\n     ++\t\tif (detached)\n     ++\t\t\tbranch = detached_label(array, 0, unlabeled);\n     ++\t\telse if (unlabeled < array->nr)\n     ++\t\t\tbranch = xstrdup(array->items[unlabeled].branch);\n     ++\t\tfor (int i = 0; branch && i < unlabeled; i++)\n     ++\t\t\tarray->items[i].branch = xstrdup(branch);\n     ++\t}\n      +\tfree(branch);\n      +}\n      +\n     @@ t/t1414-reflog-walk.sh: test_expect_success 'walk prefers reflog to ref tip' '\n      +\t)\n      +'\n      +\n     -+test_expect_success '%gb names a detached HEAD by the commit it was left at' '\n     ++test_expect_success '%gb names a detached HEAD by the commit it was detached at' '\n      +\t(\n      +\t\tcd branches &&\n      +\t\tgit switch --detach topic~1 &&\n     ++\t\tdetached_at=$(git rev-parse --short HEAD) &&\n      +\t\ttest_commit detached &&\n      +\t\tgit rebase topic &&\n      +\t\trebased=$(git rev-parse HEAD) &&\n      +\t\tgit switch topic &&\n      +\n     -+\t\tleft_before=$(git rev-parse --short detached) &&\n     -+\t\tleft_after=$(git rev-parse --short $rebased) &&\n     ++\t\trebased_at=$(git rev-parse --short $rebased) &&\n      +\t\tcat >expect <<-EOF &&\n      +\t\t(topic) checkout: moving from $rebased to topic\n     -+\t\t(detached at $left_after) rebase (pick): detached\n     -+\t\t(detached at $left_after) rebase (start): checkout topic\n     -+\t\t(detached at $left_before) commit: detached\n     -+\t\t(detached at $left_before) checkout: moving from topic to topic~1\n     ++\t\t(detached at $rebased_at) rebase (pick): detached\n     ++\t\t(detached at $rebased_at) rebase (start): checkout topic\n     ++\t\t(detached at $detached_at) commit: detached\n     ++\t\t(detached at $detached_at) checkout: moving from topic to topic~1\n      +\t\tEOF\n      +\t\tgit log -g -5 --format=\"(%gb) %gs\" >actual &&\n      +\t\ttest_cmp expect actual\n      +\t)\n      +'\n      +\n     -+test_expect_success '%gb names the branch of a \"git pull --rebase\" in a fresh clone' '\n     -+\tgit clone branches downstream &&\n     ++test_expect_success '%gb names a detached rebase by the commit it ended on' '\n     ++\twrite_script add-exec <<-\\EOF &&\n     ++\t{ echo \"exec true\" && cat \"$1\"; } >\"$1.new\" &&\n     ++\tmv \"$1.new\" \"$1\"\n     ++\tEOF\n      +\t(\n      +\t\tcd branches &&\n     -+\t\ttest_commit upstream-change\n     -+\t) &&\n     ++\t\tgit switch --detach topic^0 &&\n     ++\t\ttest_set_sequence_editor \"$TRASH_DIRECTORY/add-exec\" &&\n     ++\t\tgit rebase -i @^ &&\n     ++\t\tended_at=$(git rev-parse --short HEAD) &&\n     ++\t\ttest_commit after-fast-forward &&\n     ++\n     ++\t\tcat >expect <<-EOF &&\n     ++\t\t(detached at $ended_at) commit: after-fast-forward\n     ++\t\t(detached at $ended_at) rebase: fast-forward\n     ++\t\t(detached at $ended_at) rebase (start): checkout @^\n     ++\t\tEOF\n     ++\t\tgit log -g -3 --format=\"(%gb) %gs\" >actual &&\n     ++\t\ttest_cmp expect actual\n     ++\t)\n     ++'\n     ++\n     ++test_expect_success '%gb names an aborted rebase of a detached HEAD' '\n      +\t(\n     -+\t\tcd downstream &&\n     -+\t\ttest_commit local-change &&\n     -+\t\tgit pull --rebase &&\n     ++\t\tcd branches &&\n     ++\t\tgit switch --detach topic^0 &&\n     ++\t\ttest_must_fail git rebase -f -x false @^ &&\n     ++\t\tgit rebase --abort &&\n     ++\t\treturned_to=$(git rev-parse HEAD) &&\n     ++\t\tshort=$(git rev-parse --short HEAD) &&\n      +\n     -+\t\techo \"(topic)\" >expect &&\n     -+\t\tgit log -g --format=\"(%gb)\" >out &&\n     -+\t\tsort -u out >actual &&\n     ++\t\tcat >expect <<-EOF &&\n     ++\t\t(detached at $short) rebase (abort): returning to $returned_to\n     ++\t\t(detached at $short) rebase (pick): B\n     ++\t\t(detached at $short) rebase (start): checkout @^\n     ++\t\tEOF\n     ++\t\tgit log -g -3 --format=\"(%gb) %gs\" >actual &&\n      +\t\ttest_cmp expect actual\n      +\t)\n      +'\n     @@ t/t1414-reflog-walk.sh: test_expect_success 'walk prefers reflog to ref tip' '\n      +test_expect_success '%gb follows a renamed branch' '\n      +\t(\n      +\t\tcd branches &&\n     ++\t\tgit switch topic &&\n      +\t\ttest_commit before-rename &&\n      +\t\tgit branch -m topic renamed &&\n      +\t\ttest_commit after-rename &&\n     @@ t/t1414-reflog-walk.sh: test_expect_success 'walk prefers reflog to ref tip' '\n      +\t\ttest_cmp expect actual\n      +\t)\n      +'\n     ++\n     ++test_expect_success '%gb labels branches and detached HEADs' '\n     ++\ttest_create_repo origin &&\n     ++\t(\n     ++\t\tcd origin &&\n     ++\t\ttest_commit O1 &&\n     ++\t\ttest_commit O2\n     ++\t) &&\n     ++\tgit clone origin downstream &&\n     ++\t(\n     ++\t\tcd origin &&\n     ++\t\ttest_commit O3\n     ++\t) &&\n     ++\t(\n     ++\t\tcd downstream &&\n     ++\t\ttest_commit L1 &&\n     ++\t\tgit branch side &&\n     ++\t\ttest_commit L2 &&\n     ++\t\tgit pull -r \"file://$TRASH_DIRECTORY/origin\" &&\n     ++\t\ttest_commit L3 &&\n     ++\t\tGIT_REFLOG_ACTION=\"sync switch\" git rebase -i FETCH_HEAD side^0 &&\n     ++\t\tH1=$(git rev-parse --short HEAD) &&\n     ++\t\ttest_commit \"checkout main\" L4.t L4 L4 &&\n     ++\t\tgit rebase FETCH_HEAD side &&\n     ++\t\ttest_commit L5 &&\n     ++\t\tgit rebase origin/main origin/main &&\n     ++\t\tH2=$(git rev-parse --short HEAD) &&\n     ++\t\ttest_commit L6 &&\n     ++\t\tgit rebase main refs/heads/main &&\n     ++\t\tH3=$(git rev-parse --short HEAD) &&\n     ++\t\ttest_commit L7 &&\n     ++\t\tgit switch side &&\n     ++\t\ttest_commit L8 &&\n     ++\t\tgit checkout origin/main &&\n     ++\t\tH4=$(git rev-parse --short HEAD) &&\n     ++\t\ttest_commit L9 &&\n     ++\n     ++\t\tcat >expect <<-EOF &&\n     ++\t\tcommit: L9 (detached at $H4)\n     ++\t\tcheckout: moving from side to origin/main (detached at $H4)\n     ++\t\tcommit: L8 (side)\n     ++\t\tcheckout: moving from $(git rev-parse L7) to side (side)\n     ++\t\tcommit: L7 (detached at $H3)\n     ++\t\trebase: checkout refs/heads/main (detached at $H3)\n     ++\t\tcommit: L6 (detached at $H2)\n     ++\t\trebase: checkout origin/main (detached at $H2)\n     ++\t\tcommit: L5 (side)\n     ++\t\trebase (finish): returning to refs/heads/side (side)\n     ++\t\trebase (pick): L1 (side)\n     ++\t\trebase (start): checkout FETCH_HEAD (side)\n     ++\t\tcommit: checkout main (detached at $H1)\n     ++\t\tsync switch (pick): L1 (detached at $H1)\n     ++\t\tsync switch (start): checkout FETCH_HEAD (detached at $H1)\n     ++\t\tcommit: L3 (main)\n     ++\t\tpull -r file://$TRASH_DIRECTORY/origin (finish): returning to refs/heads/main (main)\n     ++\t\tpull -r file://$TRASH_DIRECTORY/origin (pick): L2 (main)\n     ++\t\tpull -r file://$TRASH_DIRECTORY/origin (pick): L1 (main)\n     ++\t\tpull -r file://$TRASH_DIRECTORY/origin (start): checkout $(git rev-parse FETCH_HEAD) (main)\n     ++\t\tcommit: L2 (main)\n     ++\t\tcommit: L1 (main)\n     ++\t\tclone: from $(cd ../origin && pwd) (main)\n     ++\t\tEOF\n     ++\n     ++\t\tgit log -g --format=\"%gs (%gb)\" >actual &&\n     ++\t\ttest_cmp expect actual\n     ++\t)\n     ++'\n      +\n       test_expect_success 'rev-list -g complains when there are no reflogs' '\n       \ttest_must_fail git rev-list -g\n\n-- \ngitgitgadget\n\n"},{"id":"557788","messageId":"115b5a81b7fffb9664a813c21f41d11bee57b98f.1794052536.git.gitgitgadget@gmail.com","threadId":"66545","inReplyTo":"pull.2448.v5.git.git.1794052536.gitgitgadget@gmail.com","subject":"[PATCH v5 1/2] reflog: name the strings that mark switches, renames and rebase steps","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-07T11:55:35Z","receivedAt":"2026-11-07T11:55:35Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nThe next commit adds a \"%gb\" placeholder that shows which branch HEAD\nwas on for each reflog entry. It finds the branch from the messages\nthat branch switches, branch renames and rebases leave in the reflog.\nThe commands that write these messages spell out the strings by hand,\nand \"@{-<n>}\" and \"git status\" each keep their own copy of the switch\nprefix to read it back. Reading them in one more place would add yet\nanother copy that has to stay in sync.\n\nName the strings once in reflog.h and use the names wherever the\nmessages are written or read: the \"checkout: moving from \" prefix\nwritten by \"git checkout\", the \"Branch: renamed \" prefix written by\n\"git branch -m\", and the \"start\", \"finish\" and \"abort\" steps, the\n\"returning to \" before the branch a rebase goes back to, the\n\"checkout \" before what it checks out and the \"fast-forward\" it\nrecords when a commit can be reused, all written by \"git rebase\".\n\nNo change in behavior.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n builtin/branch.c   |  3 ++-\n builtin/checkout.c |  3 ++-\n builtin/rebase.c   | 19 ++++++++++---------\n object-name.c      |  3 ++-\n reflog.h           |  9 +++++++++\n sequencer.c        | 15 ++++++++-------\n wt-status.c        |  3 ++-\n 7 files changed, 35 insertions(+), 20 deletions(-)\n\ndiff --git a/builtin/branch.c b/builtin/branch.c\nindex b536b619bb..f7acb0f349 100644\n--- a/builtin/branch.c\n+++ b/builtin/branch.c\n@@ -13,6 +13,7 @@\n #include \"editor.h\"\n #include \"environment.h\"\n #include \"refs.h\"\n+#include \"reflog.h\"\n #include \"commit.h\"\n #include \"gettext.h\"\n #include \"object-name.h\"\n@@ -669,7 +670,7 @@ static void copy_or_rename_branch(const char *oldname, const char *newname, int\n \t\tstrbuf_addf(&logmsg, \"Branch: copied %s to %s\",\n \t\t\t    oldref.buf, newref.buf);\n \telse\n-\t\tstrbuf_addf(&logmsg, \"Branch: renamed %s to %s\",\n+\t\tstrbuf_addf(&logmsg, REFLOG_RENAME_PREFIX \"%s to %s\",\n \t\t\t    oldref.buf, newref.buf);\n \n \tif (!copy && !(oldref_usage & IS_ORPHAN) &&\ndiff --git a/builtin/checkout.c b/builtin/checkout.c\nindex f28467f6df..cbdd78dfc8 100644\n--- a/builtin/checkout.c\n+++ b/builtin/checkout.c\n@@ -26,6 +26,7 @@\n #include \"preload-index.h\"\n #include \"read-cache.h\"\n #include \"refs.h\"\n+#include \"reflog.h\"\n #include \"remote.h\"\n #include \"repo-settings.h\"\n #include \"resolve-undo.h\"\n@@ -1043,7 +1044,7 @@ static void update_refs_for_switch(const struct checkout_opts *opts,\n \n \treflog_msg = getenv(\"GIT_REFLOG_ACTION\");\n \tif (!reflog_msg)\n-\t\tstrbuf_addf(&msg, \"checkout: moving from %s to %s\",\n+\t\tstrbuf_addf(&msg, REFLOG_SWITCH_PREFIX \"%s to %s\",\n \t\t\told_desc ? old_desc : \"(invalid)\", new_branch_info->name);\n \telse\n \t\tstrbuf_insertstr(&msg, 0, reflog_msg);\ndiff --git a/builtin/rebase.c b/builtin/rebase.c\nindex 535db60181..47a7b2fd3c 100644\n--- a/builtin/rebase.c\n+++ b/builtin/rebase.c\n@@ -17,6 +17,7 @@\n #include \"strvec.h\"\n #include \"dir.h\"\n #include \"refs.h\"\n+#include \"reflog.h\"\n #include \"config.h\"\n #include \"unpack-trees.h\"\n #include \"lockfile.h\"\n@@ -601,11 +602,11 @@ static int move_to_original_branch(struct rebase_options *opts)\n \tif (!opts->onto)\n \t\tBUG(\"move_to_original_branch without onto\");\n \n-\tstrbuf_addf(&branch_reflog, \"%s (finish): %s onto %s\",\n-\t\t    opts->reflog_action,\n+\tstrbuf_addf(&branch_reflog, \"%s (%s): %s onto %s\",\n+\t\t    opts->reflog_action, REFLOG_STEP_FINISH,\n \t\t    opts->head_name, oid_to_hex(&opts->onto->object.oid));\n-\tstrbuf_addf(&head_reflog, \"%s (finish): returning to %s\",\n-\t\t    opts->reflog_action, opts->head_name);\n+\tstrbuf_addf(&head_reflog, \"%s (%s): \" REFLOG_RETURNING_TO \"%s\",\n+\t\t    opts->reflog_action, REFLOG_STEP_FINISH, opts->head_name);\n \tropts.branch = opts->head_name;\n \tropts.flags = RESET_WORKING_TREE_REFS_ONLY |\n \t\t      RESET_WORKING_TREE_UPDATE_HEAD;\n@@ -867,7 +868,7 @@ static int checkout_up_to_date(struct rebase_options *options)\n \tstruct reset_working_tree_options ropts = { 0 };\n \tint ret = 0;\n \n-\tstrbuf_addf(&buf, \"%s: checkout %s\",\n+\tstrbuf_addf(&buf, \"%s: \" REFLOG_CHECKOUT \"%s\",\n \t\t    options->reflog_action, options->switch_to);\n \tropts.oid = &options->orig_head->object.oid;\n \tropts.branch = options->head_name;\n@@ -1413,8 +1414,8 @@ int cmd_rebase(int argc,\n \t\tif (read_basic_state(&options))\n \t\t\texit(1);\n \n-\t\tstrbuf_addf(&head_msg, \"%s (abort): returning to %s\",\n-\t\t\t    options.reflog_action,\n+\t\tstrbuf_addf(&head_msg, \"%s (%s): \" REFLOG_RETURNING_TO \"%s\",\n+\t\t\t    options.reflog_action, REFLOG_STEP_ABORT,\n \t\t\t    options.head_name ? options.head_name\n \t\t\t\t\t      : oid_to_hex(&options.orig_head->object.oid));\n \t\tropts.oid = &options.orig_head->object.oid;\n@@ -1884,8 +1885,8 @@ int cmd_rebase(int argc,\n \t\tprintf(_(\"First, rewinding head to replay your work on top of \"\n \t\t\t \"it...\\n\"));\n \n-\tstrbuf_addf(&msg, \"%s (start): checkout %s\",\n-\t\t    options.reflog_action, options.onto_name);\n+\tstrbuf_addf(&msg, \"%s (%s): \" REFLOG_CHECKOUT \"%s\",\n+\t\t    options.reflog_action, REFLOG_STEP_START, options.onto_name);\n \tropts.oid = &options.onto->object.oid;\n \tropts.orig_head = &options.orig_head->object.oid;\n \tropts.flags = RESET_WORKING_TREE_DETACH |\ndiff --git a/object-name.c b/object-name.c\nindex 6546685760..5b6ce8e0e0 100644\n--- a/object-name.c\n+++ b/object-name.c\n@@ -13,6 +13,7 @@\n #include \"tree.h\"\n #include \"tree-walk.h\"\n #include \"refs.h\"\n+#include \"reflog.h\"\n #include \"remote.h\"\n #include \"dir.h\"\n #include \"odb.h\"\n@@ -1251,7 +1252,7 @@ static int grab_nth_branch_switch(const char *refname UNUSED,\n \tconst char *match = NULL, *target = NULL;\n \tsize_t len;\n \n-\tif (skip_prefix(message, \"checkout: moving from \", &match))\n+\tif (skip_prefix(message, REFLOG_SWITCH_PREFIX, &match))\n \t\ttarget = strstr(match, \" to \");\n \n \tif (!match || !target)\ndiff --git a/reflog.h b/reflog.h\nindex 3bdd1ca8a5..d396d1df05 100644\n--- a/reflog.h\n+++ b/reflog.h\n@@ -2,6 +2,15 @@\n #define REFLOG_H\n #include \"refs.h\"\n \n+#define REFLOG_SWITCH_PREFIX \"checkout: moving from \"\n+#define REFLOG_RENAME_PREFIX \"Branch: renamed \"\n+#define REFLOG_STEP_START \"start\"\n+#define REFLOG_STEP_FINISH \"finish\"\n+#define REFLOG_STEP_ABORT \"abort\"\n+#define REFLOG_RETURNING_TO \"returning to \"\n+#define REFLOG_CHECKOUT \"checkout \"\n+#define REFLOG_FAST_FORWARD \"fast-forward\"\n+\n #define REFLOG_EXPIRE_TOTAL   (1 << 0)\n #define REFLOG_EXPIRE_UNREACH (1 << 1)\n \ndiff --git a/sequencer.c b/sequencer.c\nindex 4a1c0e70b8..5aa560315d 100644\n--- a/sequencer.c\n+++ b/sequencer.c\n@@ -31,6 +31,7 @@\n #include \"merge-ort.h\"\n #include \"merge-ort-wrappers.h\"\n #include \"refs.h\"\n+#include \"reflog.h\"\n #include \"sparse-index.h\"\n #include \"strvec.h\"\n #include \"quote.h\"\n@@ -664,7 +665,7 @@ static int fast_forward_to(struct repository *r,\n \tif (checkout_fast_forward(r, from, to, 1))\n \t\treturn -1; /* the callee should have complained already */\n \n-\tstrbuf_addf(&sb, \"%s: fast-forward\", action_name(opts));\n+\tstrbuf_addf(&sb, \"%s: \" REFLOG_FAST_FORWARD, action_name(opts));\n \n \ttransaction = ref_store_transaction_begin(get_main_ref_store(the_repository),\n \t\t\t\t\t\t  0, &err);\n@@ -5014,8 +5015,8 @@ static int checkout_onto(struct repository *r, struct replay_opts *opts,\n \t\t\t RESET_WORKING_TREE_UPDATE_HEAD |\n \t\t\t RESET_WORKING_TREE_UPDATE_ORIG_HEAD |\n \t\t\t RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK,\n-\t\t.head_msg = reflog_message(opts, \"start\", \"checkout %s\",\n-\t\t\t\t\t   onto_name),\n+\t\t.head_msg = reflog_message(opts, REFLOG_STEP_START,\n+\t\t\t\t\t   REFLOG_CHECKOUT \"%s\", onto_name),\n \t\t.default_reflog_action = sequencer_reflog_action(opts)\n \t};\n \tif (reset_working_tree(r, &ropts)) {\n@@ -5314,16 +5315,16 @@ cleanup_head_ref:\n \t\t\t\tres = error(_(\"could not read 'onto'\"));\n \t\t\t\tgoto cleanup_head_ref;\n \t\t\t}\n-\t\t\tmsg = reflog_message(opts, \"finish\", \"%s onto %s\",\n-\t\t\t\thead_ref.buf, buf.buf);\n+\t\t\tmsg = reflog_message(opts, REFLOG_STEP_FINISH,\n+\t\t\t\t\"%s onto %s\", head_ref.buf, buf.buf);\n \t\t\tif (refs_update_ref(get_main_ref_store(the_repository), msg, head_ref.buf, &head, &orig,\n \t\t\t\t\t    REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) {\n \t\t\t\tres = error(_(\"could not update %s\"),\n \t\t\t\t\thead_ref.buf);\n \t\t\t\tgoto cleanup_head_ref;\n \t\t\t}\n-\t\t\tmsg = reflog_message(opts, \"finish\", \"returning to %s\",\n-\t\t\t\thead_ref.buf);\n+\t\t\tmsg = reflog_message(opts, REFLOG_STEP_FINISH,\n+\t\t\t\tREFLOG_RETURNING_TO \"%s\", head_ref.buf);\n \t\t\tif (refs_update_symref(get_main_ref_store(the_repository), \"HEAD\", head_ref.buf, msg)) {\n \t\t\t\tres = error(_(\"could not update HEAD to %s\"),\n \t\t\t\t\thead_ref.buf);\ndiff --git a/wt-status.c b/wt-status.c\nindex 238bb48643..3516432338 100644\n--- a/wt-status.c\n+++ b/wt-status.c\n@@ -22,6 +22,7 @@\n #include \"strvec.h\"\n #include \"remote.h\"\n #include \"refs.h\"\n+#include \"reflog.h\"\n #include \"submodule.h\"\n #include \"column.h\"\n #include \"read-cache.h\"\n@@ -1790,7 +1791,7 @@ static int grab_1st_switch(const char *refname UNUSED,\n \tstruct grab_1st_switch_cbdata *cb = cb_data;\n \tconst char *target = NULL, *end;\n \n-\tif (!skip_prefix(message, \"checkout: moving from \", &message))\n+\tif (!skip_prefix(message, REFLOG_SWITCH_PREFIX, &message))\n \t\treturn 0;\n \ttarget = strstr(message, \" to \");\n \tif (!target)\n-- \ngitgitgadget\n\n\n"},{"id":"557789","messageId":"34bb9309088b2ff9252d22456b504c3e20b63bef.1794052536.git.gitgitgadget@gmail.com","threadId":"66545","inReplyTo":"pull.2448.v5.git.git.1794052536.gitgitgadget@gmail.com","subject":"[PATCH v5 2/2] pretty: add %gb to name the branch of each reflog entry","fromName":"Harald Nordgren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2026-11-07T11:55:36Z","receivedAt":"2026-11-07T11:55:36Z","isPatch":true,"sender":{"key":"haraldnordgren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9569897?v=4"},"body":"From: Harald Nordgren <haraldnordgren@gmail.com>\n\nThe reflog of HEAD records the work done on every branch, so the\noutput of \"git reflog\" mixes them and does not say which entry was\nmade on which branch. Working that out by hand means looking back for\nthe branch switch before each entry, and a rebase can move HEAD to\nanother branch without writing one, as \"git rebase <upstream>\n<branch>\" does.\n\nAdd a \"%gb\" placeholder that names the branch HEAD was on when each\nentry was recorded:\n\n    $ git log -g --format='%h %gd: (%gb) %gs'\n    9c8d7e6 HEAD@{0}: (topic) rebase (finish): returning to refs/heads/topic\n    9c8d7e6 HEAD@{1}: (topic) rebase (pick): add tests\n    1a2b3c4 HEAD@{2}: (topic) rebase (start): checkout main\n    1a2b3c4 HEAD@{3}: (main) commit: fix typo\n    5d6e7f8 HEAD@{4}: (main) checkout: moving from topic to main\n\nThe branch is read from the entries created by branch switches,\nbranch renames and rebases. The entries of a rebase are labeled with\nthe branch being rebased. When HEAD was detached, the entry shows\n\"detached at <commit>\" with the commit HEAD was detached at, or for a\nrebase of a detached HEAD the commit the rebase ended on. These\nentries only record what was typed, so when no newer entry says\notherwise, a name counts as a branch if a branch of that name exists.\nWhen nothing older in the reflog says which branch a rebase started\non, the rebase is taken to have started on the branch it rebased. For\nthe reflog of any other ref, \"%gb\" is the name of that ref.\n\nSigned-off-by: Harald Nordgren <haraldnordgren@gmail.com>\n---\n Documentation/pretty-formats.adoc |   3 +\n pretty.c                          |   4 +\n reflog-walk.c                     | 217 +++++++++++++++++++++++++++-\n reflog-walk.h                     |   2 +\n t/t1414-reflog-walk.sh            | 229 ++++++++++++++++++++++++++++++\n 5 files changed, 449 insertions(+), 6 deletions(-)\n\ndiff --git a/Documentation/pretty-formats.adoc b/Documentation/pretty-formats.adoc\nindex 2ae0eb11a9..a8d00397c4 100644\n--- a/Documentation/pretty-formats.adoc\n+++ b/Documentation/pretty-formats.adoc\n@@ -334,6 +334,9 @@ endif::git-rev-list[]\n +%gE+:: reflog identity email (respecting .mailmap, see\n \tlinkgit:git-shortlog[1] or linkgit:git-blame[1])\n +%gs+:: reflog subject\n++%gb+:: reflog branch, the branch `HEAD` was on when the entry was\n+\trecorded, e.g., `topic` or `detached at 1a2b3c4`. For the reflog\n+\tof any other ref, this is the shortened refname.\n ++%(trailers++`[:<option>,...]`++)++::\n display the trailers of the body as interpreted by\n linkgit:git-interpret-trailers[1]. The `trailers` string may be followed by\ndiff --git a/pretty.c b/pretty.c\nindex d8a9f370f6..1b3f7e2dc3 100644\n--- a/pretty.c\n+++ b/pretty.c\n@@ -1637,6 +1637,10 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */\n \t\t\tif (c->pretty_ctx->reflog_info)\n \t\t\t\tget_reflog_message(sb, c->pretty_ctx->reflog_info);\n \t\t\treturn 2;\n+\t\tcase 'b':\t/* reflog branch */\n+\t\t\tif (c->pretty_ctx->reflog_info)\n+\t\t\t\tget_reflog_branch(sb, c->pretty_ctx->reflog_info);\n+\t\t\treturn 2;\n \t\tcase 'n':\n \t\tcase 'N':\n \t\tcase 'e':\ndiff --git a/reflog-walk.c b/reflog-walk.c\nindex 4dbeaa93a7..2372823e9d 100644\n--- a/reflog-walk.c\n+++ b/reflog-walk.c\n@@ -3,7 +3,10 @@\n #include \"git-compat-util.h\"\n #include \"commit.h\"\n #include \"refs.h\"\n+#include \"reflog.h\"\n #include \"diff.h\"\n+#include \"hex.h\"\n+#include \"object-name.h\"\n #include \"repository.h\"\n #include \"revision.h\"\n #include \"string-list.h\"\n@@ -18,8 +21,10 @@ struct complete_reflogs {\n \t\ttimestamp_t timestamp;\n \t\tint tz;\n \t\tchar *message;\n+\t\tchar *branch;\n \t} *items;\n \tint nr, alloc;\n+\tbool branches_marked;\n };\n \n static int read_one_reflog(const char *refname UNUSED,\n@@ -38,6 +43,7 @@ static int read_one_reflog(const char *refname UNUSED,\n \titem->timestamp = timestamp;\n \titem->tz = tz;\n \titem->message = xstrdup(message);\n+\titem->branch = NULL;\n \tarray->nr++;\n \treturn 0;\n }\n@@ -52,6 +58,7 @@ static void free_complete_reflog(struct complete_reflogs *array)\n \tfor (i = 0; i < array->nr; i++) {\n \t\tfree(array->items[i].email);\n \t\tfree(array->items[i].message);\n+\t\tfree(array->items[i].branch);\n \t}\n \tfree(array->items);\n \tfree(array->ref);\n@@ -65,6 +72,174 @@ static void complete_reflogs_clear(void *util, const char *str UNUSED)\n \tfree_complete_reflog(array);\n }\n \n+static const char *skip_rebase_step(const char *message, const char *step)\n+{\n+\tconst char *p = strstr(message, \" (\");\n+\tconst char *colon = strstr(message, \": \");\n+\n+\tif (!p || (colon && colon < p) ||\n+\t    !skip_prefix(p + 2, step, &p) || !skip_prefix(p, \"): \", &p))\n+\t\treturn NULL;\n+\treturn p;\n+}\n+\n+static bool is_commit_subject(const char *text, const struct object_id *oid)\n+{\n+\tstruct commit *commit = lookup_commit_reference_gently(the_repository,\n+\t\t\t\t\t\t\t       oid, 1);\n+\tconst char *buffer, *subject;\n+\tsize_t len;\n+\tbool ret;\n+\n+\tif (!commit)\n+\t\treturn false;\n+\tbuffer = repo_get_commit_buffer(the_repository, commit, NULL);\n+\tlen = find_commit_subject(buffer, &subject);\n+\tret = strcspn(text, \"\\n\") == len && !memcmp(text, subject, len);\n+\trepo_unuse_commit_buffer(the_repository, commit, buffer);\n+\treturn ret;\n+}\n+\n+static const char *skip_rebase_checkout(const struct reflog_info *item)\n+{\n+\tconst char *colon = strstr(item->message, \": \");\n+\tconst char *rev;\n+\n+\tif (!colon || !skip_prefix(colon + 2, REFLOG_CHECKOUT, &rev) ||\n+\t    is_commit_subject(colon + 2, &item->noid))\n+\t\treturn NULL;\n+\treturn rev;\n+}\n+\n+static const char *existing_branch(const char *name)\n+{\n+\tchar *ref = xstrfmt(\"refs/heads/%.*s\", (int)strcspn(name, \"\\n\"), name);\n+\tbool exists = refs_ref_exists(get_main_ref_store(the_repository), ref);\n+\n+\tfree(ref);\n+\treturn exists ? name : NULL;\n+}\n+\n+struct branch_move {\n+\tconst char *from, *to;\n+\tsize_t from_len, to_len;\n+\tbool from_detached;\n+};\n+\n+static bool parse_branch_move(const struct reflog_info *item,\n+\t\t\t      struct branch_move *move)\n+{\n+\tconst char *message = item->message;\n+\tconst char *from, *to, *end;\n+\tstruct object_id oid;\n+\n+\tmemset(move, 0, sizeof(*move));\n+\tif (skip_prefix(message, REFLOG_SWITCH_PREFIX, &from)) {\n+\t\tto = strstr(from, \" to \");\n+\t\tif (!to)\n+\t\t\treturn false;\n+\t\tif (!parse_oid_hex(from, &oid, &end) && end == to) {\n+\t\t\tmove->from_detached = true;\n+\t\t} else {\n+\t\t\tmove->from = from;\n+\t\t\tmove->from_len = to - from;\n+\t\t}\n+\t\tmove->to = existing_branch(to + strlen(\" to \"));\n+\t} else if (skip_prefix(message, REFLOG_RENAME_PREFIX \"refs/heads/\",\n+\t\t\t       &from)) {\n+\t\tto = strstr(from, \" to refs/heads/\");\n+\t\tif (!to)\n+\t\t\treturn false;\n+\t\tmove->from = from;\n+\t\tmove->from_len = to - from;\n+\t\tmove->to = to + strlen(\" to refs/heads/\");\n+\t} else if (((to = skip_rebase_step(message, REFLOG_STEP_FINISH)) ||\n+\t\t    (to = skip_rebase_step(message, REFLOG_STEP_ABORT))) &&\n+\t\t   skip_prefix(to, REFLOG_RETURNING_TO \"refs/heads/\", &to)) {\n+\t\tmove->from = move->to = to;\n+\t\tmove->from_len = strcspn(to, \"\\n\");\n+\t} else if (skip_rebase_step(message, REFLOG_STEP_START)) {\n+\t\treturn true;\n+\t} else if ((to = skip_rebase_checkout(item))) {\n+\t\tmove->to = existing_branch(to);\n+\t} else {\n+\t\treturn false;\n+\t}\n+\tif (move->to)\n+\t\tmove->to_len = strcspn(move->to, \"\\n\");\n+\treturn true;\n+}\n+\n+static char *detached_label(const struct complete_reflogs *array,\n+\t\t\t    int i, int end)\n+{\n+\tconst struct reflog_info *item = &array->items[i];\n+\n+\tif (skip_rebase_step(item->message, REFLOG_STEP_START)) {\n+\t\tsize_t len = strstr(item->message, \" (\") + 2 - item->message;\n+\n+\t\tfor (int j = end - 1; j > i; j--) {\n+\t\t\tconst char *message = array->items[j].message;\n+\n+\t\t\tif (!strncmp(message, item->message, len) ||\n+\t\t\t    starts_with(message,\n+\t\t\t\t\t\"rebase: \" REFLOG_FAST_FORWARD)) {\n+\t\t\t\titem = &array->items[j];\n+\t\t\t\tbreak;\n+\t\t\t}\n+\t\t}\n+\t}\n+\treturn xstrfmt(\"detached at %s\",\n+\t\t       repo_find_unique_abbrev(the_repository, &item->noid,\n+\t\t\t\t\t       DEFAULT_ABBREV));\n+}\n+\n+static void mark_branches(struct complete_reflogs *array)\n+{\n+\tstruct ref_store *refs = get_main_ref_store(the_repository);\n+\tconst char *head = refs_resolve_ref_unsafe(refs, \"HEAD\", 0, NULL, NULL);\n+\tchar *branch = NULL;\n+\tbool detached = false;\n+\tint unlabeled = array->nr;\n+\n+\tif (head && skip_prefix(head, \"refs/heads/\", &head))\n+\t\tbranch = xstrdup(head);\n+\telse if (head)\n+\t\tdetached = true;\n+\n+\tfor (int i = array->nr - 1; i >= 0; i--) {\n+\t\tstruct reflog_info *item = &array->items[i];\n+\t\tstruct branch_move move;\n+\n+\t\tif (!parse_branch_move(item, &move)) {\n+\t\t\titem->branch = xstrdup_or_null(branch);\n+\t\t\tcontinue;\n+\t\t}\n+\n+\t\tif (!branch) {\n+\t\t\tbranch = move.to && !detached ?\n+\t\t\t\txmemdupz(move.to, move.to_len) :\n+\t\t\t\tdetached_label(array, i, unlabeled);\n+\t\t\tfor (int j = i + 1; j < unlabeled; j++)\n+\t\t\t\tarray->items[j].branch = xstrdup(branch);\n+\t\t}\n+\t\titem->branch = branch;\n+\t\tbranch = move.from ? xmemdupz(move.from, move.from_len) : NULL;\n+\t\tdetached = move.from_detached;\n+\t\tunlabeled = i;\n+\t}\n+\n+\tif (!branch) {\n+\t\tif (detached)\n+\t\t\tbranch = detached_label(array, 0, unlabeled);\n+\t\telse if (unlabeled < array->nr)\n+\t\t\tbranch = xstrdup(array->items[unlabeled].branch);\n+\t\tfor (int i = 0; branch && i < unlabeled; i++)\n+\t\t\tarray->items[i].branch = xstrdup(branch);\n+\t}\n+\tfree(branch);\n+}\n+\n static struct complete_reflogs *read_complete_reflog(const char *ref)\n {\n \tstruct complete_reflogs *reflogs =\n@@ -234,6 +409,16 @@ int add_reflog_for_walk(struct reflog_walk_info *info,\n \treturn 0;\n }\n \n+static const char *short_ref(struct complete_reflogs *reflogs)\n+{\n+\tstruct ref_store *refs = get_main_ref_store(the_repository);\n+\n+\tif (!reflogs->short_ref)\n+\t\treflogs->short_ref =\n+\t\t\trefs_shorten_unambiguous_ref(refs, reflogs->ref, 0);\n+\treturn reflogs->short_ref;\n+}\n+\n void get_reflog_selector(struct strbuf *sb,\n \t\t\t struct reflog_walk_info *reflog_info,\n \t\t\t struct date_mode dmode, int force_date,\n@@ -247,12 +432,7 @@ void get_reflog_selector(struct strbuf *sb,\n \t\treturn;\n \n \tif (shorten) {\n-\t\tif (!commit_reflog->reflogs->short_ref)\n-\t\t\tcommit_reflog->reflogs->short_ref\n-\t\t\t\t= refs_shorten_unambiguous_ref(get_main_ref_store(the_repository),\n-\t\t\t\t\t\t\t       commit_reflog->reflogs->ref,\n-\t\t\t\t\t\t\t       0);\n-\t\tprinted_ref = commit_reflog->reflogs->short_ref;\n+\t\tprinted_ref = short_ref(commit_reflog->reflogs);\n \t} else {\n \t\tprinted_ref = commit_reflog->reflogs->ref;\n \t}\n@@ -287,6 +467,31 @@ void get_reflog_message(struct strbuf *sb,\n \tstrbuf_add(sb, info->message, len);\n }\n \n+void get_reflog_branch(struct strbuf *sb,\n+\t\t       struct reflog_walk_info *reflog_info)\n+{\n+\tstruct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;\n+\tstruct complete_reflogs *reflogs;\n+\tconst char *branch;\n+\n+\tif (!commit_reflog)\n+\t\treturn;\n+\n+\treflogs = commit_reflog->reflogs;\n+\tif (strcmp(reflogs->ref, \"HEAD\")) {\n+\t\tstrbuf_addstr(sb, short_ref(reflogs));\n+\t\treturn;\n+\t}\n+\n+\tif (!reflogs->branches_marked) {\n+\t\tmark_branches(reflogs);\n+\t\treflogs->branches_marked = true;\n+\t}\n+\tbranch = reflogs->items[commit_reflog->recno+1].branch;\n+\tif (branch)\n+\t\tstrbuf_addstr(sb, branch);\n+}\n+\n const char *get_reflog_ident(struct reflog_walk_info *reflog_info)\n {\n \tstruct commit_reflog *commit_reflog = reflog_info->last_commit_reflog;\ndiff --git a/reflog-walk.h b/reflog-walk.h\nindex 989583dc55..fc1f344ce6 100644\n--- a/reflog-walk.h\n+++ b/reflog-walk.h\n@@ -13,6 +13,8 @@ void show_reflog_message(struct reflog_walk_info *info, int,\n \t\t\t struct date_mode, int force_date);\n void get_reflog_message(struct strbuf *sb,\n \t\t\tstruct reflog_walk_info *reflog_info);\n+void get_reflog_branch(struct strbuf *sb,\n+\t\t       struct reflog_walk_info *reflog_info);\n const char *get_reflog_ident(struct reflog_walk_info *reflog_info);\n timestamp_t get_reflog_timestamp(struct reflog_walk_info *reflog_info);\n void get_reflog_selector(struct strbuf *sb,\ndiff --git a/t/t1414-reflog-walk.sh b/t/t1414-reflog-walk.sh\nindex be6c3f472c..456c84d98d 100755\n--- a/t/t1414-reflog-walk.sh\n+++ b/t/t1414-reflog-walk.sh\n@@ -131,6 +131,235 @@ test_expect_success 'walk prefers reflog to ref tip' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success '%gb names the branch HEAD was on' '\n+\tgit init branches &&\n+\t(\n+\t\tcd branches &&\n+\t\ttest_commit base &&\n+\t\tgit switch -c topic &&\n+\t\ttest_commit A &&\n+\t\ttest_commit B &&\n+\t\tgit switch main &&\n+\t\ttest_commit upstream &&\n+\t\tgit switch topic &&\n+\t\tgit rebase main &&\n+\n+\t\tcat >expect <<-\\EOF &&\n+\t\t(topic) rebase (finish): returning to refs/heads/topic\n+\t\t(topic) rebase (pick): B\n+\t\t(topic) rebase (pick): A\n+\t\t(topic) rebase (start): checkout main\n+\t\t(topic) checkout: moving from main to topic\n+\t\t(main) commit: upstream\n+\t\t(main) checkout: moving from topic to main\n+\t\t(topic) commit: B\n+\t\t(topic) commit: A\n+\t\t(topic) checkout: moving from main to topic\n+\t\t(main) commit (initial): base\n+\t\tEOF\n+\t\tgit log -g --format=\"(%gb) %gs\" >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '%gb names the ref of other reflogs' '\n+\t(\n+\t\tcd branches &&\n+\t\techo main >expect &&\n+\t\tgit log -g -1 --format=%gb main >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '%gb follows \"git rebase <upstream> <branch>\"' '\n+\t(\n+\t\tcd branches &&\n+\t\tgit switch main &&\n+\t\ttest_commit main-change &&\n+\t\tgit rebase main topic &&\n+\t\tgit switch main &&\n+\t\tgit rebase main topic &&\n+\n+\t\tcat >expect <<-\\EOF &&\n+\t\t(topic) rebase: checkout topic\n+\t\t(main) checkout: moving from topic to main\n+\t\t(topic) rebase (finish): returning to refs/heads/topic\n+\t\t(topic) rebase (pick): B\n+\t\t(topic) rebase (pick): A\n+\t\t(topic) rebase (start): checkout main\n+\t\t(main) commit: main-change\n+\t\t(main) checkout: moving from topic to main\n+\t\tEOF\n+\t\tgit log -g -8 --format=\"(%gb) %gs\" >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '%gb follows \"git rebase <upstream> <branch>\" with GIT_REFLOG_ACTION' '\n+\t(\n+\t\tcd branches &&\n+\t\tgit switch main &&\n+\t\tGIT_REFLOG_ACTION=\"sync switch\" git rebase main topic &&\n+\n+\t\tcat >expect <<-\\EOF &&\n+\t\t(topic) sync switch: checkout topic\n+\t\t(main) checkout: moving from topic to main\n+\t\tEOF\n+\t\tgit log -g -2 --format=\"(%gb) %gs\" >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '%gb names a detached HEAD by the commit it was detached at' '\n+\t(\n+\t\tcd branches &&\n+\t\tgit switch --detach topic~1 &&\n+\t\tdetached_at=$(git rev-parse --short HEAD) &&\n+\t\ttest_commit detached &&\n+\t\tgit rebase topic &&\n+\t\trebased=$(git rev-parse HEAD) &&\n+\t\tgit switch topic &&\n+\n+\t\trebased_at=$(git rev-parse --short $rebased) &&\n+\t\tcat >expect <<-EOF &&\n+\t\t(topic) checkout: moving from $rebased to topic\n+\t\t(detached at $rebased_at) rebase (pick): detached\n+\t\t(detached at $rebased_at) rebase (start): checkout topic\n+\t\t(detached at $detached_at) commit: detached\n+\t\t(detached at $detached_at) checkout: moving from topic to topic~1\n+\t\tEOF\n+\t\tgit log -g -5 --format=\"(%gb) %gs\" >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '%gb names a detached rebase by the commit it ended on' '\n+\twrite_script add-exec <<-\\EOF &&\n+\t{ echo \"exec true\" && cat \"$1\"; } >\"$1.new\" &&\n+\tmv \"$1.new\" \"$1\"\n+\tEOF\n+\t(\n+\t\tcd branches &&\n+\t\tgit switch --detach topic^0 &&\n+\t\ttest_set_sequence_editor \"$TRASH_DIRECTORY/add-exec\" &&\n+\t\tgit rebase -i @^ &&\n+\t\tended_at=$(git rev-parse --short HEAD) &&\n+\t\ttest_commit after-fast-forward &&\n+\n+\t\tcat >expect <<-EOF &&\n+\t\t(detached at $ended_at) commit: after-fast-forward\n+\t\t(detached at $ended_at) rebase: fast-forward\n+\t\t(detached at $ended_at) rebase (start): checkout @^\n+\t\tEOF\n+\t\tgit log -g -3 --format=\"(%gb) %gs\" >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '%gb names an aborted rebase of a detached HEAD' '\n+\t(\n+\t\tcd branches &&\n+\t\tgit switch --detach topic^0 &&\n+\t\ttest_must_fail git rebase -f -x false @^ &&\n+\t\tgit rebase --abort &&\n+\t\treturned_to=$(git rev-parse HEAD) &&\n+\t\tshort=$(git rev-parse --short HEAD) &&\n+\n+\t\tcat >expect <<-EOF &&\n+\t\t(detached at $short) rebase (abort): returning to $returned_to\n+\t\t(detached at $short) rebase (pick): B\n+\t\t(detached at $short) rebase (start): checkout @^\n+\t\tEOF\n+\t\tgit log -g -3 --format=\"(%gb) %gs\" >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '%gb follows a renamed branch' '\n+\t(\n+\t\tcd branches &&\n+\t\tgit switch topic &&\n+\t\ttest_commit before-rename &&\n+\t\tgit branch -m topic renamed &&\n+\t\ttest_commit after-rename &&\n+\n+\t\tcat >expect <<-\\EOF &&\n+\t\t(renamed) commit: after-rename\n+\t\t(renamed) Branch: renamed refs/heads/topic to refs/heads/renamed\n+\t\t(topic) commit: before-rename\n+\t\tEOF\n+\t\tgit log -g -3 --format=\"(%gb) %gs\" >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success '%gb labels branches and detached HEADs' '\n+\ttest_create_repo origin &&\n+\t(\n+\t\tcd origin &&\n+\t\ttest_commit O1 &&\n+\t\ttest_commit O2\n+\t) &&\n+\tgit clone origin downstream &&\n+\t(\n+\t\tcd origin &&\n+\t\ttest_commit O3\n+\t) &&\n+\t(\n+\t\tcd downstream &&\n+\t\ttest_commit L1 &&\n+\t\tgit branch side &&\n+\t\ttest_commit L2 &&\n+\t\tgit pull -r \"file://$TRASH_DIRECTORY/origin\" &&\n+\t\ttest_commit L3 &&\n+\t\tGIT_REFLOG_ACTION=\"sync switch\" git rebase -i FETCH_HEAD side^0 &&\n+\t\tH1=$(git rev-parse --short HEAD) &&\n+\t\ttest_commit \"checkout main\" L4.t L4 L4 &&\n+\t\tgit rebase FETCH_HEAD side &&\n+\t\ttest_commit L5 &&\n+\t\tgit rebase origin/main origin/main &&\n+\t\tH2=$(git rev-parse --short HEAD) &&\n+\t\ttest_commit L6 &&\n+\t\tgit rebase main refs/heads/main &&\n+\t\tH3=$(git rev-parse --short HEAD) &&\n+\t\ttest_commit L7 &&\n+\t\tgit switch side &&\n+\t\ttest_commit L8 &&\n+\t\tgit checkout origin/main &&\n+\t\tH4=$(git rev-parse --short HEAD) &&\n+\t\ttest_commit L9 &&\n+\n+\t\tcat >expect <<-EOF &&\n+\t\tcommit: L9 (detached at $H4)\n+\t\tcheckout: moving from side to origin/main (detached at $H4)\n+\t\tcommit: L8 (side)\n+\t\tcheckout: moving from $(git rev-parse L7) to side (side)\n+\t\tcommit: L7 (detached at $H3)\n+\t\trebase: checkout refs/heads/main (detached at $H3)\n+\t\tcommit: L6 (detached at $H2)\n+\t\trebase: checkout origin/main (detached at $H2)\n+\t\tcommit: L5 (side)\n+\t\trebase (finish): returning to refs/heads/side (side)\n+\t\trebase (pick): L1 (side)\n+\t\trebase (start): checkout FETCH_HEAD (side)\n+\t\tcommit: checkout main (detached at $H1)\n+\t\tsync switch (pick): L1 (detached at $H1)\n+\t\tsync switch (start): checkout FETCH_HEAD (detached at $H1)\n+\t\tcommit: L3 (main)\n+\t\tpull -r file://$TRASH_DIRECTORY/origin (finish): returning to refs/heads/main (main)\n+\t\tpull -r file://$TRASH_DIRECTORY/origin (pick): L2 (main)\n+\t\tpull -r file://$TRASH_DIRECTORY/origin (pick): L1 (main)\n+\t\tpull -r file://$TRASH_DIRECTORY/origin (start): checkout $(git rev-parse FETCH_HEAD) (main)\n+\t\tcommit: L2 (main)\n+\t\tcommit: L1 (main)\n+\t\tclone: from $(cd ../origin && pwd) (main)\n+\t\tEOF\n+\n+\t\tgit log -g --format=\"%gs (%gb)\" >actual &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n test_expect_success 'rev-list -g complains when there are no reflogs' '\n \ttest_must_fail git rev-list -g\n '\n-- \ngitgitgadget\n\n"}]}