{"thread":{"id":"65726","subject":"[PATCH 0/2] builtin/history: introduce \"drop\" subcommand","startedAt":"2026-06-01T15:36:22Z","lastAt":"2026-07-01T10:21:34Z","messageCount":101,"participants":["Patrick Steinhardt","Junio C Hamano","Pablo Sabater","Kristoffer Haugsbakk","Phillip Wood","Christian Couder"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"544400","messageId":"20260601-b4-pks-history-drop-v1-0-643e32340d55@pks.im","threadId":"65726","inReplyTo":null,"subject":"[PATCH 0/2] builtin/history: introduce \"drop\" subcommand","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-01T15:36:12Z","receivedAt":"2026-06-01T15:36:22Z","isPatch":true,"body":"Hi,\n\nthis small patch series introduces the new \"drop\" subcommand for\ngit-history(1). As a reader might guess, the command does exactly that:\ngiven a commit, it will drop that commit from the commit history and\nreplay descendant branches on top of it.\n\nThanks!\n\nPatrick\n\n---\nPatrick Steinhardt (2):\n      builtin/history: split handling of ref updates into two phases\n      builtin/history: implement \"drop\" subcommand\n\n Documentation/git-history.adoc |  38 ++-\n builtin/history.c              | 333 +++++++++++++++++++++++---\n t/meson.build                  |   1 +\n t/t3454-history-drop.sh        | 513 +++++++++++++++++++++++++++++++++++++++++\n 4 files changed, 846 insertions(+), 39 deletions(-)\n\n\n---\nbase-commit: 1666c1265231b0bc5f613fbbf3f0a9896cdef76e\nchange-id: 20260601-b4-pks-history-drop-28f6c6399e7b\n\n"},{"id":"544401","messageId":"20260601-b4-pks-history-drop-v1-1-643e32340d55@pks.im","threadId":"65726","inReplyTo":"20260601-b4-pks-history-drop-v1-0-643e32340d55@pks.im","subject":"[PATCH 1/2] builtin/history: split handling of ref updates into two phases","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-01T15:36:13Z","receivedAt":"2026-06-01T15:36:23Z","isPatch":true,"body":"The function `handle_reference_updates()` is used by git-history(1) to\nupdate all references that refer to commits that have been rewritten. As\nsuch, it performs two steps:\n\n  - It gathers the references that need to be updated in the first\n    place.\n\n  - It prepares and commits the reference transaction.\n\nIn a subsequent commit we'll want to handle those two steps separately.\nPrepare for this by splitting up the function into two.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n builtin/history.c | 102 ++++++++++++++++++++++++++++++++++--------------------\n 1 file changed, 64 insertions(+), 38 deletions(-)\n\ndiff --git a/builtin/history.c b/builtin/history.c\nindex 0fc06fb204..4fadf38c32 100644\n--- a/builtin/history.c\n+++ b/builtin/history.c\n@@ -333,21 +333,17 @@ static int handle_ref_update(struct ref_transaction *transaction,\n \t\t\t\t      NULL, NULL, 0, reflog_msg, err);\n }\n \n-static int handle_reference_updates(struct rev_info *revs,\n-\t\t\t\t    enum ref_action action,\n-\t\t\t\t    struct commit *original,\n-\t\t\t\t    struct commit *rewritten,\n-\t\t\t\t    const char *reflog_msg,\n-\t\t\t\t    int dry_run,\n-\t\t\t\t    enum replay_empty_commit_action empty)\n+static int compute_pending_ref_updates(struct rev_info *revs,\n+\t\t\t\t       enum ref_action action,\n+\t\t\t\t       struct commit *original,\n+\t\t\t\t       struct commit *rewritten,\n+\t\t\t\t       enum replay_empty_commit_action empty,\n+\t\t\t\t       struct replay_result *result)\n {\n \tconst struct name_decoration *decoration;\n \tstruct replay_revisions_options opts = {\n \t\t.empty = empty,\n \t};\n-\tstruct replay_result result = { 0 };\n-\tstruct ref_transaction *transaction = NULL;\n-\tstruct strbuf err = STRBUF_INIT;\n \tchar hex[GIT_MAX_HEXSZ + 1];\n \tbool detached_head;\n \tint head_flags = 0;\n@@ -359,34 +355,13 @@ static int handle_reference_updates(struct rev_info *revs,\n \n \topts.onto = oid_to_hex_r(hex, &rewritten->object.oid);\n \n-\tret = replay_revisions(revs, &opts, &result);\n+\tret = replay_revisions(revs, &opts, result);\n \tif (ret)\n-\t\tgoto out;\n+\t\treturn ret;\n \n \tif (action != REF_ACTION_BRANCHES && action != REF_ACTION_HEAD)\n \t\tBUG(\"unsupported ref action %d\", action);\n \n-\tif (!dry_run) {\n-\t\ttransaction = ref_store_transaction_begin(get_main_ref_store(revs->repo), 0, &err);\n-\t\tif (!transaction) {\n-\t\t\tret = error(_(\"failed to begin ref transaction: %s\"), err.buf);\n-\t\t\tgoto out;\n-\t\t}\n-\t}\n-\n-\tfor (size_t i = 0; i < result.updates_nr; i++) {\n-\t\tret = handle_ref_update(transaction,\n-\t\t\t\t\tresult.updates[i].refname,\n-\t\t\t\t\t&result.updates[i].new_oid,\n-\t\t\t\t\t&result.updates[i].old_oid,\n-\t\t\t\t\treflog_msg, &err);\n-\t\tif (ret) {\n-\t\t\tret = error(_(\"failed to update ref '%s': %s\"),\n-\t\t\t\t    result.updates[i].refname, err.buf);\n-\t\t\tgoto out;\n-\t\t}\n-\t}\n-\n \t/*\n \t * `replay_revisions()` only updates references that are\n \t * ancestors of `rewritten`, so we need to manually\n@@ -414,14 +389,43 @@ static int handle_reference_updates(struct rev_info *revs,\n \t\t    !detached_head)\n \t\t\tcontinue;\n \n+\t\tALLOC_GROW(result->updates, result->updates_nr + 1, result->updates_alloc);\n+\t\tresult->updates[result->updates_nr].refname = xstrdup(decoration->name);\n+\t\tresult->updates[result->updates_nr].old_oid = original->object.oid;\n+\t\tresult->updates[result->updates_nr].new_oid = rewritten->object.oid;\n+\t\tresult->updates_nr++;\n+\t}\n+\n+\treturn 0;\n+}\n+\n+static int apply_pending_ref_updates(struct repository *repo,\n+\t\t\t\t     const struct replay_result *result,\n+\t\t\t\t     const char *reflog_msg,\n+\t\t\t\t     int dry_run)\n+{\n+\tstruct ref_transaction *transaction = NULL;\n+\tstruct strbuf err = STRBUF_INIT;\n+\tint ret;\n+\n+\tif (!dry_run) {\n+\t\ttransaction = ref_store_transaction_begin(get_main_ref_store(repo),\n+\t\t\t\t\t\t\t  0, &err);\n+\t\tif (!transaction) {\n+\t\t\tret = error(_(\"failed to begin ref transaction: %s\"), err.buf);\n+\t\t\tgoto out;\n+\t\t}\n+\t}\n+\n+\tfor (size_t i = 0; i < result->updates_nr; i++) {\n \t\tret = handle_ref_update(transaction,\n-\t\t\t\t\tdecoration->name,\n-\t\t\t\t\t&rewritten->object.oid,\n-\t\t\t\t\t&original->object.oid,\n+\t\t\t\t\tresult->updates[i].refname,\n+\t\t\t\t\t&result->updates[i].new_oid,\n+\t\t\t\t\t&result->updates[i].old_oid,\n \t\t\t\t\treflog_msg, &err);\n \t\tif (ret) {\n \t\t\tret = error(_(\"failed to update ref '%s': %s\"),\n-\t\t\t\t    decoration->name, err.buf);\n+\t\t\t\t    result->updates[i].refname, err.buf);\n \t\t\tgoto out;\n \t\t}\n \t}\n@@ -435,11 +439,33 @@ static int handle_reference_updates(struct rev_info *revs,\n \n out:\n \tref_transaction_free(transaction);\n-\treplay_result_release(&result);\n \tstrbuf_release(&err);\n \treturn ret;\n }\n \n+static int handle_reference_updates(struct rev_info *revs,\n+\t\t\t\t    enum ref_action action,\n+\t\t\t\t    struct commit *original,\n+\t\t\t\t    struct commit *rewritten,\n+\t\t\t\t    const char *reflog_msg,\n+\t\t\t\t    int dry_run,\n+\t\t\t\t    enum replay_empty_commit_action empty)\n+{\n+\tstruct replay_result result = { 0 };\n+\tint ret;\n+\n+\tret = compute_pending_ref_updates(revs, action, original, rewritten,\n+\t\t\t\t\t  empty, &result);\n+\tif (ret)\n+\t\tgoto out;\n+\n+\tret = apply_pending_ref_updates(revs->repo, &result, reflog_msg, dry_run);\n+\n+out:\n+\treplay_result_release(&result);\n+\treturn ret;\n+}\n+\n static int commit_became_empty(struct repository *repo,\n \t\t\t       struct commit *original,\n \t\t\t       struct tree *result)\n\n-- \n2.54.0.926.g75ba10bac6.dirty\n\n"},{"id":"544402","messageId":"20260601-b4-pks-history-drop-v1-2-643e32340d55@pks.im","threadId":"65726","inReplyTo":"20260601-b4-pks-history-drop-v1-0-643e32340d55@pks.im","subject":"[PATCH 2/2] builtin/history: implement \"drop\" subcommand","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-01T15:36:14Z","receivedAt":"2026-06-01T15:36:32Z","isPatch":true,"body":"A common operation when editing the commit history is to drop a specific\ncommit from the history entirely, but this operation is not currently\ncovered by git-history(1).\n\nA couple of noteworthy bits:\n\n  - This is the first git-history(1) command that will ultimately result\n    in changes to both the index and the working tree. We thus have to\n    add logic to merge resulting changes into those.\n\n  - It is still not possible to replay merge commits, so this limitation\n    is inherited for the new \"drop\" command.\n\n  - For now we refuse to drop root commits. While we _can_ indeed drop\n    root commits in the general case, there are edge cases where the\n    resulting history would become completely empty. This is thus left\n    to a subsequent patch series.\n\nOther than that, most of the logic is rather straight-forward as we can\ncontinue to build on the preexisting logic in git-history(1) for most of\nthe part.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n Documentation/git-history.adoc |  38 ++-\n builtin/history.c              | 231 +++++++++++++++++++\n t/meson.build                  |   1 +\n t/t3454-history-drop.sh        | 513 +++++++++++++++++++++++++++++++++++++++++\n 4 files changed, 782 insertions(+), 1 deletion(-)\n\ndiff --git a/Documentation/git-history.adoc b/Documentation/git-history.adoc\nindex 2ba8121795..4eac732fd2 100644\n--- a/Documentation/git-history.adoc\n+++ b/Documentation/git-history.adoc\n@@ -8,6 +8,7 @@ git-history - EXPERIMENTAL: Rewrite history\n SYNOPSIS\n --------\n [synopsis]\n+git history drop <commit> [--dry-run] [--update-refs=(branches|head)] [--empty=(drop|keep|abort)]\n git history fixup <commit> [--dry-run] [--update-refs=(branches|head)] [--reedit-message] [--empty=(drop|keep|abort)]\n git history reword <commit> [--dry-run] [--update-refs=(branches|head)]\n git history split <commit> [--dry-run] [--update-refs=(branches|head)] [--] [<pathspec>...]\n@@ -51,13 +52,28 @@ be stateful operations. The limitation can be lifted once (if) Git learns about\n first-class conflicts.\n \n When using `fixup` with `--empty=drop`, dropping the root commit is not yet\n-supported.\n+supported. Likewise, `drop` cannot remove the root commit or a merge commit.\n \n COMMANDS\n --------\n \n The following commands are available to rewrite history in different ways:\n \n+`drop <commit>`::\n+\tRemove the specified commit from the history. All descendants of the\n+\tcommit are replayed directly onto its parent.\n++\n+The root commit cannot be dropped as that may lead to edge cases where refs\n+end up with no commits anymore. Merge commits cannot be dropped either; see\n+LIMITATIONS.\n++\n+If `HEAD` points at a commit that is to be rewritten, the index and working\n+tree are updated to match the new `HEAD`. The command aborts before any\n+references are updated in case local modifications would be overwritten.\n++\n+If replaying any descendant would result in a conflict, the command aborts\n+with an error.\n+\n `fixup <commit>`::\n \tApply the currently staged changes to the specified commit. This is\n \tsimilar in nature to `git commit --fixup=<commit>` followed by `git\n@@ -170,6 +186,26 @@ The staged addition of `unrelated.txt` has been incorporated into the `first`\n commit. All descendant commits have been replayed on top of the rewritten\n history.\n \n+Drop a commit\n+~~~~~~~~~~~~~\n+\n+----------\n+$ git log --oneline\n+abc1234 (HEAD -> main) third\n+def5678 second\n+ghi9012 first\n+\n+$ git history drop def5678\n+\n+$ git log --oneline\n+jkl3456 (HEAD -> main) third\n+ghi9012 first\n+----------\n+\n+The `second` commit has been removed from the history, and `third` has been\n+replayed directly on top of `first`. All branches that pointed at the dropped\n+commit have been moved to its parent.\n+\n Split a commit\n ~~~~~~~~~~~~~~\n \ndiff --git a/builtin/history.c b/builtin/history.c\nindex 4fadf38c32..12c27defbb 100644\n--- a/builtin/history.c\n+++ b/builtin/history.c\n@@ -21,9 +21,12 @@\n #include \"sequencer.h\"\n #include \"strvec.h\"\n #include \"tree.h\"\n+#include \"tree-walk.h\"\n #include \"unpack-trees.h\"\n #include \"wt-status.h\"\n \n+#define GIT_HISTORY_DROP_USAGE \\\n+\tN_(\"git history drop <commit> [--dry-run] [--update-refs=(branches|head)] [--empty=(drop|keep|abort)]\")\n #define GIT_HISTORY_FIXUP_USAGE \\\n \tN_(\"git history fixup <commit> [--dry-run] [--update-refs=(branches|head)] [--reedit-message] [--empty=(drop|keep|abort)]\")\n #define GIT_HISTORY_REWORD_USAGE \\\n@@ -1001,12 +1004,239 @@ static int cmd_history_split(int argc,\n \treturn ret;\n }\n \n+static int update_worktree(struct repository *repo,\n+\t\t\t   const struct commit *old_head,\n+\t\t\t   const struct commit *new_head,\n+\t\t\t   bool dry_run)\n+{\n+\tstruct index_state index = INDEX_STATE_INIT(repo);\n+\tstruct unpack_trees_options opts = { 0 };\n+\tstruct lock_file lock = LOCK_INIT;\n+\tstruct tree_desc desc[2] = { 0 };\n+\tchar *desc_buf[2] = { 0 };\n+\tint ret;\n+\n+\tif (!dry_run &&\n+\t    repo_hold_locked_index(repo, &lock, LOCK_REPORT_ON_ERROR) < 0)\n+\t\treturn -1;\n+\n+\tif (read_index_from(&index, repo->index_file, repo->gitdir) < 0) {\n+\t\tret = error(_(\"unable to read index\"));\n+\t\tgoto out;\n+\t}\n+\n+\tsetup_unpack_trees_porcelain(&opts, \"history drop\");\n+\topts.head_idx = 1;\n+\topts.src_index = &index;\n+\topts.dst_index = &index;\n+\topts.fn = twoway_merge;\n+\topts.merge = 1;\n+\topts.update = !dry_run;\n+\topts.dry_run = dry_run;\n+\topts.preserve_ignored = 0;\n+\tinit_checkout_metadata(&opts.meta, NULL, &new_head->object.oid, NULL);\n+\n+\tdesc_buf[0] = fill_tree_descriptor(repo, &desc[0], &old_head->object.oid);\n+\tdesc_buf[1] = fill_tree_descriptor(repo, &desc[1], &new_head->object.oid);\n+\n+\tif (unpack_trees(2, desc, &opts)) {\n+\t\tret = -1;\n+\t\tgoto out;\n+\t}\n+\n+\tif (!dry_run) {\n+\t\tcache_tree_free(&index.cache_tree);\n+\n+\t\tif (write_locked_index(&index, &lock, COMMIT_LOCK)) {\n+\t\t\tret = error(_(\"could not write index\"));\n+\t\t\tgoto out;\n+\t\t}\n+\t}\n+\n+\tret = 0;\n+\n+out:\n+\tclear_unpack_trees_porcelain(&opts);\n+\trollback_lock_file(&lock);\n+\trelease_index(&index);\n+\tfree(desc_buf[0]);\n+\tfree(desc_buf[1]);\n+\treturn ret;\n+}\n+\n+static int find_head_tree_change(struct repository *repo,\n+\t\t\t\t const struct replay_result *result,\n+\t\t\t\t struct commit **old_head,\n+\t\t\t\t struct commit **new_head,\n+\t\t\t\t bool *changed)\n+{\n+\tconst struct replay_ref_update *head_update = NULL;\n+\tstruct commit *old_head_commit, *new_head_commit;\n+\tstruct tree *old_head_tree, *new_head_tree;\n+\tconst char *head_target;\n+\tint head_flags;\n+\n+\t*changed = false;\n+\n+\thead_target = refs_resolve_ref_unsafe(get_main_ref_store(repo),\n+\t\t\t\t\t      \"HEAD\", RESOLVE_REF_NO_RECURSE,\n+\t\t\t\t\t      NULL, &head_flags);\n+\tif (!head_target)\n+\t\treturn error(_(\"cannot look up HEAD\"));\n+\tif (!(head_flags & REF_ISSYMREF))\n+\t\thead_target = \"HEAD\";\n+\n+\tfor (size_t i = 0; i < result->updates_nr; i++) {\n+\t\tif (!strcmp(result->updates[i].refname, head_target)) {\n+\t\t\thead_update = &result->updates[i];\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\n+\tif (!head_update)\n+\t\treturn 0;\n+\n+\told_head_commit = lookup_commit_reference(repo, &head_update->old_oid);\n+\tnew_head_commit = lookup_commit_reference(repo, &head_update->new_oid);\n+\tif (!old_head_commit || !new_head_commit)\n+\t\treturn error(_(\"cannot resolve HEAD commit\"));\n+\n+\told_head_tree = repo_get_commit_tree(repo, old_head_commit);\n+\tnew_head_tree = repo_get_commit_tree(repo, new_head_commit);\n+\tif (!old_head_tree || !new_head_tree)\n+\t\treturn error(_(\"cannot resolve tree for HEAD\"));\n+\n+\tif (oideq(&old_head_tree->object.oid, &new_head_tree->object.oid))\n+\t\treturn 0;\n+\n+\t*old_head = old_head_commit;\n+\t*new_head = new_head_commit;\n+\t*changed = true;\n+\n+\treturn 0;\n+}\n+\n+static int cmd_history_drop(int argc,\n+\t\t\t    const char **argv,\n+\t\t\t    const char *prefix,\n+\t\t\t    struct repository *repo)\n+{\n+\tconst char * const usage[] = {\n+\t\tGIT_HISTORY_DROP_USAGE,\n+\t\tNULL,\n+\t};\n+\tenum replay_empty_commit_action empty = REPLAY_EMPTY_COMMIT_DROP;\n+\tenum ref_action action = REF_ACTION_DEFAULT;\n+\tint dry_run = 0;\n+\tstruct option options[] = {\n+\t\tOPT_CALLBACK_F(0, \"update-refs\", &action, \"(branches|head)\",\n+\t\t\t       N_(\"control which refs should be updated\"),\n+\t\t\t       PARSE_OPT_NONEG, parse_ref_action),\n+\t\tOPT_BOOL('n', \"dry-run\", &dry_run,\n+\t\t\t N_(\"perform a dry-run without updating any refs\")),\n+\t\tOPT_CALLBACK_F(0, \"empty\", &empty, \"(drop|keep|abort)\",\n+\t\t\t       N_(\"how to handle descendants that become empty\"),\n+\t\t\t       PARSE_OPT_NONEG, parse_opt_empty),\n+\t\tOPT_END(),\n+\t};\n+\tstruct strbuf reflog_msg = STRBUF_INIT;\n+\tstruct commit *original, *rewritten;\n+\tstruct rev_info revs = { 0 };\n+\tstruct replay_result result = { 0 };\n+\tstruct commit *old_head, *new_head;\n+\tbool head_moves = false;\n+\tint ret;\n+\n+\targc = parse_options(argc, argv, prefix, options, usage, 0);\n+\tif (argc != 1) {\n+\t\tret = error(_(\"command expects a single revision\"));\n+\t\tgoto out;\n+\t}\n+\trepo_config(repo, git_default_config, NULL);\n+\n+\tif (action == REF_ACTION_DEFAULT)\n+\t\taction = REF_ACTION_BRANCHES;\n+\n+\toriginal = lookup_commit_reference_by_name(argv[0]);\n+\tif (!original) {\n+\t\tret = error(_(\"commit cannot be found: %s\"), argv[0]);\n+\t\tgoto out;\n+\t}\n+\n+\tif (!original->parents) {\n+\t\tret = error(_(\"cannot drop root commit %s: \"\n+\t\t\t      \"it has no parent to replay onto\"),\n+\t\t\t    argv[0]);\n+\t\tgoto out;\n+\t} else if (original->parents->next) {\n+\t\tret = error(_(\"cannot drop merge commit\"));\n+\t\tgoto out;\n+\t}\n+\n+\tret = setup_revwalk(repo, action, original, &revs);\n+\tif (ret)\n+\t\tgoto out;\n+\n+\trewritten = original->parents->item;\n+\n+\tret = compute_pending_ref_updates(&revs, action, original, rewritten,\n+\t\t\t\t\t  empty, &result);\n+\tif (ret) {\n+\t\tret = error(_(\"failed replaying descendants\"));\n+\t\tgoto out;\n+\t}\n+\n+\t/*\n+\t * If HEAD will move as a result of the rewrite then we'll have to\n+\t * merge in the changes into the worktree and index. This merge can of\n+\t * course conflict, which will cause the whole operation to abort.\n+\t *\n+\t * If we had already updated the refs at that point then we'd have an\n+\t * inconsistent repository state. So we first perform a dry-run merge\n+\t * here before updating refs.\n+\t */\n+\tif (!dry_run && !is_bare_repository()) {\n+\t\tret = find_head_tree_change(repo, &result, &old_head,\n+\t\t\t\t\t    &new_head, &head_moves);\n+\t\tif (ret < 0)\n+\t\t\tgoto out;\n+\n+\t\tif (head_moves && update_worktree(repo, old_head, new_head, true) < 0) {\n+\t\t\tret = error(_(\"dropping this commit would \"\n+\t\t\t\t      \"overwrite local changes; aborting\"));\n+\t\t\tgoto out;\n+\t\t}\n+\t}\n+\n+\tstrbuf_addf(&reflog_msg, \"drop: dropping %s\", argv[0]);\n+\tret = apply_pending_ref_updates(repo, &result, reflog_msg.buf, dry_run);\n+\tif (ret < 0) {\n+\t\tret = error(_(\"failed to update references\"));\n+\t\tgoto out;\n+\t}\n+\n+\tif (head_moves && update_worktree(repo, old_head, new_head, false) < 0) {\n+\t\tret = error(_(\"failed to update working tree; \"\n+\t\t\t      \"run `git checkout HEAD` to sync\"));\n+\t\tgoto out;\n+\t}\n+\n+\tret = 0;\n+\n+out:\n+\treplay_result_release(&result);\n+\tstrbuf_release(&reflog_msg);\n+\trelease_revisions(&revs);\n+\treturn ret;\n+}\n+\n int cmd_history(int argc,\n \t\tconst char **argv,\n \t\tconst char *prefix,\n \t\tstruct repository *repo)\n {\n \tconst char * const usage[] = {\n+\t\tGIT_HISTORY_DROP_USAGE,\n \t\tGIT_HISTORY_FIXUP_USAGE,\n \t\tGIT_HISTORY_REWORD_USAGE,\n \t\tGIT_HISTORY_SPLIT_USAGE,\n@@ -1014,6 +1244,7 @@ int cmd_history(int argc,\n \t};\n \tparse_opt_subcommand_fn *fn = NULL;\n \tstruct option options[] = {\n+\t\tOPT_SUBCOMMAND(\"drop\", &fn, cmd_history_drop),\n \t\tOPT_SUBCOMMAND(\"fixup\", &fn, cmd_history_fixup),\n \t\tOPT_SUBCOMMAND(\"reword\", &fn, cmd_history_reword),\n \t\tOPT_SUBCOMMAND(\"split\", &fn, cmd_history_split),\ndiff --git a/t/meson.build b/t/meson.build\nindex 2af8d01279..d5e71056b2 100644\n--- a/t/meson.build\n+++ b/t/meson.build\n@@ -399,6 +399,7 @@ integration_tests = [\n   't3451-history-reword.sh',\n   't3452-history-split.sh',\n   't3453-history-fixup.sh',\n+  't3454-history-drop.sh',\n   't3500-cherry.sh',\n   't3501-revert-cherry-pick.sh',\n   't3502-cherry-pick-merge.sh',\ndiff --git a/t/t3454-history-drop.sh b/t/t3454-history-drop.sh\nnew file mode 100755\nindex 0000000000..b320ff09b3\n--- /dev/null\n+++ b/t/t3454-history-drop.sh\n@@ -0,0 +1,513 @@\n+#!/bin/sh\n+\n+test_description='tests for git-history drop subcommand'\n+\n+. ./test-lib.sh\n+. \"$TEST_DIRECTORY/lib-log-graph.sh\"\n+\n+expect_graph () {\n+\tcat >expect &&\n+\tlib_test_cmp_graph --graph --format=%s \"$@\"\n+}\n+\n+expect_log () {\n+\tgit log --format=\"%s\" \"$@\" >actual &&\n+\tcat >expect &&\n+\ttest_cmp expect actual\n+}\n+\n+test_expect_success 'errors on missing commit argument' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit initial &&\n+\t\ttest_must_fail git history drop 2>err &&\n+\t\ttest_grep \"command expects a single revision\" err\n+\t)\n+'\n+\n+test_expect_success 'errors on too many arguments' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit initial &&\n+\t\ttest_must_fail git history drop HEAD HEAD 2>err &&\n+\t\ttest_grep \"command expects a single revision\" err\n+\t)\n+'\n+\n+test_expect_success 'errors on unknown revision' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit initial &&\n+\t\ttest_must_fail git history drop does-not-exist 2>err &&\n+\t\ttest_grep \"commit cannot be found: does-not-exist\" err\n+\t)\n+'\n+\n+test_expect_success 'errors with invalid --empty= value' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\ttest_commit -C repo initial &&\n+\ttest_commit -C repo second &&\n+\ttest_must_fail git -C repo history drop --empty=bogus HEAD 2>err &&\n+\ttest_grep \"unrecognized.*--empty.*bogus\" err\n+'\n+\n+test_expect_success 'drops a commit in the middle and replays descendants' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\n+\t\tgit symbolic-ref HEAD >expect &&\n+\t\tgit history drop HEAD~ &&\n+\t\tgit symbolic-ref HEAD >actual &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\texpect_log <<-\\EOF &&\n+\t\tthird\n+\t\tfirst\n+\t\tEOF\n+\n+\t\ttest_must_fail git show HEAD:second.t &&\n+\t\ttest_path_is_missing second.t &&\n+\n+\t\tgit reflog >reflog &&\n+\t\ttest_grep \"drop: dropping HEAD~\" reflog\n+\t)\n+'\n+\n+test_expect_success 'drops the HEAD commit' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\n+\t\tgit history drop HEAD &&\n+\n+\t\texpect_log <<-\\EOF\n+\t\tfirst\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'drops a commit on detached HEAD' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\t\tgit checkout --detach HEAD &&\n+\n+\t\tgit history drop HEAD~ &&\n+\n+\t\texpect_log <<-\\EOF\n+\t\tthird\n+\t\tfirst\n+\t\tEOF\n+\t)\n+'\n+\n+# Note: in this case it would actually be fine to drop the root commit, as we\n+# do have a descendant commit, and no reference points to the root commit\n+# directly. So this is something that we may relax eventually.\n+test_expect_success 'refuses to drop the root commit' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\n+\t\ttest_must_fail git history drop HEAD~ 2>err &&\n+\t\ttest_grep \"cannot drop root commit\" err\n+\t)\n+'\n+\n+# In contrast to the above case, we actually don't want to drop the root commit\n+# here as that would cause us to end up with an empty commit graph.\n+test_expect_success 'refuses to drop the root commit when branch becomes empty' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\n+\t\ttest_must_fail git history drop HEAD 2>err &&\n+\t\ttest_grep \"cannot drop root commit\" err\n+\t)\n+'\n+\n+test_expect_success 'refuses to drop a merge commit' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\tgit branch branch &&\n+\t\ttest_commit ours &&\n+\t\tgit switch branch &&\n+\t\ttest_commit theirs &&\n+\t\tgit switch - &&\n+\t\tgit merge theirs &&\n+\n+\t\ttest_must_fail git history drop HEAD 2>err &&\n+\t\ttest_grep \"cannot drop merge commit\" err\n+\t)\n+'\n+\n+test_expect_success 'refuses when descendants contain a merge commit' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\ttest_commit middle &&\n+\t\tgit branch branch &&\n+\t\ttest_commit ours &&\n+\t\tgit switch branch &&\n+\t\ttest_commit theirs &&\n+\t\tgit switch - &&\n+\t\tgit merge theirs &&\n+\n+\t\ttest_must_fail git history drop middle 2>err &&\n+\t\ttest_grep \"replaying merge commits is not supported yet\" err\n+\t)\n+'\n+\n+test_expect_success 'works in a bare repository' '\n+\ttest_when_finished \"rm -rf repo repo.git\" &&\n+\n+\tgit init repo &&\n+\ttest_commit -C repo first &&\n+\ttest_commit -C repo second &&\n+\ttest_commit -C repo third &&\n+\n+\tgit clone --bare repo repo.git &&\n+\t(\n+\t\tcd repo.git &&\n+\n+\t\tgit history drop HEAD~ &&\n+\t\texpect_log <<-\\EOF\n+\t\tthird\n+\t\tfirst\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'updates branches on other lines of descent' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\ttest_commit target &&\n+\t\tgit branch theirs &&\n+\t\ttest_commit ours &&\n+\t\tgit switch theirs &&\n+\t\ttest_commit theirs &&\n+\n+\t\texpect_graph --branches <<-\\EOF &&\n+\t\t* theirs\n+\t\t| * ours\n+\t\t|/\n+\t\t* target\n+\t\t* base\n+\t\tEOF\n+\n+\t\tgit history drop target &&\n+\n+\t\texpect_graph --branches <<-\\EOF\n+\t\t* ours\n+\t\t| * theirs\n+\t\t|/\n+\t\t* base\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'moves branch pointing at dropped commit to its parent' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo --initial-branch=main &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\tgit branch points-at-second &&\n+\t\ttest_commit third &&\n+\n+\t\tgit rev-parse first >expect &&\n+\t\tgit history drop second &&\n+\t\tgit rev-parse points-at-second >actual &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\texpect_log --format=\"%s %D\" --branches <<-\\EOF\n+\t\tthird HEAD -> main\n+\t\tfirst tag: first, points-at-second\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--dry-run prints ref updates without modifying repo' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo --initial-branch=main &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\tgit branch branch &&\n+\t\ttest_commit middle &&\n+\t\ttest_commit ours &&\n+\t\tgit switch branch &&\n+\t\ttest_commit theirs &&\n+\n+\t\tgit refs list >refs-expect &&\n+\t\tgit history drop --dry-run main~ >updates &&\n+\t\tgit refs list >refs-actual &&\n+\t\ttest_cmp refs-expect refs-actual &&\n+\t\ttest_grep \"update refs/heads/main\" updates &&\n+\n+\t\tgit update-ref --stdin <updates &&\n+\t\texpect_log main <<-\\EOF\n+\t\tours\n+\t\tbase\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--update-refs=head updates only HEAD' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo --initial-branch=main &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\ttest_commit target &&\n+\t\tgit branch theirs &&\n+\t\ttest_commit ours &&\n+\t\tgit switch theirs &&\n+\t\ttest_commit theirs &&\n+\n+\t\t# When told to update HEAD only, the command refuses to\n+\t\t# rewrite commits that are not an ancestor of HEAD.\n+\t\ttest_must_fail git history drop --update-refs=head main 2>err &&\n+\t\ttest_grep \"rewritten commit must be an ancestor of HEAD\" err &&\n+\n+\t\texpect_graph --branches <<-\\EOF &&\n+\t\t* theirs\n+\t\t| * ours\n+\t\t|/\n+\t\t* target\n+\t\t* base\n+\t\tEOF\n+\n+\t\tgit switch main &&\n+\t\tgit history drop --update-refs=head target &&\n+\n+\t\texpect_graph --branches <<-\\EOF\n+\t\t* ours\n+\t\t| * theirs\n+\t\t| * target\n+\t\t|/\n+\t\t* base\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'conflict with replayed commit aborts cleanly' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\ttest_commit conflict-a file &&\n+\t\ttest_commit conflict-b file &&\n+\n+\t\tgit refs list >refs-expect &&\n+\t\ttest_must_fail git history drop HEAD~ 2>err &&\n+\t\ttest_grep \"failed replaying descendants\" err &&\n+\t\tgit refs list >refs-actual &&\n+\t\ttest_cmp refs-expect refs-actual\n+\t)\n+'\n+\n+# Build a history where a descendant of the drop target reverts the change\n+# introduced by the drop target. After dropping, the descendant's diff applies\n+# against a tree that already lacks the change, so it becomes empty.\n+setup_empty_descendant_repo () {\n+\tgit init \"$1\" &&\n+\t(\n+\t\tcd \"$1\" &&\n+\t\techo C1 >file &&\n+\t\tgit add file &&\n+\t\tgit commit -m \"base\" &&\n+\t\tgit tag base &&\n+\t\techo C2 >file &&\n+\t\tgit add file &&\n+\t\tgit commit -m \"drop-me\" &&\n+\t\tgit tag drop-me &&\n+\t\ttest_commit middle &&\n+\t\techo C1 >file &&\n+\t\tgit add file &&\n+\t\tgit commit -m \"revert-drop-me\" &&\n+\t\tgit tag revert-drop-me\n+\t)\n+}\n+\n+test_expect_success '--empty=drop drops descendants that become empty' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tsetup_empty_descendant_repo repo &&\n+\t(\n+\t\tcd repo &&\n+\n+\t\tgit history drop --empty=drop drop-me &&\n+\n+\t\texpect_log <<-\\EOF\n+\t\tmiddle\n+\t\tbase\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--empty=keep keeps descendants that become empty' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tsetup_empty_descendant_repo repo &&\n+\t(\n+\t\tcd repo &&\n+\n+\t\tgit history drop --empty=keep drop-me &&\n+\n+\t\texpect_log <<-\\EOF &&\n+\t\trevert-drop-me\n+\t\tmiddle\n+\t\tbase\n+\t\tEOF\n+\t\tgit diff HEAD~ HEAD >diff &&\n+\t\ttest_must_be_empty diff\n+\t)\n+'\n+\n+test_expect_success '--empty=abort errors out when a descendant becomes empty' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tsetup_empty_descendant_repo repo &&\n+\t(\n+\t\tcd repo &&\n+\n+\t\ttest_must_fail git history drop --empty=abort drop-me 2>err &&\n+\t\ttest_grep \"became empty after replay\" err\n+\t)\n+'\n+\n+test_expect_success 'updates index and worktree when HEAD moves' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\n+\t\tgit history drop second &&\n+\n+\t\t# Worktree should no longer contain second.t.\n+\t\ttest_path_is_missing second.t &&\n+\t\ttest_path_is_file first.t &&\n+\t\ttest_path_is_file third.t &&\n+\n+\t\t# Index and worktree should both match the new HEAD.\n+\t\tgit status --porcelain --untracked-files=no >status &&\n+\t\ttest_must_be_empty status\n+\t)\n+'\n+\n+test_expect_success 'updates worktree when dropping HEAD itself' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\n+\t\tgit history drop HEAD &&\n+\n+\t\ttest_path_is_missing second.t &&\n+\t\ttest_path_is_file first.t &&\n+\n+\t\tgit status --porcelain --untracked-files=no >status &&\n+\t\ttest_must_be_empty status\n+\t)\n+'\n+\n+test_expect_success 'preserves unrelated unstaged modifications' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\techo first-content >unrelated.txt &&\n+\t\tgit add unrelated.txt &&\n+\t\tgit commit -m \"add unrelated\" &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\n+\t\techo locally-modified >unrelated.txt &&\n+\n+\t\tgit diff >diff-expect &&\n+\t\tgit history drop second &&\n+\t\tgit diff >diff-actual &&\n+\t\ttest_cmp diff-expect diff-actual &&\n+\t\ttest_path_is_missing second.t\n+\t)\n+'\n+\n+test_expect_success 'preserves unrelated staged changes' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\techo first-content >unrelated.txt &&\n+\t\tgit add unrelated.txt &&\n+\t\tgit commit -m \"add unrelated\" &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\n+\t\techo staged-change >unrelated.txt &&\n+\t\tgit add unrelated.txt &&\n+\n+\t\tgit diff --cached >diff-expect &&\n+\t\tgit history drop second &&\n+\t\tgit diff --cached >diff-actual &&\n+\t\ttest_cmp diff-expect diff-actual &&\n+\t\ttest_path_is_missing second.t\n+\t)\n+'\n+\n+test_expect_success 'aborts when local modifications would be overwritten' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\ttest_commit conflict &&\n+\n+\t\techo local-edit >conflict.t &&\n+\t\tgit diff >diff-expect &&\n+\t\ttest_must_fail git history drop HEAD 2>err &&\n+\t\ttest_grep \"would overwrite local changes\" err &&\n+\t\tgit diff >diff-actual &&\n+\t\ttest_cmp diff-expect diff-actual\n+\t)\n+'\n+\n+test_done\n\n-- \n2.54.0.926.g75ba10bac6.dirty\n\n"},{"id":"544445","messageId":"xmqqbjdt25e3.fsf@gitster.g","threadId":"65726","inReplyTo":"20260601-b4-pks-history-drop-v1-2-643e32340d55@pks.im","subject":"Re: [PATCH 2/2] builtin/history: implement \"drop\" subcommand","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-06-01T23:43:48Z","receivedAt":"2026-06-01T23:43:51Z","isPatch":true,"body":"Patrick Steinhardt <ps@pks.im> writes:\n\n> A common operation when editing the commit history is to drop a specific\n> commit from the history entirely, but this operation is not currently\n> covered by git-history(1).\n>\n> A couple of noteworthy bits:\n>\n>   - This is the first git-history(1) command that will ultimately result\n>     in changes to both the index and the working tree. We thus have to\n>     add logic to merge resulting changes into those.\n>\n>   - It is still not possible to replay merge commits, so this limitation\n>     is inherited for the new \"drop\" command.\n>\n>   - For now we refuse to drop root commits. While we _can_ indeed drop\n>     root commits in the general case, there are edge cases where the\n>     resulting history would become completely empty. This is thus left\n>     to a subsequent patch series.\n>\n> Other than that, most of the logic is rather straight-forward as we can\n> continue to build on the preexisting logic in git-history(1) for most of\n> the part.\n>\n> Signed-off-by: Patrick Steinhardt <ps@pks.im>\n> ---\n> ...\n> +static int update_worktree(struct repository *repo,\n> +\t\t\t   const struct commit *old_head,\n> +\t\t\t   const struct commit *new_head,\n> +\t\t\t   bool dry_run)\n> +{\n> +...\n> +\n> +out:\n> +\tclear_unpack_trees_porcelain(&opts);\n> +\trollback_lock_file(&lock);\n> +\trelease_index(&index);\n> +\tfree(desc_buf[0]);\n> +\tfree(desc_buf[1]);\n> +\treturn ret;\n> +}\n\nThe function looks very familiar---anybody who wants to perform\n\"checkout <other-commit>\" needs to do exactly the above.  It is a\nbit surprising and disappointing that this topic needs to *invent*\nits own helper function and carry it as a file-scope static.\n\n> +\tif (head_moves && update_worktree(repo, old_head, new_head, false) < 0) {\n> +\t\tret = error(_(\"failed to update working tree; \"\n> +\t\t\t      \"run `git checkout HEAD` to sync\"));\n> +\t\tgoto out;\n> +\t}\n\nThis is minor, but unlike in documentation pages written in AsciiDoc, we do\nnot do backticks for literals in our error messages, I think.\n"},{"id":"544461","messageId":"CAN5EUNQbSN7+SDWcrh3jTD7SXrnD=e-fQ9Qj9778R7cy2q4b1g@mail.gmail.com","threadId":"65726","inReplyTo":"20260601-b4-pks-history-drop-v1-2-643e32340d55@pks.im","subject":"Re: [PATCH 2/2] builtin/history: implement \"drop\" subcommand","fromName":"Pablo Sabater","fromEmail":"pabloosabaterr@gmail.com","sentAt":"2026-06-02T07:31:17Z","receivedAt":"2026-06-02T07:31:30Z","isPatch":true,"body":"Hi!\n\nEl mar, 2 jun 2026 a las 8:16, Patrick Steinhardt (<ps@pks.im>) escribió:\n\n> +\n> +static int cmd_history_drop(int argc,\n> +                           const char **argv,\n> +                           const char *prefix,\n> +                           struct repository *repo)\n> +{\n> +       const char * const usage[] = {\n> +               GIT_HISTORY_DROP_USAGE,\n> +               NULL,\n> +       };\n> +       enum replay_empty_commit_action empty = REPLAY_EMPTY_COMMIT_DROP;\n> +       enum ref_action action = REF_ACTION_DEFAULT;\n> +       int dry_run = 0;\n> +       struct option options[] = {\n> +               OPT_CALLBACK_F(0, \"update-refs\", &action, \"(branches|head)\",\n> +                              N_(\"control which refs should be updated\"),\n> +                              PARSE_OPT_NONEG, parse_ref_action),\n> +               OPT_BOOL('n', \"dry-run\", &dry_run,\n> +                        N_(\"perform a dry-run without updating any refs\")),\n> +               OPT_CALLBACK_F(0, \"empty\", &empty, \"(drop|keep|abort)\",\n> +                              N_(\"how to handle descendants that become empty\"),\n> +                              PARSE_OPT_NONEG, parse_opt_empty),\n> +               OPT_END(),\n> +       };\n> +       struct strbuf reflog_msg = STRBUF_INIT;\n> +       struct commit *original, *rewritten;\n> +       struct rev_info revs = { 0 };\n> +       struct replay_result result = { 0 };\n> +       struct commit *old_head, *new_head;\n> +       bool head_moves = false;\n> +       int ret;\n> +\n> +       argc = parse_options(argc, argv, prefix, options, usage, 0);\n> +       if (argc != 1) {\n> +               ret = error(_(\"command expects a single revision\"));\n> +               goto out;\n> +       }\n> +       repo_config(repo, git_default_config, NULL);\n> +\n> +       if (action == REF_ACTION_DEFAULT)\n> +               action = REF_ACTION_BRANCHES;\n> +\n> +       original = lookup_commit_reference_by_name(argv[0]);\n> +       if (!original) {\n> +               ret = error(_(\"commit cannot be found: %s\"), argv[0]);\n> +               goto out;\n> +       }\n> +\n> +       if (!original->parents) {\n> +               ret = error(_(\"cannot drop root commit %s: \"\n> +                             \"it has no parent to replay onto\"),\n> +                           argv[0]);\n> +               goto out;\n> +       } else if (original->parents->next) {\n> +               ret = error(_(\"cannot drop merge commit\"));\n\nWhy the if block adds which commit context, but not on the else if block?\n\n> +               goto out;\n> +       }\n\n> diff --git a/t/t3454-history-drop.sh b/t/t3454-history-drop.sh\n> new file mode 100755\n> index 0000000000..b320ff09b3\n> --- /dev/null\n> +++ b/t/t3454-history-drop.sh\n> @@ -0,0 +1,513 @@\n> +#!/bin/sh\n> +\n> +test_description='tests for git-history drop subcommand'\n> +\n> +. ./test-lib.sh\n> +. \"$TEST_DIRECTORY/lib-log-graph.sh\"\n> +\n> +expect_graph () {\n> +       cat >expect &&\n> +       lib_test_cmp_graph --graph --format=%s \"$@\"\n> +}\n\nThis function appears exactly the same at t6016 and t4215 but named as\ncheck_graph. I was gonna do a cleanup for a commit series I'm working\non to bring that function to `lib-log-graph.sh` because all these test\nfiles share that they import graph functions from `lib-log-graph.c`,\nmaybe you could do it?\n\nAlso:\n\nlib_test_cmp_graph () {\n        git log --graph \"$@\" >output &&\n        sed 's/ *$//' >output.sanitized <output &&\n        test_cmp expect output.sanitized\n}\n\nAlready uses `--graph` you can drop it from expect_graph()\n\nI can't say much more, from what I tested it worked fine but I haven't\ntested very exhaustively tho,\n\n--\nPablo\n"},{"id":"544597","messageId":"ah_8myzMQJ97Y0n0@pks.im","threadId":"65726","inReplyTo":"CAN5EUNQbSN7+SDWcrh3jTD7SXrnD=e-fQ9Qj9778R7cy2q4b1g@mail.gmail.com","subject":"Re: [PATCH 2/2] builtin/history: implement \"drop\" subcommand","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-03T10:06:19Z","receivedAt":"2026-06-03T10:06:34Z","isPatch":true,"body":"On Tue, Jun 02, 2026 at 09:31:17AM +0200, Pablo Sabater wrote:\n> El mar, 2 jun 2026 a las 8:16, Patrick Steinhardt (<ps@pks.im>) escribió:\n[snip]\n> > +       original = lookup_commit_reference_by_name(argv[0]);\n> > +       if (!original) {\n> > +               ret = error(_(\"commit cannot be found: %s\"), argv[0]);\n> > +               goto out;\n> > +       }\n> > +\n> > +       if (!original->parents) {\n> > +               ret = error(_(\"cannot drop root commit %s: \"\n> > +                             \"it has no parent to replay onto\"),\n> > +                           argv[0]);\n> > +               goto out;\n> > +       } else if (original->parents->next) {\n> > +               ret = error(_(\"cannot drop merge commit\"));\n> \n> Why the if block adds which commit context, but not on the else if block?\n\nTrue indeed, will adapt.\n\n> > diff --git a/t/t3454-history-drop.sh b/t/t3454-history-drop.sh\n> > new file mode 100755\n> > index 0000000000..b320ff09b3\n> > --- /dev/null\n> > +++ b/t/t3454-history-drop.sh\n> > @@ -0,0 +1,513 @@\n> > +#!/bin/sh\n> > +\n> > +test_description='tests for git-history drop subcommand'\n> > +\n> > +. ./test-lib.sh\n> > +. \"$TEST_DIRECTORY/lib-log-graph.sh\"\n> > +\n> > +expect_graph () {\n> > +       cat >expect &&\n> > +       lib_test_cmp_graph --graph --format=%s \"$@\"\n> > +}\n> \n> This function appears exactly the same at t6016 and t4215 but named as\n> check_graph. I was gonna do a cleanup for a commit series I'm working\n> on to bring that function to `lib-log-graph.sh` because all these test\n> files share that they import graph functions from `lib-log-graph.c`,\n> maybe you could do it?\n\nI'd rather keep this series focussed, but I wouldn't mind a follow up\nthat deduplicates these call sites.\n\n> Also:\n> \n> lib_test_cmp_graph () {\n>         git log --graph \"$@\" >output &&\n>         sed 's/ *$//' >output.sanitized <output &&\n>         test_cmp expect output.sanitized\n> }\n> \n> Already uses `--graph` you can drop it from expect_graph()\n\nTrue indeed, dropped the \"--graph\" argument.\n\nThanks!\n\nPatrick\n"},{"id":"544598","messageId":"ah_8s1Q1Kg0dgw6a@pks.im","threadId":"65726","inReplyTo":"xmqqbjdt25e3.fsf@gitster.g","subject":"Re: [PATCH 2/2] builtin/history: implement \"drop\" subcommand","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-03T10:06:43Z","receivedAt":"2026-06-03T10:06:48Z","isPatch":true,"body":"On Tue, Jun 02, 2026 at 08:43:48AM +0900, Junio C Hamano wrote:\n> Patrick Steinhardt <ps@pks.im> writes:\n[snip]\n> > +static int update_worktree(struct repository *repo,\n> > +\t\t\t   const struct commit *old_head,\n> > +\t\t\t   const struct commit *new_head,\n> > +\t\t\t   bool dry_run)\n> > +{\n> > +...\n> > +\n> > +out:\n> > +\tclear_unpack_trees_porcelain(&opts);\n> > +\trollback_lock_file(&lock);\n> > +\trelease_index(&index);\n> > +\tfree(desc_buf[0]);\n> > +\tfree(desc_buf[1]);\n> > +\treturn ret;\n> > +}\n> \n> The function looks very familiar---anybody who wants to perform\n> \"checkout <other-commit>\" needs to do exactly the above.  It is a\n> bit surprising and disappointing that this topic needs to *invent*\n> its own helper function and carry it as a file-scope static.\n\nIt certainly is. We basically have this whole dance in ~8 different\nlocations by now, and given the verbosity that is required for the whole\nsetup it's a good hint that the interface is not exactly great.\n\nOne of the functions that we might be able to reuse is `reset_head()`...\ngoes down the rabbit hole... ugh, this is turning out to be somewhat\npainful. I'll send a v2 that does the whole exercise, but I'm not a 100%\nconvinced it's the right thing to do. There's various assumptions that\nwe have to break:\n\n  - It assumes that the index is always clean.\n\n  - We don't have a dry-run mode.\n\n  - We need to stop it from updating any refs.\n\n  - We need to introduce another field to let the caller decide which\n    commit we're moving from.\n\nSo I'm 7 commits deep now adapting the function to our needs. But maybe\nthe end result is ultimately worth it...? We'll see.\n\nPatrick\n"},{"id":"544625","messageId":"20260603-b4-pks-history-drop-v2-0-742cb5b5176d@pks.im","threadId":"65726","inReplyTo":"20260601-b4-pks-history-drop-v1-0-643e32340d55@pks.im","subject":"[PATCH v2 0/9] builtin/history: introduce \"drop\" subcommand","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-03T16:13:59Z","receivedAt":"2026-06-03T16:14:07Z","isPatch":true,"body":"Hi,\n\nthis small patch series introduces the new \"drop\" subcommand for\ngit-history(1). As a reader might guess, the command does exactly that:\ngiven a commit, it will drop that commit from the commit history and\nreplay descendant branches on top of it.\n\nChanges in v2:\n  - Reworked `update_worktree()` to use `reset_head()`, which required a\n    bunch of changes to `reset_head()`.\n  - Consistently mention the commit that cannot be dropped as part of\n    error messages.\n  - Adapt error message to not use backticks anymore.\n  - Drop redundant \"--graph\" flag in a test helper.\n  - Link to v1: https://patch.msgid.link/20260601-b4-pks-history-drop-v1-0-643e32340d55@pks.im\n\nThanks!\n\nPatrick\n\n---\nPatrick Steinhardt (9):\n      read-cache: split out function to drop unmerged entries to stage 0\n      reset: drop `USE_THE_REPOSITORY_VARIABLE`\n      reset: modernize flags passed to `reset_head()`\n      reset: introduce dry-run mode\n      reset: introduce ability to skip reference updates\n      reset: allow the caller to specify the current HEAD object\n      reset: stop assuming that the caller passes in a clean index\n      builtin/history: split handling of ref updates into two phases\n      builtin/history: implement \"drop\" subcommand\n\n Documentation/git-history.adoc |  38 ++-\n builtin/history.c              | 289 ++++++++++++++++++++---\n builtin/rebase.c               |   2 +-\n read-cache-ll.h                |   1 +\n read-cache.c                   |  12 +-\n reset.c                        |  91 +++++---\n reset.h                        |  44 +++-\n sequencer.c                    |   2 +-\n t/meson.build                  |   1 +\n t/t3454-history-drop.sh        | 513 +++++++++++++++++++++++++++++++++++++++++\n 10 files changed, 905 insertions(+), 88 deletions(-)\n\nRange-diff versus v1:\n\n -:  ---------- >  1:  d6e4f3193d read-cache: split out function to drop unmerged entries to stage 0\n -:  ---------- >  2:  2eef3d77e4 reset: drop `USE_THE_REPOSITORY_VARIABLE`\n -:  ---------- >  3:  cbfd105ca3 reset: modernize flags passed to `reset_head()`\n -:  ---------- >  4:  bbb7f3c61c reset: introduce dry-run mode\n -:  ---------- >  5:  b3d036cea1 reset: introduce ability to skip reference updates\n -:  ---------- >  6:  7df1787049 reset: allow the caller to specify the current HEAD object\n -:  ---------- >  7:  f58254bbb8 reset: stop assuming that the caller passes in a clean index\n 1:  2a4b683b8c =  8:  9dee781f0a builtin/history: split handling of ref updates into two phases\n 2:  02712e70d3 !  9:  2b4e4075e6 builtin/history: implement \"drop\" subcommand\n    @@ Documentation/git-history.adoc: The staged addition of `unrelated.txt` has been\n     \n      ## builtin/history.c ##\n     @@\n    + #include \"read-cache.h\"\n    + #include \"refs.h\"\n    + #include \"replay.h\"\n    ++#include \"reset.h\"\n    + #include \"revision.h\"\n      #include \"sequencer.h\"\n      #include \"strvec.h\"\n      #include \"tree.h\"\n    @@ builtin/history.c: static int cmd_history_split(int argc,\n     +\t\t\t   const struct commit *new_head,\n     +\t\t\t   bool dry_run)\n     +{\n    -+\tstruct index_state index = INDEX_STATE_INIT(repo);\n    -+\tstruct unpack_trees_options opts = { 0 };\n    -+\tstruct lock_file lock = LOCK_INIT;\n    -+\tstruct tree_desc desc[2] = { 0 };\n    -+\tchar *desc_buf[2] = { 0 };\n    -+\tint ret;\n    -+\n    -+\tif (!dry_run &&\n    -+\t    repo_hold_locked_index(repo, &lock, LOCK_REPORT_ON_ERROR) < 0)\n    -+\t\treturn -1;\n    -+\n    -+\tif (read_index_from(&index, repo->index_file, repo->gitdir) < 0) {\n    -+\t\tret = error(_(\"unable to read index\"));\n    -+\t\tgoto out;\n    -+\t}\n    -+\n    -+\tsetup_unpack_trees_porcelain(&opts, \"history drop\");\n    -+\topts.head_idx = 1;\n    -+\topts.src_index = &index;\n    -+\topts.dst_index = &index;\n    -+\topts.fn = twoway_merge;\n    -+\topts.merge = 1;\n    -+\topts.update = !dry_run;\n    -+\topts.dry_run = dry_run;\n    -+\topts.preserve_ignored = 0;\n    -+\tinit_checkout_metadata(&opts.meta, NULL, &new_head->object.oid, NULL);\n    -+\n    -+\tdesc_buf[0] = fill_tree_descriptor(repo, &desc[0], &old_head->object.oid);\n    -+\tdesc_buf[1] = fill_tree_descriptor(repo, &desc[1], &new_head->object.oid);\n    -+\n    -+\tif (unpack_trees(2, desc, &opts)) {\n    -+\t\tret = -1;\n    -+\t\tgoto out;\n    -+\t}\n    -+\n    -+\tif (!dry_run) {\n    -+\t\tcache_tree_free(&index.cache_tree);\n    -+\n    -+\t\tif (write_locked_index(&index, &lock, COMMIT_LOCK)) {\n    -+\t\t\tret = error(_(\"could not write index\"));\n    -+\t\t\tgoto out;\n    -+\t\t}\n    -+\t}\n    -+\n    -+\tret = 0;\n    -+\n    -+out:\n    -+\tclear_unpack_trees_porcelain(&opts);\n    -+\trollback_lock_file(&lock);\n    -+\trelease_index(&index);\n    -+\tfree(desc_buf[0]);\n    -+\tfree(desc_buf[1]);\n    -+\treturn ret;\n    ++\tstruct reset_head_opts opts = {\n    ++\t\t.oid_from = &old_head->object.oid,\n    ++\t\t.oid = &new_head->object.oid,\n    ++\t\t.flags = RESET_HEAD_SKIP_REF_UPDATES,\n    ++\t};\n    ++\tif (dry_run)\n    ++\t\topts.flags |= RESET_HEAD_DRY_RUN;\n    ++\treturn reset_head(repo, &opts);\n     +}\n     +\n     +static int find_head_tree_change(struct repository *repo,\n    @@ builtin/history.c: static int cmd_history_split(int argc,\n     +\t\t\t    argv[0]);\n     +\t\tgoto out;\n     +\t} else if (original->parents->next) {\n    -+\t\tret = error(_(\"cannot drop merge commit\"));\n    ++\t\tret = error(_(\"cannot drop merge commit: %s\"), argv[0]);\n     +\t\tgoto out;\n     +\t}\n     +\n    @@ builtin/history.c: static int cmd_history_split(int argc,\n     +\t}\n     +\n     +\tif (head_moves && update_worktree(repo, old_head, new_head, false) < 0) {\n    -+\t\tret = error(_(\"failed to update working tree; \"\n    -+\t\t\t      \"run `git checkout HEAD` to sync\"));\n    ++\t\tret = error(_(\"could not update working tree to new commit %s\"),\n    ++\t\t\t    oid_to_hex(&new_head->object.oid));\n     +\t\tgoto out;\n     +\t}\n     +\n    @@ t/t3454-history-drop.sh (new)\n     +\n     +expect_graph () {\n     +\tcat >expect &&\n    -+\tlib_test_cmp_graph --graph --format=%s \"$@\"\n    ++\tlib_test_cmp_graph --format=%s \"$@\"\n     +}\n     +\n     +expect_log () {\n\n---\nbase-commit: 1666c1265231b0bc5f613fbbf3f0a9896cdef76e\nchange-id: 20260601-b4-pks-history-drop-28f6c6399e7b\n\n"},{"id":"544626","messageId":"20260603-b4-pks-history-drop-v2-1-742cb5b5176d@pks.im","threadId":"65726","inReplyTo":"20260603-b4-pks-history-drop-v2-0-742cb5b5176d@pks.im","subject":"[PATCH v2 1/9] read-cache: split out function to drop unmerged entries to stage 0","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-03T16:14:00Z","receivedAt":"2026-06-03T16:14:09Z","isPatch":true,"body":"In `repo_read_index_unmerged()` we read the index and then drop any\nunmerged entries to stage 0. In a subsequent commit we'll want to\nperform this operation on arbitrary indexes, not only the one of the\ngiven repository.\n\nPrepare for this by splitting out the functionality into a new function\nthat can act on an arbitrary index.\n\nWhile at it, fix a signedness mismatch when iterating through the index\ncache entries.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n read-cache-ll.h |  1 +\n read-cache.c    | 12 +++++++-----\n 2 files changed, 8 insertions(+), 5 deletions(-)\n\ndiff --git a/read-cache-ll.h b/read-cache-ll.h\nindex 2c8b4b21b1..71b87615eb 100644\n--- a/read-cache-ll.h\n+++ b/read-cache-ll.h\n@@ -309,6 +309,7 @@ int write_locked_index(struct index_state *, struct lock_file *lock, unsigned fl\n void discard_index(struct index_state *);\n void move_index_extensions(struct index_state *dst, struct index_state *src);\n int unmerged_index(const struct index_state *);\n+int index_state_unmerged_to_stage0(struct index_state *istate);\n \n /**\n  * Returns 1 if istate differs from tree, 0 otherwise.  If tree is NULL,\ndiff --git a/read-cache.c b/read-cache.c\nindex 21829102ae..799a5bc719 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -3403,13 +3403,15 @@ int write_locked_index(struct index_state *istate, struct lock_file *lock,\n  */\n int repo_read_index_unmerged(struct repository *repo)\n {\n-\tstruct index_state *istate;\n-\tint i;\n+\trepo_read_index(repo);\n+\treturn index_state_unmerged_to_stage0(repo->index);\n+}\n+\n+int index_state_unmerged_to_stage0(struct index_state *istate)\n+{\n \tint unmerged = 0;\n \n-\trepo_read_index(repo);\n-\tistate = repo->index;\n-\tfor (i = 0; i < istate->cache_nr; i++) {\n+\tfor (unsigned int i = 0; i < istate->cache_nr; i++) {\n \t\tstruct cache_entry *ce = istate->cache[i];\n \t\tstruct cache_entry *new_ce;\n \t\tint len;\n\n-- \n2.54.0.1064.gd145956f57.dirty\n\n"},{"id":"544627","messageId":"20260603-b4-pks-history-drop-v2-2-742cb5b5176d@pks.im","threadId":"65726","inReplyTo":"20260603-b4-pks-history-drop-v2-0-742cb5b5176d@pks.im","subject":"[PATCH v2 2/9] reset: drop `USE_THE_REPOSITORY_VARIABLE`","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-03T16:14:01Z","receivedAt":"2026-06-03T16:14:11Z","isPatch":true,"body":"In \"reset.c\" we still have references to `the_repository`, even though\nthe only entry point into the file already receives a repository as\nparameter.\n\nUpdate all uses of `the_repository` to instead use the passed-in repo\nand drop `USE_THE_REPOSITORY_VARIABLE`.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reset.c | 27 +++++++++++++--------------\n 1 file changed, 13 insertions(+), 14 deletions(-)\n\ndiff --git a/reset.c b/reset.c\nindex 46e30e6394..3b3cb74dab 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -1,5 +1,3 @@\n-#define USE_THE_REPOSITORY_VARIABLE\n-\n #include \"git-compat-util.h\"\n #include \"cache-tree.h\"\n #include \"gettext.h\"\n@@ -13,7 +11,8 @@\n #include \"unpack-trees.h\"\n #include \"hook.h\"\n \n-static int update_refs(const struct reset_head_opts *opts,\n+static int update_refs(struct repository *repo,\n+\t\t       const struct reset_head_opts *opts,\n \t\t       const struct object_id *oid,\n \t\t       const struct object_id *head)\n {\n@@ -42,19 +41,19 @@ static int update_refs(const struct reset_head_opts *opts,\n \tprefix_len = msg.len;\n \n \tif (update_orig_head) {\n-\t\tif (!repo_get_oid(the_repository, \"ORIG_HEAD\", &oid_old_orig))\n+\t\tif (!repo_get_oid(repo, \"ORIG_HEAD\", &oid_old_orig))\n \t\t\told_orig = &oid_old_orig;\n \t\tif (head) {\n \t\t\tif (!reflog_orig_head) {\n \t\t\t\tstrbuf_addstr(&msg, \"updating ORIG_HEAD\");\n \t\t\t\treflog_orig_head = msg.buf;\n \t\t\t}\n-\t\t\trefs_update_ref(get_main_ref_store(the_repository),\n+\t\t\trefs_update_ref(get_main_ref_store(repo),\n \t\t\t\t\treflog_orig_head, \"ORIG_HEAD\",\n \t\t\t\t\torig_head ? orig_head : head,\n \t\t\t\t\told_orig, 0, UPDATE_REFS_MSG_ON_ERR);\n \t\t} else if (old_orig)\n-\t\t\trefs_delete_ref(get_main_ref_store(the_repository),\n+\t\t\trefs_delete_ref(get_main_ref_store(repo),\n \t\t\t\t\tNULL, \"ORIG_HEAD\", old_orig, 0);\n \t}\n \n@@ -64,23 +63,23 @@ static int update_refs(const struct reset_head_opts *opts,\n \t\treflog_head = msg.buf;\n \t}\n \tif (!switch_to_branch)\n-\t\tret = refs_update_ref(get_main_ref_store(the_repository),\n+\t\tret = refs_update_ref(get_main_ref_store(repo),\n \t\t\t\t      reflog_head, \"HEAD\", oid, head,\n \t\t\t\t      detach_head ? REF_NO_DEREF : 0,\n \t\t\t\t      UPDATE_REFS_MSG_ON_ERR);\n \telse {\n-\t\tret = refs_update_ref(get_main_ref_store(the_repository),\n+\t\tret = refs_update_ref(get_main_ref_store(repo),\n \t\t\t\t      reflog_branch ? reflog_branch : reflog_head,\n \t\t\t\t      switch_to_branch, oid, NULL, 0,\n \t\t\t\t      UPDATE_REFS_MSG_ON_ERR);\n \t\tif (!ret)\n-\t\t\tret = refs_update_symref(get_main_ref_store(the_repository),\n+\t\t\tret = refs_update_symref(get_main_ref_store(repo),\n \t\t\t\t\t\t \"HEAD\", switch_to_branch,\n \t\t\t\t\t\t reflog_head);\n \t}\n \tif (!ret && run_hook)\n-\t\trun_hooks_l(the_repository, \"post-checkout\",\n-\t\t\t    oid_to_hex(head ? head : null_oid(the_hash_algo)),\n+\t\trun_hooks_l(repo, \"post-checkout\",\n+\t\t\t    oid_to_hex(head ? head : null_oid(repo->hash_algo)),\n \t\t\t    oid_to_hex(oid), \"1\", NULL);\n \tstrbuf_release(&msg);\n \treturn ret;\n@@ -126,7 +125,7 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \t\toid = &head_oid;\n \n \tif (refs_only)\n-\t\treturn update_refs(opts, oid, head);\n+\t\treturn update_refs(r, opts, oid, head);\n \n \taction = reset_hard ? \"reset\" : \"checkout\";\n \tsetup_unpack_trees_porcelain(&unpack_tree_opts, action);\n@@ -163,7 +162,7 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \t\tgoto leave_reset_head;\n \t}\n \n-\ttree = repo_parse_tree_indirect(the_repository, oid);\n+\ttree = repo_parse_tree_indirect(r, oid);\n \tif (!tree) {\n \t\tret = error(_(\"unable to read tree (%s)\"), oid_to_hex(oid));\n \t\tgoto leave_reset_head;\n@@ -177,7 +176,7 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \t}\n \n \tif (oid != &head_oid || update_orig_head || switch_to_branch)\n-\t\tret = update_refs(opts, oid, head);\n+\t\tret = update_refs(r, opts, oid, head);\n \n leave_reset_head:\n \trollback_lock_file(&lock);\n\n-- \n2.54.0.1064.gd145956f57.dirty\n\n"},{"id":"544628","messageId":"20260603-b4-pks-history-drop-v2-3-742cb5b5176d@pks.im","threadId":"65726","inReplyTo":"20260603-b4-pks-history-drop-v2-0-742cb5b5176d@pks.im","subject":"[PATCH v2 3/9] reset: modernize flags passed to `reset_head()`","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-03T16:14:02Z","receivedAt":"2026-06-03T16:14:13Z","isPatch":true,"body":"The flags passed to `reset_head()` are declared as defines. This has\nfallen a bit out of practice nowadays, where we instead prefer to use\nenums.\n\nModernize the code accordingly.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n builtin/rebase.c |  2 +-\n reset.c          |  4 ++--\n reset.h          | 30 ++++++++++++++++++------------\n sequencer.c      |  2 +-\n 4 files changed, 22 insertions(+), 16 deletions(-)\n\ndiff --git a/builtin/rebase.c b/builtin/rebase.c\nindex fa4f5d9306..6351a3aa32 100644\n--- a/builtin/rebase.c\n+++ b/builtin/rebase.c\n@@ -1876,7 +1876,7 @@ int cmd_rebase(int argc,\n \t\t    options.reflog_action, options.onto_name);\n \tropts.oid = &options.onto->object.oid;\n \tropts.orig_head = &options.orig_head->object.oid;\n-\tropts.flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD |\n+\tropts.flags = RESET_HEAD_DETACH | RESET_HEAD_ORIG_HEAD |\n \t\t\tRESET_HEAD_RUN_POST_CHECKOUT_HOOK;\n \tropts.head_msg = msg.buf;\n \tropts.default_reflog_action = options.reflog_action;\ndiff --git a/reset.c b/reset.c\nindex 3b3cb74dab..9ff14f5ed1 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -18,7 +18,7 @@ static int update_refs(struct repository *repo,\n {\n \tunsigned detach_head = opts->flags & RESET_HEAD_DETACH;\n \tunsigned run_hook = opts->flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK;\n-\tunsigned update_orig_head = opts->flags & RESET_ORIG_HEAD;\n+\tunsigned update_orig_head = opts->flags & RESET_HEAD_ORIG_HEAD;\n \tconst struct object_id *orig_head = opts->orig_head;\n \tconst char *switch_to_branch = opts->branch;\n \tconst char *reflog_branch = opts->branch_msg;\n@@ -91,7 +91,7 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \tconst char *switch_to_branch = opts->branch;\n \tunsigned reset_hard = opts->flags & RESET_HEAD_HARD;\n \tunsigned refs_only = opts->flags & RESET_HEAD_REFS_ONLY;\n-\tunsigned update_orig_head = opts->flags & RESET_ORIG_HEAD;\n+\tunsigned update_orig_head = opts->flags & RESET_HEAD_ORIG_HEAD;\n \tstruct object_id *head = NULL, head_oid;\n \tstruct tree_desc desc[2] = { { NULL }, { NULL } };\n \tstruct lock_file lock = LOCK_INIT;\ndiff --git a/reset.h b/reset.h\nindex a28f81829d..97ced2601e 100644\n--- a/reset.h\n+++ b/reset.h\n@@ -6,16 +6,22 @@\n \n #define GIT_REFLOG_ACTION_ENVIRONMENT \"GIT_REFLOG_ACTION\"\n \n-/* Request a detached checkout */\n-#define RESET_HEAD_DETACH (1<<0)\n-/* Request a reset rather than a checkout */\n-#define RESET_HEAD_HARD (1<<1)\n-/* Run the post-checkout hook */\n-#define RESET_HEAD_RUN_POST_CHECKOUT_HOOK (1<<2)\n-/* Only update refs, do not touch the worktree */\n-#define RESET_HEAD_REFS_ONLY (1<<3)\n-/* Update ORIG_HEAD as well as HEAD */\n-#define RESET_ORIG_HEAD (1<<4)\n+enum reset_head_flags {\n+\t/* Request a detached checkout */\n+\tRESET_HEAD_DETACH = (1 << 0),\n+\n+\t/* Request a reset rather than a checkout */\n+\tRESET_HEAD_HARD = (1 << 1),\n+\n+\t/* Run the post-checkout hook */\n+\tRESET_HEAD_RUN_POST_CHECKOUT_HOOK = (1 << 2),\n+\n+\t/* Only update refs, do not touch the worktree */\n+\tRESET_HEAD_REFS_ONLY = (1 << 3),\n+\n+\t/* Update ORIG_HEAD as well as HEAD */\n+\tRESET_HEAD_ORIG_HEAD = (1 << 4),\n+};\n \n struct reset_head_opts {\n \t/*\n@@ -33,7 +39,7 @@ struct reset_head_opts {\n \t/*\n \t * Flags defined above.\n \t */\n-\tunsigned flags;\n+\tenum reset_head_flags flags;\n \t/*\n \t * Optional reflog message for branch, defaults to head_msg.\n \t */\n@@ -45,7 +51,7 @@ struct reset_head_opts {\n \tconst char *head_msg;\n \t/*\n \t * Optional reflog message for ORIG_HEAD, if this omitted and flags\n-\t * contains RESET_ORIG_HEAD then default_reflog_action must be given.\n+\t * contains RESET_HEAD_ORIG_HEAD then default_reflog_action must be given.\n \t */\n \tconst char *orig_head_msg;\n \t/*\ndiff --git a/sequencer.c b/sequencer.c\nindex 1ee4b2875b..0b89a977b0 100644\n--- a/sequencer.c\n+++ b/sequencer.c\n@@ -4870,7 +4870,7 @@ static int checkout_onto(struct repository *r, struct replay_opts *opts,\n \tstruct reset_head_opts ropts = {\n \t\t.oid = onto,\n \t\t.orig_head = orig_head,\n-\t\t.flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD |\n+\t\t.flags = RESET_HEAD_DETACH | RESET_HEAD_ORIG_HEAD |\n \t\t\t\tRESET_HEAD_RUN_POST_CHECKOUT_HOOK,\n \t\t.head_msg = reflog_message(opts, \"start\", \"checkout %s\",\n \t\t\t\t\t   onto_name),\n\n-- \n2.54.0.1064.gd145956f57.dirty\n\n"},{"id":"544629","messageId":"20260603-b4-pks-history-drop-v2-4-742cb5b5176d@pks.im","threadId":"65726","inReplyTo":"20260603-b4-pks-history-drop-v2-0-742cb5b5176d@pks.im","subject":"[PATCH v2 4/9] reset: introduce dry-run mode","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-03T16:14:03Z","receivedAt":"2026-06-03T16:14:15Z","isPatch":true,"body":"In a subsequent commit we'll add add another caller to `reset_head()`\nthat wants to perform a dry-run check of whether it would be possible to\nudpate the index and working tree when moving to a new commit. Introduce\na new flag that lets the caller perform this operation.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reset.c | 44 +++++++++++++++++++++++++++++++++-----------\n reset.h |  6 ++++++\n 2 files changed, 39 insertions(+), 11 deletions(-)\n\ndiff --git a/reset.c b/reset.c\nindex 9ff14f5ed1..a8d7eea4d6 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -92,11 +92,14 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \tunsigned reset_hard = opts->flags & RESET_HEAD_HARD;\n \tunsigned refs_only = opts->flags & RESET_HEAD_REFS_ONLY;\n \tunsigned update_orig_head = opts->flags & RESET_HEAD_ORIG_HEAD;\n+\tunsigned dry_run = opts->flags & RESET_HEAD_DRY_RUN;\n \tstruct object_id *head = NULL, head_oid;\n \tstruct tree_desc desc[2] = { { NULL }, { NULL } };\n \tstruct lock_file lock = LOCK_INIT;\n \tstruct unpack_trees_options unpack_tree_opts = { 0 };\n \tstruct tree *tree;\n+\tstruct index_state scratch_index = INDEX_STATE_INIT(r);\n+\tstruct index_state *istate;\n \tconst char *action;\n \tint ret = 0, nr = 0;\n \n@@ -109,7 +112,7 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \tif (opts->branch_msg && !opts->branch)\n \t\tBUG(\"branch reflog message given without a branch\");\n \n-\tif (!refs_only && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {\n+\tif (!refs_only && !dry_run && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {\n \t\tret = -1;\n \t\tgoto leave_reset_head;\n \t}\n@@ -124,16 +127,36 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \tif (!oid)\n \t\toid = &head_oid;\n \n-\tif (refs_only)\n-\t\treturn update_refs(r, opts, oid, head);\n+\tif (refs_only) {\n+\t\tif (!dry_run)\n+\t\t\treturn update_refs(r, opts, oid, head);\n+\t\treturn 0;\n+\t}\n+\n+\tif (dry_run) {\n+\t\tif (read_index_from(&scratch_index, r->index_file, r->gitdir) < 0 ||\n+\t\t    index_state_unmerged_to_stage0(&scratch_index) < 0) {\n+\t\t\tret = error(_(\"could not read index\"));\n+\t\t\tgoto leave_reset_head;\n+\t\t}\n+\n+\t\tistate = &scratch_index;\n+\t} else {\n+\t\tif (repo_read_index_unmerged(r) < 0) {\n+\t\t\tret = error(_(\"could not read index\"));\n+\t\t\tgoto leave_reset_head;\n+\t\t}\n+\t\tistate = r->index;\n+\t}\n \n \taction = reset_hard ? \"reset\" : \"checkout\";\n \tsetup_unpack_trees_porcelain(&unpack_tree_opts, action);\n \tunpack_tree_opts.head_idx = 1;\n-\tunpack_tree_opts.src_index = r->index;\n-\tunpack_tree_opts.dst_index = r->index;\n+\tunpack_tree_opts.src_index = istate;\n+\tunpack_tree_opts.dst_index = istate;\n \tunpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge;\n-\tunpack_tree_opts.update = 1;\n+\tunpack_tree_opts.update = !dry_run;\n+\tunpack_tree_opts.dry_run = dry_run;\n \tunpack_tree_opts.merge = 1;\n \tunpack_tree_opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */\n \tunpack_tree_opts.skip_cache_tree_update = 1;\n@@ -141,11 +164,6 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \tif (reset_hard)\n \t\tunpack_tree_opts.reset = UNPACK_RESET_PROTECT_UNTRACKED;\n \n-\tif (repo_read_index_unmerged(r) < 0) {\n-\t\tret = error(_(\"could not read index\"));\n-\t\tgoto leave_reset_head;\n-\t}\n-\n \tif (!reset_hard && !fill_tree_descriptor(r, &desc[nr++], &head_oid)) {\n \t\tret = error(_(\"failed to find tree of %s\"),\n \t\t\t    oid_to_hex(&head_oid));\n@@ -162,6 +180,9 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \t\tgoto leave_reset_head;\n \t}\n \n+\tif (dry_run)\n+\t\tgoto leave_reset_head;\n+\n \ttree = repo_parse_tree_indirect(r, oid);\n \tif (!tree) {\n \t\tret = error(_(\"unable to read tree (%s)\"), oid_to_hex(oid));\n@@ -181,6 +202,7 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n leave_reset_head:\n \trollback_lock_file(&lock);\n \tclear_unpack_trees_porcelain(&unpack_tree_opts);\n+\trelease_index(&scratch_index);\n \twhile (nr)\n \t\tfree((void *)desc[--nr].buffer);\n \treturn ret;\ndiff --git a/reset.h b/reset.h\nindex 97ced2601e..9f696382c1 100644\n--- a/reset.h\n+++ b/reset.h\n@@ -21,6 +21,12 @@ enum reset_head_flags {\n \n \t/* Update ORIG_HEAD as well as HEAD */\n \tRESET_HEAD_ORIG_HEAD = (1 << 4),\n+\n+\t/*\n+\t * Perform a dry-run by performing the operation without updating\n+\t * any user-visible state.\n+\t */\n+\tRESET_HEAD_DRY_RUN = (1 << 5),\n };\n \n struct reset_head_opts {\n\n-- \n2.54.0.1064.gd145956f57.dirty\n\n"},{"id":"544630","messageId":"20260603-b4-pks-history-drop-v2-5-742cb5b5176d@pks.im","threadId":"65726","inReplyTo":"20260603-b4-pks-history-drop-v2-0-742cb5b5176d@pks.im","subject":"[PATCH v2 5/9] reset: introduce ability to skip reference updates","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-03T16:14:04Z","receivedAt":"2026-06-03T16:14:19Z","isPatch":true,"body":"In a subsequent commit we'll introduce a new caller to `reset_head()`\nthat really only wants to update the index and working tree, without\nupdating any references. Introduce a new flag that lets the caller\nperform this operation.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reset.c | 7 ++++++-\n reset.h | 3 +++\n 2 files changed, 9 insertions(+), 1 deletion(-)\n\ndiff --git a/reset.c b/reset.c\nindex a8d7eea4d6..ed9df6ca5c 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -93,6 +93,7 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \tunsigned refs_only = opts->flags & RESET_HEAD_REFS_ONLY;\n \tunsigned update_orig_head = opts->flags & RESET_HEAD_ORIG_HEAD;\n \tunsigned dry_run = opts->flags & RESET_HEAD_DRY_RUN;\n+\tunsigned skip_ref_updates = opts->flags & RESET_HEAD_SKIP_REF_UPDATES;\n \tstruct object_id *head = NULL, head_oid;\n \tstruct tree_desc desc[2] = { { NULL }, { NULL } };\n \tstruct lock_file lock = LOCK_INIT;\n@@ -112,6 +113,9 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \tif (opts->branch_msg && !opts->branch)\n \t\tBUG(\"branch reflog message given without a branch\");\n \n+\tif (skip_ref_updates && (opts->branch || refs_only))\n+\t\tBUG(\"asked to perform ref updates and skip them at the same time\");\n+\n \tif (!refs_only && !dry_run && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {\n \t\tret = -1;\n \t\tgoto leave_reset_head;\n@@ -196,7 +200,8 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \t\tgoto leave_reset_head;\n \t}\n \n-\tif (oid != &head_oid || update_orig_head || switch_to_branch)\n+\tif (!skip_ref_updates &&\n+\t    (oid != &head_oid || update_orig_head || switch_to_branch))\n \t\tret = update_refs(r, opts, oid, head);\n \n leave_reset_head:\ndiff --git a/reset.h b/reset.h\nindex 9f696382c1..cb0700ffa7 100644\n--- a/reset.h\n+++ b/reset.h\n@@ -27,6 +27,9 @@ enum reset_head_flags {\n \t * any user-visible state.\n \t */\n \tRESET_HEAD_DRY_RUN = (1 << 5),\n+\n+\t/* Skip updating any references, only update the worktree and index. */\n+\tRESET_HEAD_SKIP_REF_UPDATES = (1 << 6),\n };\n \n struct reset_head_opts {\n\n-- \n2.54.0.1064.gd145956f57.dirty\n\n"},{"id":"544631","messageId":"20260603-b4-pks-history-drop-v2-6-742cb5b5176d@pks.im","threadId":"65726","inReplyTo":"20260603-b4-pks-history-drop-v2-0-742cb5b5176d@pks.im","subject":"[PATCH v2 6/9] reset: allow the caller to specify the current HEAD object","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-03T16:14:05Z","receivedAt":"2026-06-03T16:14:21Z","isPatch":true,"body":"When calling `reset_head()` we automatically derive the commit that the\ncallers wants to move from by reading the HEAD commit. Some callers may\nalready have resolved it, or they may want to move from a different\ncommit that doesn't match HEAD.\n\nIntroduce a new `oid_from` option that lets the caller specify the\ncommit.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reset.c | 5 ++++-\n reset.h | 5 +++++\n 2 files changed, 9 insertions(+), 1 deletion(-)\n\ndiff --git a/reset.c b/reset.c\nindex ed9df6ca5c..7ff72de5d2 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -121,7 +121,10 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \t\tgoto leave_reset_head;\n \t}\n \n-\tif (!repo_get_oid(r, \"HEAD\", &head_oid)) {\n+\tif (opts->oid_from) {\n+\t\toidcpy(&head_oid, opts->oid_from);\n+\t\thead = &head_oid;\n+\t} else if (!repo_get_oid(r, \"HEAD\", &head_oid)) {\n \t\thead = &head_oid;\n \t} else if (!oid || !reset_hard) {\n \t\tret = error(_(\"could not determine HEAD revision\"));\ndiff --git a/reset.h b/reset.h\nindex cb0700ffa7..51ce114543 100644\n--- a/reset.h\n+++ b/reset.h\n@@ -37,6 +37,11 @@ struct reset_head_opts {\n \t * The commit to checkout/reset to. Defaults to HEAD.\n \t */\n \tconst struct object_id *oid;\n+\t/*\n+\t * The commit to checkout/reset from when doing a two-way merge. This\n+\t * is used as one of the sides to merge.\n+\t */\n+\tconst struct object_id *oid_from;\n \t/*\n \t * Optional value to set ORIG_HEAD. Defaults to HEAD.\n \t */\n\n-- \n2.54.0.1064.gd145956f57.dirty\n\n"},{"id":"544632","messageId":"20260603-b4-pks-history-drop-v2-7-742cb5b5176d@pks.im","threadId":"65726","inReplyTo":"20260603-b4-pks-history-drop-v2-0-742cb5b5176d@pks.im","subject":"[PATCH v2 7/9] reset: stop assuming that the caller passes in a clean index","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-03T16:14:06Z","receivedAt":"2026-06-03T16:14:23Z","isPatch":true,"body":"In 652bd0211d (rebase: use 'skip_cache_tree_update' option, 2022-11-10),\nwe updated `reset_head()` to stop updating the index tree cache. This\nwas done as a performance optimization: the function is only called by\n\"sequencer.c\" and \"rebase.c\", both of which assume a clean index before\nthey perform their operation, so we know that the end result will be a\nclean index, too. Consequently, we can skip recomputing the cache as we\ncan instead use `prime_cache_tree()` directly.\n\nIn a subsequent commit we're about to add a new caller though where the\nassumption doesn't hold anymore: the index may be dirty before calling\n`reset_head()`, and consequently we cannot prime the cache with a given\ntree anymore as the index and tree will mismatch.\n\nAdapt the logic so that we only skip the cache tree update in case we're\ndoing a hard reset. While we could introduce logic that only skips the\nupdate in case the incoming index was dirty already, that doesn't really\nfeel worth it: after all, the mentioned commit says itself that the\nperformance improvement was negligible anyway.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reset.c | 8 +++++---\n 1 file changed, 5 insertions(+), 3 deletions(-)\n\ndiff --git a/reset.c b/reset.c\nindex 7ff72de5d2..05eb80216c 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -166,10 +166,11 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \tunpack_tree_opts.dry_run = dry_run;\n \tunpack_tree_opts.merge = 1;\n \tunpack_tree_opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */\n-\tunpack_tree_opts.skip_cache_tree_update = 1;\n \tinit_checkout_metadata(&unpack_tree_opts.meta, switch_to_branch, oid, NULL);\n-\tif (reset_hard)\n+\tif (reset_hard) {\n+\t\tunpack_tree_opts.skip_cache_tree_update = 1;\n \t\tunpack_tree_opts.reset = UNPACK_RESET_PROTECT_UNTRACKED;\n+\t}\n \n \tif (!reset_hard && !fill_tree_descriptor(r, &desc[nr++], &head_oid)) {\n \t\tret = error(_(\"failed to find tree of %s\"),\n@@ -196,7 +197,8 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \t\tgoto leave_reset_head;\n \t}\n \n-\tprime_cache_tree(r, r->index, tree);\n+\tif (reset_hard)\n+\t\tprime_cache_tree(r, r->index, tree);\n \n \tif (write_locked_index(r->index, &lock, COMMIT_LOCK) < 0) {\n \t\tret = error(_(\"could not write index\"));\n\n-- \n2.54.0.1064.gd145956f57.dirty\n\n"},{"id":"544633","messageId":"20260603-b4-pks-history-drop-v2-8-742cb5b5176d@pks.im","threadId":"65726","inReplyTo":"20260603-b4-pks-history-drop-v2-0-742cb5b5176d@pks.im","subject":"[PATCH v2 8/9] builtin/history: split handling of ref updates into two phases","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-03T16:14:07Z","receivedAt":"2026-06-03T16:14:26Z","isPatch":true,"body":"The function `handle_reference_updates()` is used by git-history(1) to\nupdate all references that refer to commits that have been rewritten. As\nsuch, it performs two steps:\n\n  - It gathers the references that need to be updated in the first\n    place.\n\n  - It prepares and commits the reference transaction.\n\nIn a subsequent commit we'll want to handle those two steps separately.\nPrepare for this by splitting up the function into two.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n builtin/history.c | 102 ++++++++++++++++++++++++++++++++++--------------------\n 1 file changed, 64 insertions(+), 38 deletions(-)\n\ndiff --git a/builtin/history.c b/builtin/history.c\nindex 0fc06fb204..4fadf38c32 100644\n--- a/builtin/history.c\n+++ b/builtin/history.c\n@@ -333,21 +333,17 @@ static int handle_ref_update(struct ref_transaction *transaction,\n \t\t\t\t      NULL, NULL, 0, reflog_msg, err);\n }\n \n-static int handle_reference_updates(struct rev_info *revs,\n-\t\t\t\t    enum ref_action action,\n-\t\t\t\t    struct commit *original,\n-\t\t\t\t    struct commit *rewritten,\n-\t\t\t\t    const char *reflog_msg,\n-\t\t\t\t    int dry_run,\n-\t\t\t\t    enum replay_empty_commit_action empty)\n+static int compute_pending_ref_updates(struct rev_info *revs,\n+\t\t\t\t       enum ref_action action,\n+\t\t\t\t       struct commit *original,\n+\t\t\t\t       struct commit *rewritten,\n+\t\t\t\t       enum replay_empty_commit_action empty,\n+\t\t\t\t       struct replay_result *result)\n {\n \tconst struct name_decoration *decoration;\n \tstruct replay_revisions_options opts = {\n \t\t.empty = empty,\n \t};\n-\tstruct replay_result result = { 0 };\n-\tstruct ref_transaction *transaction = NULL;\n-\tstruct strbuf err = STRBUF_INIT;\n \tchar hex[GIT_MAX_HEXSZ + 1];\n \tbool detached_head;\n \tint head_flags = 0;\n@@ -359,34 +355,13 @@ static int handle_reference_updates(struct rev_info *revs,\n \n \topts.onto = oid_to_hex_r(hex, &rewritten->object.oid);\n \n-\tret = replay_revisions(revs, &opts, &result);\n+\tret = replay_revisions(revs, &opts, result);\n \tif (ret)\n-\t\tgoto out;\n+\t\treturn ret;\n \n \tif (action != REF_ACTION_BRANCHES && action != REF_ACTION_HEAD)\n \t\tBUG(\"unsupported ref action %d\", action);\n \n-\tif (!dry_run) {\n-\t\ttransaction = ref_store_transaction_begin(get_main_ref_store(revs->repo), 0, &err);\n-\t\tif (!transaction) {\n-\t\t\tret = error(_(\"failed to begin ref transaction: %s\"), err.buf);\n-\t\t\tgoto out;\n-\t\t}\n-\t}\n-\n-\tfor (size_t i = 0; i < result.updates_nr; i++) {\n-\t\tret = handle_ref_update(transaction,\n-\t\t\t\t\tresult.updates[i].refname,\n-\t\t\t\t\t&result.updates[i].new_oid,\n-\t\t\t\t\t&result.updates[i].old_oid,\n-\t\t\t\t\treflog_msg, &err);\n-\t\tif (ret) {\n-\t\t\tret = error(_(\"failed to update ref '%s': %s\"),\n-\t\t\t\t    result.updates[i].refname, err.buf);\n-\t\t\tgoto out;\n-\t\t}\n-\t}\n-\n \t/*\n \t * `replay_revisions()` only updates references that are\n \t * ancestors of `rewritten`, so we need to manually\n@@ -414,14 +389,43 @@ static int handle_reference_updates(struct rev_info *revs,\n \t\t    !detached_head)\n \t\t\tcontinue;\n \n+\t\tALLOC_GROW(result->updates, result->updates_nr + 1, result->updates_alloc);\n+\t\tresult->updates[result->updates_nr].refname = xstrdup(decoration->name);\n+\t\tresult->updates[result->updates_nr].old_oid = original->object.oid;\n+\t\tresult->updates[result->updates_nr].new_oid = rewritten->object.oid;\n+\t\tresult->updates_nr++;\n+\t}\n+\n+\treturn 0;\n+}\n+\n+static int apply_pending_ref_updates(struct repository *repo,\n+\t\t\t\t     const struct replay_result *result,\n+\t\t\t\t     const char *reflog_msg,\n+\t\t\t\t     int dry_run)\n+{\n+\tstruct ref_transaction *transaction = NULL;\n+\tstruct strbuf err = STRBUF_INIT;\n+\tint ret;\n+\n+\tif (!dry_run) {\n+\t\ttransaction = ref_store_transaction_begin(get_main_ref_store(repo),\n+\t\t\t\t\t\t\t  0, &err);\n+\t\tif (!transaction) {\n+\t\t\tret = error(_(\"failed to begin ref transaction: %s\"), err.buf);\n+\t\t\tgoto out;\n+\t\t}\n+\t}\n+\n+\tfor (size_t i = 0; i < result->updates_nr; i++) {\n \t\tret = handle_ref_update(transaction,\n-\t\t\t\t\tdecoration->name,\n-\t\t\t\t\t&rewritten->object.oid,\n-\t\t\t\t\t&original->object.oid,\n+\t\t\t\t\tresult->updates[i].refname,\n+\t\t\t\t\t&result->updates[i].new_oid,\n+\t\t\t\t\t&result->updates[i].old_oid,\n \t\t\t\t\treflog_msg, &err);\n \t\tif (ret) {\n \t\t\tret = error(_(\"failed to update ref '%s': %s\"),\n-\t\t\t\t    decoration->name, err.buf);\n+\t\t\t\t    result->updates[i].refname, err.buf);\n \t\t\tgoto out;\n \t\t}\n \t}\n@@ -435,11 +439,33 @@ static int handle_reference_updates(struct rev_info *revs,\n \n out:\n \tref_transaction_free(transaction);\n-\treplay_result_release(&result);\n \tstrbuf_release(&err);\n \treturn ret;\n }\n \n+static int handle_reference_updates(struct rev_info *revs,\n+\t\t\t\t    enum ref_action action,\n+\t\t\t\t    struct commit *original,\n+\t\t\t\t    struct commit *rewritten,\n+\t\t\t\t    const char *reflog_msg,\n+\t\t\t\t    int dry_run,\n+\t\t\t\t    enum replay_empty_commit_action empty)\n+{\n+\tstruct replay_result result = { 0 };\n+\tint ret;\n+\n+\tret = compute_pending_ref_updates(revs, action, original, rewritten,\n+\t\t\t\t\t  empty, &result);\n+\tif (ret)\n+\t\tgoto out;\n+\n+\tret = apply_pending_ref_updates(revs->repo, &result, reflog_msg, dry_run);\n+\n+out:\n+\treplay_result_release(&result);\n+\treturn ret;\n+}\n+\n static int commit_became_empty(struct repository *repo,\n \t\t\t       struct commit *original,\n \t\t\t       struct tree *result)\n\n-- \n2.54.0.1064.gd145956f57.dirty\n\n"},{"id":"544634","messageId":"20260603-b4-pks-history-drop-v2-9-742cb5b5176d@pks.im","threadId":"65726","inReplyTo":"20260603-b4-pks-history-drop-v2-0-742cb5b5176d@pks.im","subject":"[PATCH v2 9/9] builtin/history: implement \"drop\" subcommand","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-03T16:14:08Z","receivedAt":"2026-06-03T16:14:29Z","isPatch":true,"body":"A common operation when editing the commit history is to drop a specific\ncommit from the history entirely, but this operation is not currently\ncovered by git-history(1).\n\nA couple of noteworthy bits:\n\n  - This is the first git-history(1) command that will ultimately result\n    in changes to both the index and the working tree. We thus have to\n    add logic to merge resulting changes into those.\n\n  - It is still not possible to replay merge commits, so this limitation\n    is inherited for the new \"drop\" command.\n\n  - For now we refuse to drop root commits. While we _can_ indeed drop\n    root commits in the general case, there are edge cases where the\n    resulting history would become completely empty. This is thus left\n    to a subsequent patch series.\n\nOther than that, most of the logic is rather straight-forward as we can\ncontinue to build on the preexisting logic in git-history(1) for most of\nthe part.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n Documentation/git-history.adoc |  38 ++-\n builtin/history.c              | 187 +++++++++++++++\n t/meson.build                  |   1 +\n t/t3454-history-drop.sh        | 513 +++++++++++++++++++++++++++++++++++++++++\n 4 files changed, 738 insertions(+), 1 deletion(-)\n\ndiff --git a/Documentation/git-history.adoc b/Documentation/git-history.adoc\nindex 2ba8121795..4eac732fd2 100644\n--- a/Documentation/git-history.adoc\n+++ b/Documentation/git-history.adoc\n@@ -8,6 +8,7 @@ git-history - EXPERIMENTAL: Rewrite history\n SYNOPSIS\n --------\n [synopsis]\n+git history drop <commit> [--dry-run] [--update-refs=(branches|head)] [--empty=(drop|keep|abort)]\n git history fixup <commit> [--dry-run] [--update-refs=(branches|head)] [--reedit-message] [--empty=(drop|keep|abort)]\n git history reword <commit> [--dry-run] [--update-refs=(branches|head)]\n git history split <commit> [--dry-run] [--update-refs=(branches|head)] [--] [<pathspec>...]\n@@ -51,13 +52,28 @@ be stateful operations. The limitation can be lifted once (if) Git learns about\n first-class conflicts.\n \n When using `fixup` with `--empty=drop`, dropping the root commit is not yet\n-supported.\n+supported. Likewise, `drop` cannot remove the root commit or a merge commit.\n \n COMMANDS\n --------\n \n The following commands are available to rewrite history in different ways:\n \n+`drop <commit>`::\n+\tRemove the specified commit from the history. All descendants of the\n+\tcommit are replayed directly onto its parent.\n++\n+The root commit cannot be dropped as that may lead to edge cases where refs\n+end up with no commits anymore. Merge commits cannot be dropped either; see\n+LIMITATIONS.\n++\n+If `HEAD` points at a commit that is to be rewritten, the index and working\n+tree are updated to match the new `HEAD`. The command aborts before any\n+references are updated in case local modifications would be overwritten.\n++\n+If replaying any descendant would result in a conflict, the command aborts\n+with an error.\n+\n `fixup <commit>`::\n \tApply the currently staged changes to the specified commit. This is\n \tsimilar in nature to `git commit --fixup=<commit>` followed by `git\n@@ -170,6 +186,26 @@ The staged addition of `unrelated.txt` has been incorporated into the `first`\n commit. All descendant commits have been replayed on top of the rewritten\n history.\n \n+Drop a commit\n+~~~~~~~~~~~~~\n+\n+----------\n+$ git log --oneline\n+abc1234 (HEAD -> main) third\n+def5678 second\n+ghi9012 first\n+\n+$ git history drop def5678\n+\n+$ git log --oneline\n+jkl3456 (HEAD -> main) third\n+ghi9012 first\n+----------\n+\n+The `second` commit has been removed from the history, and `third` has been\n+replayed directly on top of `first`. All branches that pointed at the dropped\n+commit have been moved to its parent.\n+\n Split a commit\n ~~~~~~~~~~~~~~\n \ndiff --git a/builtin/history.c b/builtin/history.c\nindex 4fadf38c32..fa4f5e24ad 100644\n--- a/builtin/history.c\n+++ b/builtin/history.c\n@@ -17,13 +17,17 @@\n #include \"read-cache.h\"\n #include \"refs.h\"\n #include \"replay.h\"\n+#include \"reset.h\"\n #include \"revision.h\"\n #include \"sequencer.h\"\n #include \"strvec.h\"\n #include \"tree.h\"\n+#include \"tree-walk.h\"\n #include \"unpack-trees.h\"\n #include \"wt-status.h\"\n \n+#define GIT_HISTORY_DROP_USAGE \\\n+\tN_(\"git history drop <commit> [--dry-run] [--update-refs=(branches|head)] [--empty=(drop|keep|abort)]\")\n #define GIT_HISTORY_FIXUP_USAGE \\\n \tN_(\"git history fixup <commit> [--dry-run] [--update-refs=(branches|head)] [--reedit-message] [--empty=(drop|keep|abort)]\")\n #define GIT_HISTORY_REWORD_USAGE \\\n@@ -1001,12 +1005,194 @@ static int cmd_history_split(int argc,\n \treturn ret;\n }\n \n+static int update_worktree(struct repository *repo,\n+\t\t\t   const struct commit *old_head,\n+\t\t\t   const struct commit *new_head,\n+\t\t\t   bool dry_run)\n+{\n+\tstruct reset_head_opts opts = {\n+\t\t.oid_from = &old_head->object.oid,\n+\t\t.oid = &new_head->object.oid,\n+\t\t.flags = RESET_HEAD_SKIP_REF_UPDATES,\n+\t};\n+\tif (dry_run)\n+\t\topts.flags |= RESET_HEAD_DRY_RUN;\n+\treturn reset_head(repo, &opts);\n+}\n+\n+static int find_head_tree_change(struct repository *repo,\n+\t\t\t\t const struct replay_result *result,\n+\t\t\t\t struct commit **old_head,\n+\t\t\t\t struct commit **new_head,\n+\t\t\t\t bool *changed)\n+{\n+\tconst struct replay_ref_update *head_update = NULL;\n+\tstruct commit *old_head_commit, *new_head_commit;\n+\tstruct tree *old_head_tree, *new_head_tree;\n+\tconst char *head_target;\n+\tint head_flags;\n+\n+\t*changed = false;\n+\n+\thead_target = refs_resolve_ref_unsafe(get_main_ref_store(repo),\n+\t\t\t\t\t      \"HEAD\", RESOLVE_REF_NO_RECURSE,\n+\t\t\t\t\t      NULL, &head_flags);\n+\tif (!head_target)\n+\t\treturn error(_(\"cannot look up HEAD\"));\n+\tif (!(head_flags & REF_ISSYMREF))\n+\t\thead_target = \"HEAD\";\n+\n+\tfor (size_t i = 0; i < result->updates_nr; i++) {\n+\t\tif (!strcmp(result->updates[i].refname, head_target)) {\n+\t\t\thead_update = &result->updates[i];\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\n+\tif (!head_update)\n+\t\treturn 0;\n+\n+\told_head_commit = lookup_commit_reference(repo, &head_update->old_oid);\n+\tnew_head_commit = lookup_commit_reference(repo, &head_update->new_oid);\n+\tif (!old_head_commit || !new_head_commit)\n+\t\treturn error(_(\"cannot resolve HEAD commit\"));\n+\n+\told_head_tree = repo_get_commit_tree(repo, old_head_commit);\n+\tnew_head_tree = repo_get_commit_tree(repo, new_head_commit);\n+\tif (!old_head_tree || !new_head_tree)\n+\t\treturn error(_(\"cannot resolve tree for HEAD\"));\n+\n+\tif (oideq(&old_head_tree->object.oid, &new_head_tree->object.oid))\n+\t\treturn 0;\n+\n+\t*old_head = old_head_commit;\n+\t*new_head = new_head_commit;\n+\t*changed = true;\n+\n+\treturn 0;\n+}\n+\n+static int cmd_history_drop(int argc,\n+\t\t\t    const char **argv,\n+\t\t\t    const char *prefix,\n+\t\t\t    struct repository *repo)\n+{\n+\tconst char * const usage[] = {\n+\t\tGIT_HISTORY_DROP_USAGE,\n+\t\tNULL,\n+\t};\n+\tenum replay_empty_commit_action empty = REPLAY_EMPTY_COMMIT_DROP;\n+\tenum ref_action action = REF_ACTION_DEFAULT;\n+\tint dry_run = 0;\n+\tstruct option options[] = {\n+\t\tOPT_CALLBACK_F(0, \"update-refs\", &action, \"(branches|head)\",\n+\t\t\t       N_(\"control which refs should be updated\"),\n+\t\t\t       PARSE_OPT_NONEG, parse_ref_action),\n+\t\tOPT_BOOL('n', \"dry-run\", &dry_run,\n+\t\t\t N_(\"perform a dry-run without updating any refs\")),\n+\t\tOPT_CALLBACK_F(0, \"empty\", &empty, \"(drop|keep|abort)\",\n+\t\t\t       N_(\"how to handle descendants that become empty\"),\n+\t\t\t       PARSE_OPT_NONEG, parse_opt_empty),\n+\t\tOPT_END(),\n+\t};\n+\tstruct strbuf reflog_msg = STRBUF_INIT;\n+\tstruct commit *original, *rewritten;\n+\tstruct rev_info revs = { 0 };\n+\tstruct replay_result result = { 0 };\n+\tstruct commit *old_head, *new_head;\n+\tbool head_moves = false;\n+\tint ret;\n+\n+\targc = parse_options(argc, argv, prefix, options, usage, 0);\n+\tif (argc != 1) {\n+\t\tret = error(_(\"command expects a single revision\"));\n+\t\tgoto out;\n+\t}\n+\trepo_config(repo, git_default_config, NULL);\n+\n+\tif (action == REF_ACTION_DEFAULT)\n+\t\taction = REF_ACTION_BRANCHES;\n+\n+\toriginal = lookup_commit_reference_by_name(argv[0]);\n+\tif (!original) {\n+\t\tret = error(_(\"commit cannot be found: %s\"), argv[0]);\n+\t\tgoto out;\n+\t}\n+\n+\tif (!original->parents) {\n+\t\tret = error(_(\"cannot drop root commit %s: \"\n+\t\t\t      \"it has no parent to replay onto\"),\n+\t\t\t    argv[0]);\n+\t\tgoto out;\n+\t} else if (original->parents->next) {\n+\t\tret = error(_(\"cannot drop merge commit: %s\"), argv[0]);\n+\t\tgoto out;\n+\t}\n+\n+\tret = setup_revwalk(repo, action, original, &revs);\n+\tif (ret)\n+\t\tgoto out;\n+\n+\trewritten = original->parents->item;\n+\n+\tret = compute_pending_ref_updates(&revs, action, original, rewritten,\n+\t\t\t\t\t  empty, &result);\n+\tif (ret) {\n+\t\tret = error(_(\"failed replaying descendants\"));\n+\t\tgoto out;\n+\t}\n+\n+\t/*\n+\t * If HEAD will move as a result of the rewrite then we'll have to\n+\t * merge in the changes into the worktree and index. This merge can of\n+\t * course conflict, which will cause the whole operation to abort.\n+\t *\n+\t * If we had already updated the refs at that point then we'd have an\n+\t * inconsistent repository state. So we first perform a dry-run merge\n+\t * here before updating refs.\n+\t */\n+\tif (!dry_run && !is_bare_repository()) {\n+\t\tret = find_head_tree_change(repo, &result, &old_head,\n+\t\t\t\t\t    &new_head, &head_moves);\n+\t\tif (ret < 0)\n+\t\t\tgoto out;\n+\n+\t\tif (head_moves && update_worktree(repo, old_head, new_head, true) < 0) {\n+\t\t\tret = error(_(\"dropping this commit would \"\n+\t\t\t\t      \"overwrite local changes; aborting\"));\n+\t\t\tgoto out;\n+\t\t}\n+\t}\n+\n+\tstrbuf_addf(&reflog_msg, \"drop: dropping %s\", argv[0]);\n+\tret = apply_pending_ref_updates(repo, &result, reflog_msg.buf, dry_run);\n+\tif (ret < 0) {\n+\t\tret = error(_(\"failed to update references\"));\n+\t\tgoto out;\n+\t}\n+\n+\tif (head_moves && update_worktree(repo, old_head, new_head, false) < 0) {\n+\t\tret = error(_(\"could not update working tree to new commit %s\"),\n+\t\t\t    oid_to_hex(&new_head->object.oid));\n+\t\tgoto out;\n+\t}\n+\n+\tret = 0;\n+\n+out:\n+\treplay_result_release(&result);\n+\tstrbuf_release(&reflog_msg);\n+\trelease_revisions(&revs);\n+\treturn ret;\n+}\n+\n int cmd_history(int argc,\n \t\tconst char **argv,\n \t\tconst char *prefix,\n \t\tstruct repository *repo)\n {\n \tconst char * const usage[] = {\n+\t\tGIT_HISTORY_DROP_USAGE,\n \t\tGIT_HISTORY_FIXUP_USAGE,\n \t\tGIT_HISTORY_REWORD_USAGE,\n \t\tGIT_HISTORY_SPLIT_USAGE,\n@@ -1014,6 +1200,7 @@ int cmd_history(int argc,\n \t};\n \tparse_opt_subcommand_fn *fn = NULL;\n \tstruct option options[] = {\n+\t\tOPT_SUBCOMMAND(\"drop\", &fn, cmd_history_drop),\n \t\tOPT_SUBCOMMAND(\"fixup\", &fn, cmd_history_fixup),\n \t\tOPT_SUBCOMMAND(\"reword\", &fn, cmd_history_reword),\n \t\tOPT_SUBCOMMAND(\"split\", &fn, cmd_history_split),\ndiff --git a/t/meson.build b/t/meson.build\nindex 2af8d01279..d5e71056b2 100644\n--- a/t/meson.build\n+++ b/t/meson.build\n@@ -399,6 +399,7 @@ integration_tests = [\n   't3451-history-reword.sh',\n   't3452-history-split.sh',\n   't3453-history-fixup.sh',\n+  't3454-history-drop.sh',\n   't3500-cherry.sh',\n   't3501-revert-cherry-pick.sh',\n   't3502-cherry-pick-merge.sh',\ndiff --git a/t/t3454-history-drop.sh b/t/t3454-history-drop.sh\nnew file mode 100755\nindex 0000000000..37d8413e7e\n--- /dev/null\n+++ b/t/t3454-history-drop.sh\n@@ -0,0 +1,513 @@\n+#!/bin/sh\n+\n+test_description='tests for git-history drop subcommand'\n+\n+. ./test-lib.sh\n+. \"$TEST_DIRECTORY/lib-log-graph.sh\"\n+\n+expect_graph () {\n+\tcat >expect &&\n+\tlib_test_cmp_graph --format=%s \"$@\"\n+}\n+\n+expect_log () {\n+\tgit log --format=\"%s\" \"$@\" >actual &&\n+\tcat >expect &&\n+\ttest_cmp expect actual\n+}\n+\n+test_expect_success 'errors on missing commit argument' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit initial &&\n+\t\ttest_must_fail git history drop 2>err &&\n+\t\ttest_grep \"command expects a single revision\" err\n+\t)\n+'\n+\n+test_expect_success 'errors on too many arguments' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit initial &&\n+\t\ttest_must_fail git history drop HEAD HEAD 2>err &&\n+\t\ttest_grep \"command expects a single revision\" err\n+\t)\n+'\n+\n+test_expect_success 'errors on unknown revision' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit initial &&\n+\t\ttest_must_fail git history drop does-not-exist 2>err &&\n+\t\ttest_grep \"commit cannot be found: does-not-exist\" err\n+\t)\n+'\n+\n+test_expect_success 'errors with invalid --empty= value' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\ttest_commit -C repo initial &&\n+\ttest_commit -C repo second &&\n+\ttest_must_fail git -C repo history drop --empty=bogus HEAD 2>err &&\n+\ttest_grep \"unrecognized.*--empty.*bogus\" err\n+'\n+\n+test_expect_success 'drops a commit in the middle and replays descendants' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\n+\t\tgit symbolic-ref HEAD >expect &&\n+\t\tgit history drop HEAD~ &&\n+\t\tgit symbolic-ref HEAD >actual &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\texpect_log <<-\\EOF &&\n+\t\tthird\n+\t\tfirst\n+\t\tEOF\n+\n+\t\ttest_must_fail git show HEAD:second.t &&\n+\t\ttest_path_is_missing second.t &&\n+\n+\t\tgit reflog >reflog &&\n+\t\ttest_grep \"drop: dropping HEAD~\" reflog\n+\t)\n+'\n+\n+test_expect_success 'drops the HEAD commit' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\n+\t\tgit history drop HEAD &&\n+\n+\t\texpect_log <<-\\EOF\n+\t\tfirst\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'drops a commit on detached HEAD' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\t\tgit checkout --detach HEAD &&\n+\n+\t\tgit history drop HEAD~ &&\n+\n+\t\texpect_log <<-\\EOF\n+\t\tthird\n+\t\tfirst\n+\t\tEOF\n+\t)\n+'\n+\n+# Note: in this case it would actually be fine to drop the root commit, as we\n+# do have a descendant commit, and no reference points to the root commit\n+# directly. So this is something that we may relax eventually.\n+test_expect_success 'refuses to drop the root commit' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\n+\t\ttest_must_fail git history drop HEAD~ 2>err &&\n+\t\ttest_grep \"cannot drop root commit\" err\n+\t)\n+'\n+\n+# In contrast to the above case, we actually don't want to drop the root commit\n+# here as that would cause us to end up with an empty commit graph.\n+test_expect_success 'refuses to drop the root commit when branch becomes empty' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\n+\t\ttest_must_fail git history drop HEAD 2>err &&\n+\t\ttest_grep \"cannot drop root commit\" err\n+\t)\n+'\n+\n+test_expect_success 'refuses to drop a merge commit' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\tgit branch branch &&\n+\t\ttest_commit ours &&\n+\t\tgit switch branch &&\n+\t\ttest_commit theirs &&\n+\t\tgit switch - &&\n+\t\tgit merge theirs &&\n+\n+\t\ttest_must_fail git history drop HEAD 2>err &&\n+\t\ttest_grep \"cannot drop merge commit\" err\n+\t)\n+'\n+\n+test_expect_success 'refuses when descendants contain a merge commit' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\ttest_commit middle &&\n+\t\tgit branch branch &&\n+\t\ttest_commit ours &&\n+\t\tgit switch branch &&\n+\t\ttest_commit theirs &&\n+\t\tgit switch - &&\n+\t\tgit merge theirs &&\n+\n+\t\ttest_must_fail git history drop middle 2>err &&\n+\t\ttest_grep \"replaying merge commits is not supported yet\" err\n+\t)\n+'\n+\n+test_expect_success 'works in a bare repository' '\n+\ttest_when_finished \"rm -rf repo repo.git\" &&\n+\n+\tgit init repo &&\n+\ttest_commit -C repo first &&\n+\ttest_commit -C repo second &&\n+\ttest_commit -C repo third &&\n+\n+\tgit clone --bare repo repo.git &&\n+\t(\n+\t\tcd repo.git &&\n+\n+\t\tgit history drop HEAD~ &&\n+\t\texpect_log <<-\\EOF\n+\t\tthird\n+\t\tfirst\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'updates branches on other lines of descent' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\ttest_commit target &&\n+\t\tgit branch theirs &&\n+\t\ttest_commit ours &&\n+\t\tgit switch theirs &&\n+\t\ttest_commit theirs &&\n+\n+\t\texpect_graph --branches <<-\\EOF &&\n+\t\t* theirs\n+\t\t| * ours\n+\t\t|/\n+\t\t* target\n+\t\t* base\n+\t\tEOF\n+\n+\t\tgit history drop target &&\n+\n+\t\texpect_graph --branches <<-\\EOF\n+\t\t* ours\n+\t\t| * theirs\n+\t\t|/\n+\t\t* base\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'moves branch pointing at dropped commit to its parent' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo --initial-branch=main &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\tgit branch points-at-second &&\n+\t\ttest_commit third &&\n+\n+\t\tgit rev-parse first >expect &&\n+\t\tgit history drop second &&\n+\t\tgit rev-parse points-at-second >actual &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\texpect_log --format=\"%s %D\" --branches <<-\\EOF\n+\t\tthird HEAD -> main\n+\t\tfirst tag: first, points-at-second\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--dry-run prints ref updates without modifying repo' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo --initial-branch=main &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\tgit branch branch &&\n+\t\ttest_commit middle &&\n+\t\ttest_commit ours &&\n+\t\tgit switch branch &&\n+\t\ttest_commit theirs &&\n+\n+\t\tgit refs list >refs-expect &&\n+\t\tgit history drop --dry-run main~ >updates &&\n+\t\tgit refs list >refs-actual &&\n+\t\ttest_cmp refs-expect refs-actual &&\n+\t\ttest_grep \"update refs/heads/main\" updates &&\n+\n+\t\tgit update-ref --stdin <updates &&\n+\t\texpect_log main <<-\\EOF\n+\t\tours\n+\t\tbase\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--update-refs=head updates only HEAD' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo --initial-branch=main &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\ttest_commit target &&\n+\t\tgit branch theirs &&\n+\t\ttest_commit ours &&\n+\t\tgit switch theirs &&\n+\t\ttest_commit theirs &&\n+\n+\t\t# When told to update HEAD only, the command refuses to\n+\t\t# rewrite commits that are not an ancestor of HEAD.\n+\t\ttest_must_fail git history drop --update-refs=head main 2>err &&\n+\t\ttest_grep \"rewritten commit must be an ancestor of HEAD\" err &&\n+\n+\t\texpect_graph --branches <<-\\EOF &&\n+\t\t* theirs\n+\t\t| * ours\n+\t\t|/\n+\t\t* target\n+\t\t* base\n+\t\tEOF\n+\n+\t\tgit switch main &&\n+\t\tgit history drop --update-refs=head target &&\n+\n+\t\texpect_graph --branches <<-\\EOF\n+\t\t* ours\n+\t\t| * theirs\n+\t\t| * target\n+\t\t|/\n+\t\t* base\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'conflict with replayed commit aborts cleanly' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\ttest_commit conflict-a file &&\n+\t\ttest_commit conflict-b file &&\n+\n+\t\tgit refs list >refs-expect &&\n+\t\ttest_must_fail git history drop HEAD~ 2>err &&\n+\t\ttest_grep \"failed replaying descendants\" err &&\n+\t\tgit refs list >refs-actual &&\n+\t\ttest_cmp refs-expect refs-actual\n+\t)\n+'\n+\n+# Build a history where a descendant of the drop target reverts the change\n+# introduced by the drop target. After dropping, the descendant's diff applies\n+# against a tree that already lacks the change, so it becomes empty.\n+setup_empty_descendant_repo () {\n+\tgit init \"$1\" &&\n+\t(\n+\t\tcd \"$1\" &&\n+\t\techo C1 >file &&\n+\t\tgit add file &&\n+\t\tgit commit -m \"base\" &&\n+\t\tgit tag base &&\n+\t\techo C2 >file &&\n+\t\tgit add file &&\n+\t\tgit commit -m \"drop-me\" &&\n+\t\tgit tag drop-me &&\n+\t\ttest_commit middle &&\n+\t\techo C1 >file &&\n+\t\tgit add file &&\n+\t\tgit commit -m \"revert-drop-me\" &&\n+\t\tgit tag revert-drop-me\n+\t)\n+}\n+\n+test_expect_success '--empty=drop drops descendants that become empty' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tsetup_empty_descendant_repo repo &&\n+\t(\n+\t\tcd repo &&\n+\n+\t\tgit history drop --empty=drop drop-me &&\n+\n+\t\texpect_log <<-\\EOF\n+\t\tmiddle\n+\t\tbase\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--empty=keep keeps descendants that become empty' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tsetup_empty_descendant_repo repo &&\n+\t(\n+\t\tcd repo &&\n+\n+\t\tgit history drop --empty=keep drop-me &&\n+\n+\t\texpect_log <<-\\EOF &&\n+\t\trevert-drop-me\n+\t\tmiddle\n+\t\tbase\n+\t\tEOF\n+\t\tgit diff HEAD~ HEAD >diff &&\n+\t\ttest_must_be_empty diff\n+\t)\n+'\n+\n+test_expect_success '--empty=abort errors out when a descendant becomes empty' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tsetup_empty_descendant_repo repo &&\n+\t(\n+\t\tcd repo &&\n+\n+\t\ttest_must_fail git history drop --empty=abort drop-me 2>err &&\n+\t\ttest_grep \"became empty after replay\" err\n+\t)\n+'\n+\n+test_expect_success 'updates index and worktree when HEAD moves' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\n+\t\tgit history drop second &&\n+\n+\t\t# Worktree should no longer contain second.t.\n+\t\ttest_path_is_missing second.t &&\n+\t\ttest_path_is_file first.t &&\n+\t\ttest_path_is_file third.t &&\n+\n+\t\t# Index and worktree should both match the new HEAD.\n+\t\tgit status --porcelain --untracked-files=no >status &&\n+\t\ttest_must_be_empty status\n+\t)\n+'\n+\n+test_expect_success 'updates worktree when dropping HEAD itself' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\n+\t\tgit history drop HEAD &&\n+\n+\t\ttest_path_is_missing second.t &&\n+\t\ttest_path_is_file first.t &&\n+\n+\t\tgit status --porcelain --untracked-files=no >status &&\n+\t\ttest_must_be_empty status\n+\t)\n+'\n+\n+test_expect_success 'preserves unrelated unstaged modifications' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\techo first-content >unrelated.txt &&\n+\t\tgit add unrelated.txt &&\n+\t\tgit commit -m \"add unrelated\" &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\n+\t\techo locally-modified >unrelated.txt &&\n+\n+\t\tgit diff >diff-expect &&\n+\t\tgit history drop second &&\n+\t\tgit diff >diff-actual &&\n+\t\ttest_cmp diff-expect diff-actual &&\n+\t\ttest_path_is_missing second.t\n+\t)\n+'\n+\n+test_expect_success 'preserves unrelated staged changes' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\techo first-content >unrelated.txt &&\n+\t\tgit add unrelated.txt &&\n+\t\tgit commit -m \"add unrelated\" &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\n+\t\techo staged-change >unrelated.txt &&\n+\t\tgit add unrelated.txt &&\n+\n+\t\tgit diff --cached >diff-expect &&\n+\t\tgit history drop second &&\n+\t\tgit diff --cached >diff-actual &&\n+\t\ttest_cmp diff-expect diff-actual &&\n+\t\ttest_path_is_missing second.t\n+\t)\n+'\n+\n+test_expect_success 'aborts when local modifications would be overwritten' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\ttest_commit conflict &&\n+\n+\t\techo local-edit >conflict.t &&\n+\t\tgit diff >diff-expect &&\n+\t\ttest_must_fail git history drop HEAD 2>err &&\n+\t\ttest_grep \"would overwrite local changes\" err &&\n+\t\tgit diff >diff-actual &&\n+\t\ttest_cmp diff-expect diff-actual\n+\t)\n+'\n+\n+test_done\n\n-- \n2.54.0.1064.gd145956f57.dirty\n\n"},{"id":"544636","messageId":"28af58ac-4299-4510-937d-8326b71422d8@app.fastmail.com","threadId":"65726","inReplyTo":"20260603-b4-pks-history-drop-v2-3-742cb5b5176d@pks.im","subject":"Re: [PATCH v2 3/9] reset: modernize flags passed to `reset_head()`","fromName":"Kristoffer Haugsbakk","fromEmail":"kristofferhaugsbakk@fastmail.com","sentAt":"2026-06-03T18:01:04Z","receivedAt":"2026-06-03T18:02:05Z","isPatch":true,"body":"On Wed, Jun 3, 2026, at 18:14, Patrick Steinhardt wrote:\n> The flags passed to `reset_head()` are declared as defines. This has\n> fallen a bit out of practice nowadays, where we instead prefer to use\n> enums.\n>\n> Modernize the code accordingly.\n>\n> Signed-off-by: Patrick Steinhardt <ps@pks.im>\n> ---\n\nThis seems to refer to this from CodingGuidelines (quoting\nfor reference):\n\n    When a function `F` accepts flags, those flags should be\n\nSo this goal makes sense.\n\n>[snip]\n"},{"id":"544640","messageId":"d5af942c-1b94-4c22-97e7-b83ee0b530b6@app.fastmail.com","threadId":"65726","inReplyTo":"20260603-b4-pks-history-drop-v2-4-742cb5b5176d@pks.im","subject":"Re: [PATCH v2 4/9] reset: introduce dry-run mode","fromName":"Kristoffer Haugsbakk","fromEmail":"kristofferhaugsbakk@fastmail.com","sentAt":"2026-06-03T18:18:22Z","receivedAt":"2026-06-03T18:18:43Z","isPatch":true,"body":"On Wed, Jun 3, 2026, at 18:14, Patrick Steinhardt wrote:\n> In a subsequent commit we'll add add another caller to `reset_head()`\n\ns/add add/add/\n\n> that wants to perform a dry-run check of whether it would be possible to\n> udpate the index and working tree when moving to a new commit. Introduce\n\ns/udpate/update/\n\n> a new flag that lets the caller perform this operation.\n>\n> Signed-off-by: Patrick Steinhardt <ps@pks.im>\n> ---\n>[snip]\n"},{"id":"544641","messageId":"4b4672de-17cc-426f-8498-6384b1ad0d06@app.fastmail.com","threadId":"65726","inReplyTo":"20260603-b4-pks-history-drop-v2-9-742cb5b5176d@pks.im","subject":"Re: [PATCH v2 9/9] builtin/history: implement \"drop\" subcommand","fromName":"Kristoffer Haugsbakk","fromEmail":"kristofferhaugsbakk@fastmail.com","sentAt":"2026-06-03T19:04:33Z","receivedAt":"2026-06-03T19:04:55Z","isPatch":true,"body":"On Wed, Jun 3, 2026, at 18:14, Patrick Steinhardt wrote:\n>[snip]\n> ---\n>  Documentation/git-history.adoc |  38 ++-\n>  builtin/history.c              | 187 +++++++++++++++\n>  t/meson.build                  |   1 +\n>  t/t3454-history-drop.sh        | 513 +++++++++++++++++++++++++++++++++++++++++\n>  4 files changed, 738 insertions(+), 1 deletion(-)\n>\n> diff --git a/Documentation/git-history.adoc\n> b/Documentation/git-history.adoc\n> index 2ba8121795..4eac732fd2 100644\n> --- a/Documentation/git-history.adoc\n> +++ b/Documentation/git-history.adoc\n> @@ -8,6 +8,7 @@ git-history - EXPERIMENTAL: Rewrite history\n>  SYNOPSIS\n>  --------\n>  [synopsis]\n> +git history drop <commit> [--dry-run] [--update-refs=(branches|head)]\n> [--empty=(drop|keep|abort)]\n>  git history fixup <commit> [--dry-run] [--update-refs=(branches|head)]\n> [--reedit-message] [--empty=(drop|keep|abort)]\n>  git history reword <commit> [--dry-run] [--update-refs=(branches|head)]\n>  git history split <commit> [--dry-run] [--update-refs=(branches|head)]\n> [--] [<pathspec>...]\n> @@ -51,13 +52,28 @@ be stateful operations. The limitation can be\n> lifted once (if) Git learns about\n>  first-class conflicts.\n>\n>  When using `fixup` with `--empty=drop`, dropping the root commit is not yet\n> -supported.\n> +supported. Likewise, `drop` cannot remove the root commit or a merge commit.\n>\n>  COMMANDS\n>  --------\n>\n>  The following commands are available to rewrite history in different ways:\n>\n> +`drop <commit>`::\n> +\tRemove the specified commit from the history. All descendants of the\n> +\tcommit are replayed directly onto its parent.\n> ++\n> +The root commit cannot be dropped as that may lead to edge cases where refs\n> +end up with no commits anymore. Merge commits cannot be dropped either; see\n> +LIMITATIONS.\n\nShould section names be “bare” or quoted like \"LIMITATIONS\"?\nI don’t know.\n\nMaybe add “above” since it’s a previous section.\n\n> ++\n> +If `HEAD` points at a commit that is to be rewritten, the index and working\n>[snip]\n> +Drop a commit\n> +~~~~~~~~~~~~~\n> +\n> +----------\n> +$ git log --oneline\n> +abc1234 (HEAD -> main) third\n> +def5678 second\n> +ghi9012 first\n> +\n> +$ git history drop def5678\n\nI know this is only the most simple example. And I might be dragging in\nsomething beyond the scope of this example. But I recall one\ndemonstration on the first git-history(1) series which used a lot of\nrevision expressions and someone saying that they couldn’t imagine a\nworkflow where this would be more interactive than bringing up the\ngit-rebase(1) todo editor.\n\n(I couldn’t find back to this right now.)\n\nAlthough it is slower in terms of machine cycles, the keyboard instinct\nfor dropping a nearby commit might be to do `git rebase -i @~10`\n(sufficiently high number) and navigating quickly in the configured\neditor, deleting the line or using the keybind for `drop`. This example\nwhich by implication brings up the log in order to paste the abbreviated\nhash isn’t as ergonomic in comparison.\n\nBut using a revision expression like searching the subject with\n`main^{/second}`, while not quicker probably, does distinguish itself\nfrom git-rebase(1) by being a pretty fast ad hoc invocation that can be\ndone in one command without futzing with some weird sed(1) editor in\norder to navigate to the `second` line and deleting it, or\nsomething. And that’s a small win in isolation, but it segues much more\nnaturally into letting you script, say, dropping the last commit that\nstarts with the subject `TEMP`.\n\nOr maybe revision expressions is too much in this context?\n\n> +\n> +$ git log --oneline\n>[snip]\n> diff --git a/t/t3454-history-drop.sh b/t/t3454-history-drop.sh\n> new file mode 100755\n> index 0000000000..37d8413e7e\n> --- /dev/null\n> +++ b/t/t3454-history-drop.sh\n> @@ -0,0 +1,513 @@\n> +#!/bin/sh\n> +\n> +test_description='tests for git-history drop subcommand'\n> +\n> +. ./test-lib.sh\n> +. \"$TEST_DIRECTORY/lib-log-graph.sh\"\n> +\n> +expect_graph () {\n> +\tcat >expect &&\n> +\tlib_test_cmp_graph --format=%s \"$@\"\n> +}\n> +\n> +expect_log () {\n> +\tgit log --format=\"%s\" \"$@\" >actual &&\n> +\tcat >expect &&\n> +\ttest_cmp expect actual\n> +}\n> +\n> +test_expect_success 'errors on missing commit argument' '\n> +\ttest_when_finished \"rm -rf repo\" &&\n> +\tgit init repo &&\n> +\t(\n> +\t\tcd repo &&\n> +\t\ttest_commit initial &&\n> +\t\ttest_must_fail git history drop 2>err &&\n> +\t\ttest_grep \"command expects a single revision\" err\n\nWhy not `test_cmp` since it’s a fixed error?\n\nSame for a few other tests like `errors on unknown revision`.\n\n> +\t)\n> +'\n>[snip]\n> +test_expect_success 'errors with invalid --empty= value' '\n> +\ttest_when_finished \"rm -rf repo\" &&\n> +\tgit init repo &&\n> +\ttest_commit -C repo initial &&\n> +\ttest_commit -C repo second &&\n> +\ttest_must_fail git -C repo history drop --empty=bogus HEAD 2>err &&\n> +\ttest_grep \"unrecognized.*--empty.*bogus\" err\n> +'\n\nStyle related I guess. Most tests here use a subshell but this one uses\n`git -C`? Why is that?\n\n>[snip]\n> +test_expect_success 'updates branches on other lines of descent' '\n> +\ttest_when_finished \"rm -rf repo\" &&\n> +\tgit init repo &&\n> +\t(\n> +\t\tcd repo &&\n> +\t\ttest_commit base &&\n> +\t\ttest_commit target &&\n> +\t\tgit branch theirs &&\n> +\t\ttest_commit ours &&\n> +\t\tgit switch theirs &&\n> +\t\ttest_commit theirs &&\n> +\n> +\t\texpect_graph --branches <<-\\EOF &&\n> +\t\t* theirs\n> +\t\t| * ours\n> +\t\t|/\n> +\t\t* target\n> +\t\t* base\n> +\t\tEOF\n\nOh, `expect_graph` is a cool tool.\n\n> +\n> +\t\tgit history drop target &&\n> +\n> +\t\texpect_graph --branches <<-\\EOF\n> +\t\t* ours\n> +\t\t| * theirs\n> +\t\t|/\n> +\t\t* base\n> +\t\tEOF\n> +\t)\n> +'\n>[snip]\n"},{"id":"544647","messageId":"xmqqv7bzqj5d.fsf@gitster.g","threadId":"65726","inReplyTo":"20260603-b4-pks-history-drop-v2-4-742cb5b5176d@pks.im","subject":"Re: [PATCH v2 4/9] reset: introduce dry-run mode","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-06-03T23:49:34Z","receivedAt":"2026-06-03T23:49:37Z","isPatch":true,"body":"Patrick Steinhardt <ps@pks.im> writes:\n\n> In a subsequent commit we'll add add another caller to `reset_head()`\n\nadd add?\n\n> that wants to perform a dry-run check of whether it would be possible to\n> udpate the index and working tree when moving to a new commit. Introduce\n\nudpate?\n\n> a new flag that lets the caller perform this operation.\n>\n> Signed-off-by: Patrick Steinhardt <ps@pks.im>\n> ---\n>  reset.c | 44 +++++++++++++++++++++++++++++++++-----------\n>  reset.h |  6 ++++++\n>  2 files changed, 39 insertions(+), 11 deletions(-)\n>\n> diff --git a/reset.c b/reset.c\n> index 9ff14f5ed1..a8d7eea4d6 100644\n> --- a/reset.c\n> +++ b/reset.c\n> @@ -92,11 +92,14 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n>  \tunsigned reset_hard = opts->flags & RESET_HEAD_HARD;\n>  \tunsigned refs_only = opts->flags & RESET_HEAD_REFS_ONLY;\n>  \tunsigned update_orig_head = opts->flags & RESET_HEAD_ORIG_HEAD;\n> +\tunsigned dry_run = opts->flags & RESET_HEAD_DRY_RUN;\n>  \tstruct object_id *head = NULL, head_oid;\n>  \tstruct tree_desc desc[2] = { { NULL }, { NULL } };\n>  \tstruct lock_file lock = LOCK_INIT;\n>  \tstruct unpack_trees_options unpack_tree_opts = { 0 };\n>  \tstruct tree *tree;\n> +\tstruct index_state scratch_index = INDEX_STATE_INIT(r);\n> +\tstruct index_state *istate;\n>  \tconst char *action;\n>  \tint ret = 0, nr = 0;\n>  \n> @@ -109,7 +112,7 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n>  \tif (opts->branch_msg && !opts->branch)\n>  \t\tBUG(\"branch reflog message given without a branch\");\n>  \n> -\tif (!refs_only && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {\n> +\tif (!refs_only && !dry_run && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {\n>  \t\tret = -1;\n>  \t\tgoto leave_reset_head;\n>  \t}\n> @@ -124,16 +127,36 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n>  \tif (!oid)\n>  \t\toid = &head_oid;\n>  \n> -\tif (refs_only)\n> -\t\treturn update_refs(r, opts, oid, head);\n> +\tif (refs_only) {\n> +\t\tif (!dry_run)\n> +\t\t\treturn update_refs(r, opts, oid, head);\n> +\t\treturn 0;\n> +\t}\n> +\n> +\tif (dry_run) {\n> +\t\tif (read_index_from(&scratch_index, r->index_file, r->gitdir) < 0 ||\n> +\t\t    index_state_unmerged_to_stage0(&scratch_index) < 0) {\n> +\t\t\tret = error(_(\"could not read index\"));\n> +\t\t\tgoto leave_reset_head;\n> +\t\t}\n> +\n> +\t\tistate = &scratch_index;\n> +\t} else {\n> +\t\tif (repo_read_index_unmerged(r) < 0) {\n> +\t\t\tret = error(_(\"could not read index\"));\n> +\t\t\tgoto leave_reset_head;\n> +\t\t}\n> +\t\tistate = r->index;\n> +\t}\n>  \n>  \taction = reset_hard ? \"reset\" : \"checkout\";\n>  \tsetup_unpack_trees_porcelain(&unpack_tree_opts, action);\n>  \tunpack_tree_opts.head_idx = 1;\n> -\tunpack_tree_opts.src_index = r->index;\n> -\tunpack_tree_opts.dst_index = r->index;\n> +\tunpack_tree_opts.src_index = istate;\n> +\tunpack_tree_opts.dst_index = istate;\n>  \tunpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge;\n> -\tunpack_tree_opts.update = 1;\n> +\tunpack_tree_opts.update = !dry_run;\n> +\tunpack_tree_opts.dry_run = dry_run;\n>  \tunpack_tree_opts.merge = 1;\n>  \tunpack_tree_opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */\n>  \tunpack_tree_opts.skip_cache_tree_update = 1;\n> @@ -141,11 +164,6 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n>  \tif (reset_hard)\n>  \t\tunpack_tree_opts.reset = UNPACK_RESET_PROTECT_UNTRACKED;\n>  \n> -\tif (repo_read_index_unmerged(r) < 0) {\n> -\t\tret = error(_(\"could not read index\"));\n> -\t\tgoto leave_reset_head;\n> -\t}\n> -\n>  \tif (!reset_hard && !fill_tree_descriptor(r, &desc[nr++], &head_oid)) {\n>  \t\tret = error(_(\"failed to find tree of %s\"),\n>  \t\t\t    oid_to_hex(&head_oid));\n> @@ -162,6 +180,9 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n>  \t\tgoto leave_reset_head;\n>  \t}\n>  \n> +\tif (dry_run)\n> +\t\tgoto leave_reset_head;\n> +\n>  \ttree = repo_parse_tree_indirect(r, oid);\n>  \tif (!tree) {\n>  \t\tret = error(_(\"unable to read tree (%s)\"), oid_to_hex(oid));\n> @@ -181,6 +202,7 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n>  leave_reset_head:\n>  \trollback_lock_file(&lock);\n>  \tclear_unpack_trees_porcelain(&unpack_tree_opts);\n> +\trelease_index(&scratch_index);\n>  \twhile (nr)\n>  \t\tfree((void *)desc[--nr].buffer);\n>  \treturn ret;\n> diff --git a/reset.h b/reset.h\n> index 97ced2601e..9f696382c1 100644\n> --- a/reset.h\n> +++ b/reset.h\n> @@ -21,6 +21,12 @@ enum reset_head_flags {\n>  \n>  \t/* Update ORIG_HEAD as well as HEAD */\n>  \tRESET_HEAD_ORIG_HEAD = (1 << 4),\n> +\n> +\t/*\n> +\t * Perform a dry-run by performing the operation without updating\n> +\t * any user-visible state.\n> +\t */\n> +\tRESET_HEAD_DRY_RUN = (1 << 5),\n>  };\n>  \n>  struct reset_head_opts {\n"},{"id":"544648","messageId":"xmqqqzmnqj1o.fsf@gitster.g","threadId":"65726","inReplyTo":"20260603-b4-pks-history-drop-v2-5-742cb5b5176d@pks.im","subject":"Re: [PATCH v2 5/9] reset: introduce ability to skip reference updates","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-06-03T23:51:47Z","receivedAt":"2026-06-03T23:51:49Z","isPatch":true,"body":"Patrick Steinhardt <ps@pks.im> writes:\n\n> @@ -112,6 +113,9 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n>  \tif (opts->branch_msg && !opts->branch)\n>  \t\tBUG(\"branch reflog message given without a branch\");\n>  \n> +\tif (skip_ref_updates && (opts->branch || refs_only))\n> +\t\tBUG(\"asked to perform ref updates and skip them at the same time\");\n\n;-)  That's certainly a careful safety valve.\n\nWould we also want to catch skip_ref_updates && update_orig_head\nbeing both set as a bogus request?\n\n>  \tif (!refs_only && !dry_run && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {\n>  \t\tret = -1;\n>  \t\tgoto leave_reset_head;\n> @@ -196,7 +200,8 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n>  \t\tgoto leave_reset_head;\n>  \t}\n>  \n> -\tif (oid != &head_oid || update_orig_head || switch_to_branch)\n> +\tif (!skip_ref_updates &&\n> +\t    (oid != &head_oid || update_orig_head || switch_to_branch))\n>  \t\tret = update_refs(r, opts, oid, head);\n>  \n>  leave_reset_head:\n> diff --git a/reset.h b/reset.h\n> index 9f696382c1..cb0700ffa7 100644\n> --- a/reset.h\n> +++ b/reset.h\n> @@ -27,6 +27,9 @@ enum reset_head_flags {\n>  \t * any user-visible state.\n>  \t */\n>  \tRESET_HEAD_DRY_RUN = (1 << 5),\n> +\n> +\t/* Skip updating any references, only update the worktree and index. */\n> +\tRESET_HEAD_SKIP_REF_UPDATES = (1 << 6),\n>  };\n>  \n>  struct reset_head_opts {\n"},{"id":"544649","messageId":"xmqqmrxbqir4.fsf@gitster.g","threadId":"65726","inReplyTo":"20260603-b4-pks-history-drop-v2-9-742cb5b5176d@pks.im","subject":"Re: [PATCH v2 9/9] builtin/history: implement \"drop\" subcommand","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-06-03T23:58:07Z","receivedAt":"2026-06-03T23:58:09Z","isPatch":true,"body":"Patrick Steinhardt <ps@pks.im> writes:\n\n> +static int update_worktree(struct repository *repo,\n> +\t\t\t   const struct commit *old_head,\n> +\t\t\t   const struct commit *new_head,\n> +\t\t\t   bool dry_run)\n> +{\n> +\tstruct reset_head_opts opts = {\n> +\t\t.oid_from = &old_head->object.oid,\n> +\t\t.oid = &new_head->object.oid,\n> +\t\t.flags = RESET_HEAD_SKIP_REF_UPDATES,\n> +\t};\n> +\tif (dry_run)\n> +\t\topts.flags |= RESET_HEAD_DRY_RUN;\n> +\treturn reset_head(repo, &opts);\n> +}\n> + ...\n> +\t/*\n> +\t * If HEAD will move as a result of the rewrite then we'll have to\n> +\t * merge in the changes into the worktree and index. This merge can of\n> +\t * course conflict, which will cause the whole operation to abort.\n> +\t *\n> +\t * If we had already updated the refs at that point then we'd have an\n> +\t * inconsistent repository state. So we first perform a dry-run merge\n> +\t * here before updating refs.\n> +\t */\n> +\tif (!dry_run && !is_bare_repository()) {\n> +\t\tret = find_head_tree_change(repo, &result, &old_head,\n> +\t\t\t\t\t    &new_head, &head_moves);\n> +\t\tif (ret < 0)\n> +\t\t\tgoto out;\n> +\n> +\t\tif (head_moves && update_worktree(repo, old_head, new_head, true) < 0) {\n> +\t\t\tret = error(_(\"dropping this commit would \"\n> +\t\t\t\t      \"overwrite local changes; aborting\"));\n> +\t\t\tgoto out;\n> +\t\t}\n> +\t}\n\nThis block is skipped under --dry-run, but update_worktree is\nequipped to (and indeed run unconditionally here) run in the dry-run\nmode.  Does it mean that \"git history drop --dry-run\" that user runs\nto see which refs may be updated will not get warned about possible\nworktree conflicts that would prevent the real run from happening?\nUnless there is a compelling reason not to, I think --dry-run should\nbe a close simulation of what would happen without it.\n\nThanks.\n"},{"id":"544700","messageId":"aiE_Bcup5RTfcQD8@pks.im","threadId":"65726","inReplyTo":"xmqqqzmnqj1o.fsf@gitster.g","subject":"Re: [PATCH v2 5/9] reset: introduce ability to skip reference updates","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-04T09:01:57Z","receivedAt":"2026-06-04T09:02:03Z","isPatch":true,"body":"On Thu, Jun 04, 2026 at 08:51:47AM +0900, Junio C Hamano wrote:\n> Patrick Steinhardt <ps@pks.im> writes:\n> \n> > @@ -112,6 +113,9 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n> >  \tif (opts->branch_msg && !opts->branch)\n> >  \t\tBUG(\"branch reflog message given without a branch\");\n> >  \n> > +\tif (skip_ref_updates && (opts->branch || refs_only))\n> > +\t\tBUG(\"asked to perform ref updates and skip them at the same time\");\n> \n> ;-)  That's certainly a careful safety valve.\n> \n> Would we also want to catch skip_ref_updates && update_orig_head\n> being both set as a bogus request?\n\nYeah, I think that's a good idea.\n\nPatrick\n"},{"id":"544701","messageId":"aiE_D9Dzryfx8iQI@pks.im","threadId":"65726","inReplyTo":"4b4672de-17cc-426f-8498-6384b1ad0d06@app.fastmail.com","subject":"Re: [PATCH v2 9/9] builtin/history: implement \"drop\" subcommand","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-04T09:02:07Z","receivedAt":"2026-06-04T09:02:12Z","isPatch":true,"body":"On Wed, Jun 03, 2026 at 09:04:33PM +0200, Kristoffer Haugsbakk wrote:\n> On Wed, Jun 3, 2026, at 18:14, Patrick Steinhardt wrote:\n> > diff --git a/Documentation/git-history.adoc\n> > b/Documentation/git-history.adoc\n> > index 2ba8121795..4eac732fd2 100644\n> > --- a/Documentation/git-history.adoc\n> > +++ b/Documentation/git-history.adoc\n> > @@ -51,13 +52,28 @@ be stateful operations. The limitation can be\n> > lifted once (if) Git learns about\n> >  first-class conflicts.\n> >\n> >  When using `fixup` with `--empty=drop`, dropping the root commit is not yet\n> > -supported.\n> > +supported. Likewise, `drop` cannot remove the root commit or a merge commit.\n> >\n> >  COMMANDS\n> >  --------\n> >\n> >  The following commands are available to rewrite history in different ways:\n> >\n> > +`drop <commit>`::\n> > +\tRemove the specified commit from the history. All descendants of the\n> > +\tcommit are replayed directly onto its parent.\n> > ++\n> > +The root commit cannot be dropped as that may lead to edge cases where refs\n> > +end up with no commits anymore. Merge commits cannot be dropped either; see\n> > +LIMITATIONS.\n> \n> Should section names be “bare” or quoted like \"LIMITATIONS\"?\n> I don’t know.\n> \n> Maybe add “above” since it’s a previous section.\n\nHm. I think I'd prefer to keep this as-is, as we already have\npreexisting documentation in the same manpage that does it exactly like\nthe above.\n\n> > ++\n> > +If `HEAD` points at a commit that is to be rewritten, the index and working\n> >[snip]\n> > +Drop a commit\n> > +~~~~~~~~~~~~~\n> > +\n> > +----------\n> > +$ git log --oneline\n> > +abc1234 (HEAD -> main) third\n> > +def5678 second\n> > +ghi9012 first\n> > +\n> > +$ git history drop def5678\n> \n> I know this is only the most simple example. And I might be dragging in\n> something beyond the scope of this example. But I recall one\n> demonstration on the first git-history(1) series which used a lot of\n> revision expressions and someone saying that they couldn’t imagine a\n> workflow where this would be more interactive than bringing up the\n> git-rebase(1) todo editor.\n> \n> (I couldn’t find back to this right now.)\n\nYeah, I remember this discussion.\n\n> Although it is slower in terms of machine cycles, the keyboard instinct\n> for dropping a nearby commit might be to do `git rebase -i @~10`\n> (sufficiently high number) and navigating quickly in the configured\n> editor, deleting the line or using the keybind for `drop`. This example\n> which by implication brings up the log in order to paste the abbreviated\n> hash isn’t as ergonomic in comparison.\n> \n> But using a revision expression like searching the subject with\n> `main^{/second}`, while not quicker probably, does distinguish itself\n> from git-rebase(1) by being a pretty fast ad hoc invocation that can be\n> done in one command without futzing with some weird sed(1) editor in\n> order to navigate to the `second` line and deleting it, or\n> something. And that’s a small win in isolation, but it segues much more\n> naturally into letting you script, say, dropping the last commit that\n> starts with the subject `TEMP`.\n> \n> Or maybe revision expressions is too much in this context?\n\nNo, I think that's actually a good idea to demonstrate how this can be\nused without being too unergomonic.\n\nEventually I still have the idea in my mind to implement a TUI around\ngit-history(1) that allows to drive its several subcommands without\nhaving to laboriously select the right commits. Ideally, you'd simply\nselect the commits you care about and then pick specific actions you\nwant to do on them, with a nice visual graph that updates after the\noperation. But that's a bit into the future, I guess :)\n\n> > diff --git a/t/t3454-history-drop.sh b/t/t3454-history-drop.sh\n> > new file mode 100755\n> > index 0000000000..37d8413e7e\n> > --- /dev/null\n> > +++ b/t/t3454-history-drop.sh\n> > @@ -0,0 +1,513 @@\n> > +#!/bin/sh\n> > +\n> > +test_description='tests for git-history drop subcommand'\n> > +\n> > +. ./test-lib.sh\n> > +. \"$TEST_DIRECTORY/lib-log-graph.sh\"\n> > +\n> > +expect_graph () {\n> > +\tcat >expect &&\n> > +\tlib_test_cmp_graph --format=%s \"$@\"\n> > +}\n> > +\n> > +expect_log () {\n> > +\tgit log --format=\"%s\" \"$@\" >actual &&\n> > +\tcat >expect &&\n> > +\ttest_cmp expect actual\n> > +}\n> > +\n> > +test_expect_success 'errors on missing commit argument' '\n> > +\ttest_when_finished \"rm -rf repo\" &&\n> > +\tgit init repo &&\n> > +\t(\n> > +\t\tcd repo &&\n> > +\t\ttest_commit initial &&\n> > +\t\ttest_must_fail git history drop 2>err &&\n> > +\t\ttest_grep \"command expects a single revision\" err\n> \n> Why not `test_cmp` since it’s a fixed error?\n> \n> Same for a few other tests like `errors on unknown revision`.\n\nWe tend to use test_grep for cases like this, even for static error\nmessages. I haven't really figured out myself when exactly we tend to\nuse one form over the other, to be honest.\n\n> >[snip]\n> > +test_expect_success 'errors with invalid --empty= value' '\n> > +\ttest_when_finished \"rm -rf repo\" &&\n> > +\tgit init repo &&\n> > +\ttest_commit -C repo initial &&\n> > +\ttest_commit -C repo second &&\n> > +\ttest_must_fail git -C repo history drop --empty=bogus HEAD 2>err &&\n> > +\ttest_grep \"unrecognized.*--empty.*bogus\" err\n> > +'\n> \n> Style related I guess. Most tests here use a subshell but this one uses\n> `git -C`? Why is that?\n\nNo good reason, will fix.\n\nThanks!\n\nPatrick\n"},{"id":"544702","messageId":"aiE_FtbkLVjXdSVX@pks.im","threadId":"65726","inReplyTo":"xmqqmrxbqir4.fsf@gitster.g","subject":"Re: [PATCH v2 9/9] builtin/history: implement \"drop\" subcommand","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-04T09:02:14Z","receivedAt":"2026-06-04T09:02:18Z","isPatch":true,"body":"On Thu, Jun 04, 2026 at 08:58:07AM +0900, Junio C Hamano wrote:\n> Patrick Steinhardt <ps@pks.im> writes:\n> \n> > +static int update_worktree(struct repository *repo,\n> > +\t\t\t   const struct commit *old_head,\n> > +\t\t\t   const struct commit *new_head,\n> > +\t\t\t   bool dry_run)\n> > +{\n> > +\tstruct reset_head_opts opts = {\n> > +\t\t.oid_from = &old_head->object.oid,\n> > +\t\t.oid = &new_head->object.oid,\n> > +\t\t.flags = RESET_HEAD_SKIP_REF_UPDATES,\n> > +\t};\n> > +\tif (dry_run)\n> > +\t\topts.flags |= RESET_HEAD_DRY_RUN;\n> > +\treturn reset_head(repo, &opts);\n> > +}\n> > + ...\n> > +\t/*\n> > +\t * If HEAD will move as a result of the rewrite then we'll have to\n> > +\t * merge in the changes into the worktree and index. This merge can of\n> > +\t * course conflict, which will cause the whole operation to abort.\n> > +\t *\n> > +\t * If we had already updated the refs at that point then we'd have an\n> > +\t * inconsistent repository state. So we first perform a dry-run merge\n> > +\t * here before updating refs.\n> > +\t */\n> > +\tif (!dry_run && !is_bare_repository()) {\n> > +\t\tret = find_head_tree_change(repo, &result, &old_head,\n> > +\t\t\t\t\t    &new_head, &head_moves);\n> > +\t\tif (ret < 0)\n> > +\t\t\tgoto out;\n> > +\n> > +\t\tif (head_moves && update_worktree(repo, old_head, new_head, true) < 0) {\n> > +\t\t\tret = error(_(\"dropping this commit would \"\n> > +\t\t\t\t      \"overwrite local changes; aborting\"));\n> > +\t\t\tgoto out;\n> > +\t\t}\n> > +\t}\n> \n> This block is skipped under --dry-run, but update_worktree is\n> equipped to (and indeed run unconditionally here) run in the dry-run\n> mode.  Does it mean that \"git history drop --dry-run\" that user runs\n> to see which refs may be updated will not get warned about possible\n> worktree conflicts that would prevent the real run from happening?\n> Unless there is a compelling reason not to, I think --dry-run should\n> be a close simulation of what would happen without it.\n\nThat's an oversight on my part indeed. We _should_ run this block with\ndry-run, and it shows that we're definitely lacking test coverage here.\nWill fix, thanks!\n\nPatrick\n"},{"id":"544794","messageId":"9e2cb34b-97f6-44f4-be44-60f44760e601@gmail.com","threadId":"65726","inReplyTo":"20260603-b4-pks-history-drop-v2-3-742cb5b5176d@pks.im","subject":"Re: [PATCH v2 3/9] reset: modernize flags passed to `reset_head()`","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-06-05T15:08:57Z","receivedAt":"2026-06-05T15:09:14Z","isPatch":true,"body":"Hi Patrick\n\nOn 03/06/2026 17:14, Patrick Steinhardt wrote:\n\n> -/* Update ORIG_HEAD as well as HEAD */\n> -#define RESET_ORIG_HEAD (1<<4)\n > [...]> +\t/* Update ORIG_HEAD as well as HEAD */\n> +\tRESET_HEAD_ORIG_HEAD = (1 << 4),\n\nI'm having a hard time parsing this new name, if we must have a \n\"RESET_HEAD_\" prefix can we call it something like \nRESET_HEAD_UPDATE_ORIG_HEAD?\n\nThanks\n\nPhillip\n\n> +};\n>   \n>   struct reset_head_opts {\n>   \t/*\n> @@ -33,7 +39,7 @@ struct reset_head_opts {\n>   \t/*\n>   \t * Flags defined above.\n>   \t */\n> -\tunsigned flags;\n> +\tenum reset_head_flags flags;\n>   \t/*\n>   \t * Optional reflog message for branch, defaults to head_msg.\n>   \t */\n> @@ -45,7 +51,7 @@ struct reset_head_opts {\n>   \tconst char *head_msg;\n>   \t/*\n>   \t * Optional reflog message for ORIG_HEAD, if this omitted and flags\n> -\t * contains RESET_ORIG_HEAD then default_reflog_action must be given.\n> +\t * contains RESET_HEAD_ORIG_HEAD then default_reflog_action must be given.\n>   \t */\n>   \tconst char *orig_head_msg;\n>   \t/*\n> diff --git a/sequencer.c b/sequencer.c\n> index 1ee4b2875b..0b89a977b0 100644\n> --- a/sequencer.c\n> +++ b/sequencer.c\n> @@ -4870,7 +4870,7 @@ static int checkout_onto(struct repository *r, struct replay_opts *opts,\n>   \tstruct reset_head_opts ropts = {\n>   \t\t.oid = onto,\n>   \t\t.orig_head = orig_head,\n> -\t\t.flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD |\n> +\t\t.flags = RESET_HEAD_DETACH | RESET_HEAD_ORIG_HEAD |\n>   \t\t\t\tRESET_HEAD_RUN_POST_CHECKOUT_HOOK,\n>   \t\t.head_msg = reflog_message(opts, \"start\", \"checkout %s\",\n>   \t\t\t\t\t   onto_name),\n> \n\n"},{"id":"544795","messageId":"0fdaeec8-99cd-4dc9-9549-8a08133deebf@gmail.com","threadId":"65726","inReplyTo":"20260603-b4-pks-history-drop-v2-5-742cb5b5176d@pks.im","subject":"Re: [PATCH v2 5/9] reset: introduce ability to skip reference updates","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-06-05T15:12:42Z","receivedAt":"2026-06-05T15:12:57Z","isPatch":true,"body":"Hi Patrick\n\nOn 03/06/2026 17:14, Patrick Steinhardt wrote:\n> In a subsequent commit we'll introduce a new caller to `reset_head()`\n> that really only wants to update the index and working tree, without\n> updating any references. Introduce a new flag that lets the caller\n> perform this operation.\n\nWe already have a flag to update ORIG_HEAD so would it make more sense \nto have a flag to update HEAD, rather than adding a flag to disable the \nupdates? It would mean updating the existing callers but I think it is a \nclearer api and it avoids the pitfall of\n\n\tRESET_HEAD_ORIG_HEAD | RESET_HEAD_SKIP_REF_UPDATES\n\nI wonder about the function name as well if we make updating HEAD \noptional then what does reset_head() mean? Maybe we should rename it \nsomething along the lines of reset_worktree() or update_working_copy()? \nI'm not really sure what a good name would be.\n\nThanks\n\nPhillip\n\n> Signed-off-by: Patrick Steinhardt <ps@pks.im>\n> ---\n>   reset.c | 7 ++++++-\n>   reset.h | 3 +++\n>   2 files changed, 9 insertions(+), 1 deletion(-)\n> \n> diff --git a/reset.c b/reset.c\n> index a8d7eea4d6..ed9df6ca5c 100644\n> --- a/reset.c\n> +++ b/reset.c\n> @@ -93,6 +93,7 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n>   \tunsigned refs_only = opts->flags & RESET_HEAD_REFS_ONLY;\n>   \tunsigned update_orig_head = opts->flags & RESET_HEAD_ORIG_HEAD;\n>   \tunsigned dry_run = opts->flags & RESET_HEAD_DRY_RUN;\n> +\tunsigned skip_ref_updates = opts->flags & RESET_HEAD_SKIP_REF_UPDATES;\n>   \tstruct object_id *head = NULL, head_oid;\n>   \tstruct tree_desc desc[2] = { { NULL }, { NULL } };\n>   \tstruct lock_file lock = LOCK_INIT;\n> @@ -112,6 +113,9 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n>   \tif (opts->branch_msg && !opts->branch)\n>   \t\tBUG(\"branch reflog message given without a branch\");\n>   \n> +\tif (skip_ref_updates && (opts->branch || refs_only))\n> +\t\tBUG(\"asked to perform ref updates and skip them at the same time\");\n> +\n>   \tif (!refs_only && !dry_run && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {\n>   \t\tret = -1;\n>   \t\tgoto leave_reset_head;\n> @@ -196,7 +200,8 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n>   \t\tgoto leave_reset_head;\n>   \t}\n>   \n> -\tif (oid != &head_oid || update_orig_head || switch_to_branch)\n> +\tif (!skip_ref_updates &&\n> +\t    (oid != &head_oid || update_orig_head || switch_to_branch))\n>   \t\tret = update_refs(r, opts, oid, head);\n>   \n>   leave_reset_head:\n> diff --git a/reset.h b/reset.h\n> index 9f696382c1..cb0700ffa7 100644\n> --- a/reset.h\n> +++ b/reset.h\n> @@ -27,6 +27,9 @@ enum reset_head_flags {\n>   \t * any user-visible state.\n>   \t */\n>   \tRESET_HEAD_DRY_RUN = (1 << 5),\n> +\n> +\t/* Skip updating any references, only update the worktree and index. */\n> +\tRESET_HEAD_SKIP_REF_UPDATES = (1 << 6),\n>   };\n>   \n>   struct reset_head_opts {\n> \n\n"},{"id":"544876","messageId":"aiaH3ZmFZfmWYwr7@pks.im","threadId":"65726","inReplyTo":"0fdaeec8-99cd-4dc9-9549-8a08133deebf@gmail.com","subject":"Re: [PATCH v2 5/9] reset: introduce ability to skip reference updates","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-08T09:14:05Z","receivedAt":"2026-06-08T09:14:13Z","isPatch":true,"body":"On Fri, Jun 05, 2026 at 04:12:42PM +0100, Phillip Wood wrote:\n> Hi Patrick\n> \n> On 03/06/2026 17:14, Patrick Steinhardt wrote:\n> > In a subsequent commit we'll introduce a new caller to `reset_head()`\n> > that really only wants to update the index and working tree, without\n> > updating any references. Introduce a new flag that lets the caller\n> > perform this operation.\n> \n> We already have a flag to update ORIG_HEAD so would it make more sense to\n> have a flag to update HEAD, rather than adding a flag to disable the\n> updates? It would mean updating the existing callers but I think it is a\n> clearer api and it avoids the pitfall of\n> \n> \tRESET_HEAD_ORIG_HEAD | RESET_HEAD_SKIP_REF_UPDATES\n\nHm. The question is whether it's sensible to have\n`!RESET_HEAD_UPDATE_HEAD && RESET_HEAD_UPDATE_ORIG_HEAD`. That feels\nlike a somewhat weird request, too, and we'd have to introduce extra\nlogic to make that combination work.\n\n> I wonder about the function name as well if we make updating HEAD optional\n> then what does reset_head() mean? Maybe we should rename it something along\n> the lines of reset_worktree() or update_working_copy()? I'm not really sure\n> what a good name would be.\n\nThat's a good point, the name does get somewhat awkward. I think we\nshould keep \"reset\" in there, but `reset_worktree()` to me reads as it\nif was rather related to git-worktree(1) than anything else. Maybe\n`reset_working_tree()`?\n\nPatrick\n"},{"id":"544877","messageId":"aiaH7pMleQmnzmTc@pks.im","threadId":"65726","inReplyTo":"9e2cb34b-97f6-44f4-be44-60f44760e601@gmail.com","subject":"Re: [PATCH v2 3/9] reset: modernize flags passed to `reset_head()`","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-08T09:14:22Z","receivedAt":"2026-06-08T09:14:27Z","isPatch":true,"body":"On Fri, Jun 05, 2026 at 04:08:57PM +0100, Phillip Wood wrote:\n> Hi Patrick\n> \n> On 03/06/2026 17:14, Patrick Steinhardt wrote:\n> \n> > -/* Update ORIG_HEAD as well as HEAD */\n> > -#define RESET_ORIG_HEAD (1<<4)\n> > [...]> +\t/* Update ORIG_HEAD as well as HEAD */\n> > +\tRESET_HEAD_ORIG_HEAD = (1 << 4),\n> \n> I'm having a hard time parsing this new name, if we must have a\n> \"RESET_HEAD_\" prefix can we call it something like\n> RESET_HEAD_UPDATE_ORIG_HEAD?\n\nYeah, that reads better indeed. Amended locally, thanks!\n\nPatrick\n"},{"id":"544878","messageId":"aiaI6thDj8y_EekG@pks.im","threadId":"65726","inReplyTo":"aiaH3ZmFZfmWYwr7@pks.im","subject":"Re: [PATCH v2 5/9] reset: introduce ability to skip reference updates","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-08T09:18:34Z","receivedAt":"2026-06-08T09:18:39Z","isPatch":true,"body":"On Mon, Jun 08, 2026 at 11:14:08AM +0200, Patrick Steinhardt wrote:\n> On Fri, Jun 05, 2026 at 04:12:42PM +0100, Phillip Wood wrote:\n> > Hi Patrick\n> > \n> > On 03/06/2026 17:14, Patrick Steinhardt wrote:\n> > > In a subsequent commit we'll introduce a new caller to `reset_head()`\n> > > that really only wants to update the index and working tree, without\n> > > updating any references. Introduce a new flag that lets the caller\n> > > perform this operation.\n> > \n> > We already have a flag to update ORIG_HEAD so would it make more sense to\n> > have a flag to update HEAD, rather than adding a flag to disable the\n> > updates? It would mean updating the existing callers but I think it is a\n> > clearer api and it avoids the pitfall of\n> > \n> > \tRESET_HEAD_ORIG_HEAD | RESET_HEAD_SKIP_REF_UPDATES\n> \n> Hm. The question is whether it's sensible to have\n> `!RESET_HEAD_UPDATE_HEAD && RESET_HEAD_UPDATE_ORIG_HEAD`. That feels\n> like a somewhat weird request, too, and we'd have to introduce extra\n> logic to make that combination work.\n> \n> > I wonder about the function name as well if we make updating HEAD optional\n> > then what does reset_head() mean? Maybe we should rename it something along\n> > the lines of reset_worktree() or update_working_copy()? I'm not really sure\n> > what a good name would be.\n> \n> That's a good point, the name does get somewhat awkward. I think we\n> should keep \"reset\" in there, but `reset_worktree()` to me reads as it\n> if was rather related to git-worktree(1) than anything else. Maybe\n> `reset_working_tree()`?\n\nI think I'll skip these changes for the next iteration for now. The\npatch series has already exploded quite a bit in its scope due to the\nrefactorings of `reset_head()`, so I'd prefer to maybe do such changes\nas a follow up.\n\nLet me know in case you feel strongly about this though. Thanks!\n\nPatrick\n"},{"id":"544894","messageId":"20260608-b4-pks-history-drop-v3-0-84ca8e43e937@pks.im","threadId":"65726","inReplyTo":"20260601-b4-pks-history-drop-v1-0-643e32340d55@pks.im","subject":"[PATCH v3 0/9] builtin/history: introduce \"drop\" subcommand","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-08T10:23:24Z","receivedAt":"2026-06-08T10:23:35Z","isPatch":true,"body":"Hi,\n\nthis small patch series introduces the new \"drop\" subcommand for\ngit-history(1). As a reader might guess, the command does exactly that:\ngiven a commit, it will drop that commit from the commit history and\nreplay descendant branches on top of it.\n\nChanges in v3:\n  - Fix commit message typos.\n  - Make `update_orig_head` and `skip_ref_updates` mutually exclusive.\n  - Use fancy revisions to specify the commit to drop in the example\n    section.\n  - Detect conflicting changes in the index/working tree in dry-run\n    mode.\n  - Consistently use a subshell.\n  - Rename `RESET_HEAD_ORIG_HEAD` to `RESET_HEAD_UPDATE_ORIG_HEAD`.\n  - \n  - Link to v2: https://patch.msgid.link/20260603-b4-pks-history-drop-v2-0-742cb5b5176d@pks.im\n\nChanges in v2:\n  - Reworked `update_worktree()` to use `reset_head()`, which required a\n    bunch of changes to `reset_head()`.\n  - Consistently mention the commit that cannot be dropped as part of\n    error messages.\n  - Adapt error message to not use backticks anymore.\n  - Drop redundant \"--graph\" flag in a test helper.\n  - Link to v1: https://patch.msgid.link/20260601-b4-pks-history-drop-v1-0-643e32340d55@pks.im\n\nThanks!\n\nPatrick\n\n---\nPatrick Steinhardt (9):\n      read-cache: split out function to drop unmerged entries to stage 0\n      reset: drop `USE_THE_REPOSITORY_VARIABLE`\n      reset: modernize flags passed to `reset_head()`\n      reset: introduce dry-run mode\n      reset: introduce ability to skip reference updates\n      reset: allow the caller to specify the current HEAD object\n      reset: stop assuming that the caller passes in a clean index\n      builtin/history: split handling of ref updates into two phases\n      builtin/history: implement \"drop\" subcommand\n\n Documentation/git-history.adoc |  38 ++-\n builtin/history.c              | 289 +++++++++++++++++++---\n builtin/rebase.c               |   2 +-\n read-cache-ll.h                |   1 +\n read-cache.c                   |  12 +-\n reset.c                        |  91 ++++---\n reset.h                        |  44 +++-\n sequencer.c                    |   2 +-\n t/meson.build                  |   1 +\n t/t3454-history-drop.sh        | 537 +++++++++++++++++++++++++++++++++++++++++\n 10 files changed, 929 insertions(+), 88 deletions(-)\n\nRange-diff versus v2:\n\n 1:  a93e804936 =  1:  41a723b3d0 read-cache: split out function to drop unmerged entries to stage 0\n 2:  d8f39e7dc4 =  2:  db850730ef reset: drop `USE_THE_REPOSITORY_VARIABLE`\n 3:  fdec5a57b4 !  3:  bd18736141 reset: modernize flags passed to `reset_head()`\n    @@ builtin/rebase.c: int cmd_rebase(int argc,\n      \tropts.oid = &options.onto->object.oid;\n      \tropts.orig_head = &options.orig_head->object.oid;\n     -\tropts.flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD |\n    -+\tropts.flags = RESET_HEAD_DETACH | RESET_HEAD_ORIG_HEAD |\n    ++\tropts.flags = RESET_HEAD_DETACH | RESET_HEAD_UPDATE_ORIG_HEAD |\n      \t\t\tRESET_HEAD_RUN_POST_CHECKOUT_HOOK;\n      \tropts.head_msg = msg.buf;\n      \tropts.default_reflog_action = options.reflog_action;\n    @@ reset.c: static int update_refs(struct repository *repo,\n      \tunsigned detach_head = opts->flags & RESET_HEAD_DETACH;\n      \tunsigned run_hook = opts->flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK;\n     -\tunsigned update_orig_head = opts->flags & RESET_ORIG_HEAD;\n    -+\tunsigned update_orig_head = opts->flags & RESET_HEAD_ORIG_HEAD;\n    ++\tunsigned update_orig_head = opts->flags & RESET_HEAD_UPDATE_ORIG_HEAD;\n      \tconst struct object_id *orig_head = opts->orig_head;\n      \tconst char *switch_to_branch = opts->branch;\n      \tconst char *reflog_branch = opts->branch_msg;\n    @@ reset.c: int reset_head(struct repository *r, const struct reset_head_opts *opts\n      \tunsigned reset_hard = opts->flags & RESET_HEAD_HARD;\n      \tunsigned refs_only = opts->flags & RESET_HEAD_REFS_ONLY;\n     -\tunsigned update_orig_head = opts->flags & RESET_ORIG_HEAD;\n    -+\tunsigned update_orig_head = opts->flags & RESET_HEAD_ORIG_HEAD;\n    ++\tunsigned update_orig_head = opts->flags & RESET_HEAD_UPDATE_ORIG_HEAD;\n      \tstruct object_id *head = NULL, head_oid;\n      \tstruct tree_desc desc[2] = { { NULL }, { NULL } };\n      \tstruct lock_file lock = LOCK_INIT;\n    @@ reset.h\n     +\tRESET_HEAD_REFS_ONLY = (1 << 3),\n     +\n     +\t/* Update ORIG_HEAD as well as HEAD */\n    -+\tRESET_HEAD_ORIG_HEAD = (1 << 4),\n    ++\tRESET_HEAD_UPDATE_ORIG_HEAD = (1 << 4),\n     +};\n      \n      struct reset_head_opts {\n    @@ reset.h: struct reset_head_opts {\n      \t/*\n      \t * Optional reflog message for ORIG_HEAD, if this omitted and flags\n     -\t * contains RESET_ORIG_HEAD then default_reflog_action must be given.\n    -+\t * contains RESET_HEAD_ORIG_HEAD then default_reflog_action must be given.\n    ++\t * contains RESET_HEAD_UPDATE_ORIG_HEAD then default_reflog_action must be given.\n      \t */\n      \tconst char *orig_head_msg;\n      \t/*\n    @@ sequencer.c: static int checkout_onto(struct repository *r, struct replay_opts *\n      \t\t.oid = onto,\n      \t\t.orig_head = orig_head,\n     -\t\t.flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD |\n    -+\t\t.flags = RESET_HEAD_DETACH | RESET_HEAD_ORIG_HEAD |\n    ++\t\t.flags = RESET_HEAD_DETACH | RESET_HEAD_UPDATE_ORIG_HEAD |\n      \t\t\t\tRESET_HEAD_RUN_POST_CHECKOUT_HOOK,\n      \t\t.head_msg = reflog_message(opts, \"start\", \"checkout %s\",\n      \t\t\t\t\t   onto_name),\n 4:  3af7c9a4fd !  4:  8c79e56076 reset: introduce dry-run mode\n    @@ Metadata\n      ## Commit message ##\n         reset: introduce dry-run mode\n     \n    -    In a subsequent commit we'll add add another caller to `reset_head()`\n    -    that wants to perform a dry-run check of whether it would be possible to\n    -    udpate the index and working tree when moving to a new commit. Introduce\n    +    In a subsequent commit we'll add another caller to `reset_head()` that\n    +    wants to perform a dry-run check of whether it would be possible to\n    +    update the index and working tree when moving to a new commit. Introduce\n         a new flag that lets the caller perform this operation.\n     \n         Signed-off-by: Patrick Steinhardt <ps@pks.im>\n    @@ reset.c\n     @@ reset.c: int reset_head(struct repository *r, const struct reset_head_opts *opts)\n      \tunsigned reset_hard = opts->flags & RESET_HEAD_HARD;\n      \tunsigned refs_only = opts->flags & RESET_HEAD_REFS_ONLY;\n    - \tunsigned update_orig_head = opts->flags & RESET_HEAD_ORIG_HEAD;\n    + \tunsigned update_orig_head = opts->flags & RESET_HEAD_UPDATE_ORIG_HEAD;\n     +\tunsigned dry_run = opts->flags & RESET_HEAD_DRY_RUN;\n      \tstruct object_id *head = NULL, head_oid;\n      \tstruct tree_desc desc[2] = { { NULL }, { NULL } };\n    @@ reset.h\n     @@ reset.h: enum reset_head_flags {\n      \n      \t/* Update ORIG_HEAD as well as HEAD */\n    - \tRESET_HEAD_ORIG_HEAD = (1 << 4),\n    + \tRESET_HEAD_UPDATE_ORIG_HEAD = (1 << 4),\n     +\n     +\t/*\n     +\t * Perform a dry-run by performing the operation without updating\n 5:  31d1ff1d4c !  5:  c112fbbd14 reset: introduce ability to skip reference updates\n    @@ Commit message\n      ## reset.c ##\n     @@ reset.c: int reset_head(struct repository *r, const struct reset_head_opts *opts)\n      \tunsigned refs_only = opts->flags & RESET_HEAD_REFS_ONLY;\n    - \tunsigned update_orig_head = opts->flags & RESET_HEAD_ORIG_HEAD;\n    + \tunsigned update_orig_head = opts->flags & RESET_HEAD_UPDATE_ORIG_HEAD;\n      \tunsigned dry_run = opts->flags & RESET_HEAD_DRY_RUN;\n     +\tunsigned skip_ref_updates = opts->flags & RESET_HEAD_SKIP_REF_UPDATES;\n      \tstruct object_id *head = NULL, head_oid;\n    @@ reset.c: int reset_head(struct repository *r, const struct reset_head_opts *opts\n      \tif (opts->branch_msg && !opts->branch)\n      \t\tBUG(\"branch reflog message given without a branch\");\n      \n    -+\tif (skip_ref_updates && (opts->branch || refs_only))\n    ++\tif (skip_ref_updates && (opts->branch || refs_only || update_orig_head))\n     +\t\tBUG(\"asked to perform ref updates and skip them at the same time\");\n     +\n      \tif (!refs_only && !dry_run && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {\n 6:  21b2d4d281 =  6:  9550257dd1 reset: allow the caller to specify the current HEAD object\n 7:  7c032ca1e3 =  7:  8a3d517020 reset: stop assuming that the caller passes in a clean index\n 8:  fcd4479178 =  8:  a09be2ffc0 builtin/history: split handling of ref updates into two phases\n 9:  6b1c17a8df !  9:  682b11af93 builtin/history: implement \"drop\" subcommand\n    @@ Documentation/git-history.adoc: The staged addition of `unrelated.txt` has been\n     +def5678 second\n     +ghi9012 first\n     +\n    -+$ git history drop def5678\n    ++$ git history drop 'main^{/second}'\n     +\n     +$ git log --oneline\n     +jkl3456 (HEAD -> main) third\n    @@ builtin/history.c: static int cmd_history_split(int argc,\n     +\t * inconsistent repository state. So we first perform a dry-run merge\n     +\t * here before updating refs.\n     +\t */\n    -+\tif (!dry_run && !is_bare_repository()) {\n    ++\tif (!is_bare_repository()) {\n     +\t\tret = find_head_tree_change(repo, &result, &old_head,\n     +\t\t\t\t\t    &new_head, &head_moves);\n     +\t\tif (ret < 0)\n    @@ builtin/history.c: static int cmd_history_split(int argc,\n     +\t\tgoto out;\n     +\t}\n     +\n    -+\tif (head_moves && update_worktree(repo, old_head, new_head, false) < 0) {\n    ++\tif (!dry_run && head_moves && update_worktree(repo, old_head, new_head, false) < 0) {\n     +\t\tret = error(_(\"could not update working tree to new commit %s\"),\n     +\t\t\t    oid_to_hex(&new_head->object.oid));\n     +\t\tgoto out;\n    @@ t/t3454-history-drop.sh (new)\n     +test_expect_success 'errors with invalid --empty= value' '\n     +\ttest_when_finished \"rm -rf repo\" &&\n     +\tgit init repo &&\n    -+\ttest_commit -C repo initial &&\n    -+\ttest_commit -C repo second &&\n    -+\ttest_must_fail git -C repo history drop --empty=bogus HEAD 2>err &&\n    -+\ttest_grep \"unrecognized.*--empty.*bogus\" err\n    ++\t(\n    ++\t\tcd repo &&\n    ++\t\ttest_commit initial &&\n    ++\t\ttest_commit second &&\n    ++\t\ttest_must_fail git history drop --empty=bogus HEAD 2>err &&\n    ++\t\ttest_grep \"unrecognized.*--empty.*bogus\" err\n    ++\t)\n     +'\n     +\n     +test_expect_success 'drops a commit in the middle and replays descendants' '\n    @@ t/t3454-history-drop.sh (new)\n     +\t)\n     +'\n     +\n    ++test_expect_success '--dry-run detects conflicts with modified working tree' '\n    ++\ttest_when_finished \"rm -rf repo\" &&\n    ++\tgit init repo --initial-branch=main &&\n    ++\t(\n    ++\t\tcd repo &&\n    ++\t\ttest_commit first &&\n    ++\t\ttest_commit second modify-me &&\n    ++\t\techo modified >modify-me &&\n    ++\n    ++\t\tgit refs list >refs-expect &&\n    ++\t\tgit diff >diff-expect &&\n    ++\t\ttest_must_fail git history drop --dry-run HEAD 2>err &&\n    ++\t\ttest_grep \"dropping this commit would overwrite local changes\" err &&\n    ++\t\tgit diff >diff-actual &&\n    ++\t\tgit refs list >refs-actual &&\n    ++\n    ++\t\ttest_cmp diff-expect diff-actual &&\n    ++\t\ttest_cmp refs-expect refs-actual\n    ++\t)\n    ++'\n    ++\n     +test_expect_success '--update-refs=head updates only HEAD' '\n     +\ttest_when_finished \"rm -rf repo\" &&\n     +\tgit init repo --initial-branch=main &&\n\n---\nbase-commit: 1666c1265231b0bc5f613fbbf3f0a9896cdef76e\nchange-id: 20260601-b4-pks-history-drop-28f6c6399e7b\n\n"},{"id":"544895","messageId":"20260608-b4-pks-history-drop-v3-1-84ca8e43e937@pks.im","threadId":"65726","inReplyTo":"20260608-b4-pks-history-drop-v3-0-84ca8e43e937@pks.im","subject":"[PATCH v3 1/9] read-cache: split out function to drop unmerged entries to stage 0","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-08T10:23:25Z","receivedAt":"2026-06-08T10:23:36Z","isPatch":true,"body":"In `repo_read_index_unmerged()` we read the index and then drop any\nunmerged entries to stage 0. In a subsequent commit we'll want to\nperform this operation on arbitrary indexes, not only the one of the\ngiven repository.\n\nPrepare for this by splitting out the functionality into a new function\nthat can act on an arbitrary index.\n\nWhile at it, fix a signedness mismatch when iterating through the index\ncache entries.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n read-cache-ll.h |  1 +\n read-cache.c    | 12 +++++++-----\n 2 files changed, 8 insertions(+), 5 deletions(-)\n\ndiff --git a/read-cache-ll.h b/read-cache-ll.h\nindex 2c8b4b21b1..71b87615eb 100644\n--- a/read-cache-ll.h\n+++ b/read-cache-ll.h\n@@ -309,6 +309,7 @@ int write_locked_index(struct index_state *, struct lock_file *lock, unsigned fl\n void discard_index(struct index_state *);\n void move_index_extensions(struct index_state *dst, struct index_state *src);\n int unmerged_index(const struct index_state *);\n+int index_state_unmerged_to_stage0(struct index_state *istate);\n \n /**\n  * Returns 1 if istate differs from tree, 0 otherwise.  If tree is NULL,\ndiff --git a/read-cache.c b/read-cache.c\nindex 21829102ae..799a5bc719 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -3403,13 +3403,15 @@ int write_locked_index(struct index_state *istate, struct lock_file *lock,\n  */\n int repo_read_index_unmerged(struct repository *repo)\n {\n-\tstruct index_state *istate;\n-\tint i;\n+\trepo_read_index(repo);\n+\treturn index_state_unmerged_to_stage0(repo->index);\n+}\n+\n+int index_state_unmerged_to_stage0(struct index_state *istate)\n+{\n \tint unmerged = 0;\n \n-\trepo_read_index(repo);\n-\tistate = repo->index;\n-\tfor (i = 0; i < istate->cache_nr; i++) {\n+\tfor (unsigned int i = 0; i < istate->cache_nr; i++) {\n \t\tstruct cache_entry *ce = istate->cache[i];\n \t\tstruct cache_entry *new_ce;\n \t\tint len;\n\n-- \n2.54.0.1136.gdb2ca164c4.dirty\n\n"},{"id":"544896","messageId":"20260608-b4-pks-history-drop-v3-2-84ca8e43e937@pks.im","threadId":"65726","inReplyTo":"20260608-b4-pks-history-drop-v3-0-84ca8e43e937@pks.im","subject":"[PATCH v3 2/9] reset: drop `USE_THE_REPOSITORY_VARIABLE`","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-08T10:23:26Z","receivedAt":"2026-06-08T10:23:37Z","isPatch":true,"body":"In \"reset.c\" we still have references to `the_repository`, even though\nthe only entry point into the file already receives a repository as\nparameter.\n\nUpdate all uses of `the_repository` to instead use the passed-in repo\nand drop `USE_THE_REPOSITORY_VARIABLE`.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reset.c | 27 +++++++++++++--------------\n 1 file changed, 13 insertions(+), 14 deletions(-)\n\ndiff --git a/reset.c b/reset.c\nindex 46e30e6394..3b3cb74dab 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -1,5 +1,3 @@\n-#define USE_THE_REPOSITORY_VARIABLE\n-\n #include \"git-compat-util.h\"\n #include \"cache-tree.h\"\n #include \"gettext.h\"\n@@ -13,7 +11,8 @@\n #include \"unpack-trees.h\"\n #include \"hook.h\"\n \n-static int update_refs(const struct reset_head_opts *opts,\n+static int update_refs(struct repository *repo,\n+\t\t       const struct reset_head_opts *opts,\n \t\t       const struct object_id *oid,\n \t\t       const struct object_id *head)\n {\n@@ -42,19 +41,19 @@ static int update_refs(const struct reset_head_opts *opts,\n \tprefix_len = msg.len;\n \n \tif (update_orig_head) {\n-\t\tif (!repo_get_oid(the_repository, \"ORIG_HEAD\", &oid_old_orig))\n+\t\tif (!repo_get_oid(repo, \"ORIG_HEAD\", &oid_old_orig))\n \t\t\told_orig = &oid_old_orig;\n \t\tif (head) {\n \t\t\tif (!reflog_orig_head) {\n \t\t\t\tstrbuf_addstr(&msg, \"updating ORIG_HEAD\");\n \t\t\t\treflog_orig_head = msg.buf;\n \t\t\t}\n-\t\t\trefs_update_ref(get_main_ref_store(the_repository),\n+\t\t\trefs_update_ref(get_main_ref_store(repo),\n \t\t\t\t\treflog_orig_head, \"ORIG_HEAD\",\n \t\t\t\t\torig_head ? orig_head : head,\n \t\t\t\t\told_orig, 0, UPDATE_REFS_MSG_ON_ERR);\n \t\t} else if (old_orig)\n-\t\t\trefs_delete_ref(get_main_ref_store(the_repository),\n+\t\t\trefs_delete_ref(get_main_ref_store(repo),\n \t\t\t\t\tNULL, \"ORIG_HEAD\", old_orig, 0);\n \t}\n \n@@ -64,23 +63,23 @@ static int update_refs(const struct reset_head_opts *opts,\n \t\treflog_head = msg.buf;\n \t}\n \tif (!switch_to_branch)\n-\t\tret = refs_update_ref(get_main_ref_store(the_repository),\n+\t\tret = refs_update_ref(get_main_ref_store(repo),\n \t\t\t\t      reflog_head, \"HEAD\", oid, head,\n \t\t\t\t      detach_head ? REF_NO_DEREF : 0,\n \t\t\t\t      UPDATE_REFS_MSG_ON_ERR);\n \telse {\n-\t\tret = refs_update_ref(get_main_ref_store(the_repository),\n+\t\tret = refs_update_ref(get_main_ref_store(repo),\n \t\t\t\t      reflog_branch ? reflog_branch : reflog_head,\n \t\t\t\t      switch_to_branch, oid, NULL, 0,\n \t\t\t\t      UPDATE_REFS_MSG_ON_ERR);\n \t\tif (!ret)\n-\t\t\tret = refs_update_symref(get_main_ref_store(the_repository),\n+\t\t\tret = refs_update_symref(get_main_ref_store(repo),\n \t\t\t\t\t\t \"HEAD\", switch_to_branch,\n \t\t\t\t\t\t reflog_head);\n \t}\n \tif (!ret && run_hook)\n-\t\trun_hooks_l(the_repository, \"post-checkout\",\n-\t\t\t    oid_to_hex(head ? head : null_oid(the_hash_algo)),\n+\t\trun_hooks_l(repo, \"post-checkout\",\n+\t\t\t    oid_to_hex(head ? head : null_oid(repo->hash_algo)),\n \t\t\t    oid_to_hex(oid), \"1\", NULL);\n \tstrbuf_release(&msg);\n \treturn ret;\n@@ -126,7 +125,7 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \t\toid = &head_oid;\n \n \tif (refs_only)\n-\t\treturn update_refs(opts, oid, head);\n+\t\treturn update_refs(r, opts, oid, head);\n \n \taction = reset_hard ? \"reset\" : \"checkout\";\n \tsetup_unpack_trees_porcelain(&unpack_tree_opts, action);\n@@ -163,7 +162,7 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \t\tgoto leave_reset_head;\n \t}\n \n-\ttree = repo_parse_tree_indirect(the_repository, oid);\n+\ttree = repo_parse_tree_indirect(r, oid);\n \tif (!tree) {\n \t\tret = error(_(\"unable to read tree (%s)\"), oid_to_hex(oid));\n \t\tgoto leave_reset_head;\n@@ -177,7 +176,7 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \t}\n \n \tif (oid != &head_oid || update_orig_head || switch_to_branch)\n-\t\tret = update_refs(opts, oid, head);\n+\t\tret = update_refs(r, opts, oid, head);\n \n leave_reset_head:\n \trollback_lock_file(&lock);\n\n-- \n2.54.0.1136.gdb2ca164c4.dirty\n\n"},{"id":"544897","messageId":"20260608-b4-pks-history-drop-v3-3-84ca8e43e937@pks.im","threadId":"65726","inReplyTo":"20260608-b4-pks-history-drop-v3-0-84ca8e43e937@pks.im","subject":"[PATCH v3 3/9] reset: modernize flags passed to `reset_head()`","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-08T10:23:27Z","receivedAt":"2026-06-08T10:23:40Z","isPatch":true,"body":"The flags passed to `reset_head()` are declared as defines. This has\nfallen a bit out of practice nowadays, where we instead prefer to use\nenums.\n\nModernize the code accordingly.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n builtin/rebase.c |  2 +-\n reset.c          |  4 ++--\n reset.h          | 30 ++++++++++++++++++------------\n sequencer.c      |  2 +-\n 4 files changed, 22 insertions(+), 16 deletions(-)\n\ndiff --git a/builtin/rebase.c b/builtin/rebase.c\nindex fa4f5d9306..4c20bd50cb 100644\n--- a/builtin/rebase.c\n+++ b/builtin/rebase.c\n@@ -1876,7 +1876,7 @@ int cmd_rebase(int argc,\n \t\t    options.reflog_action, options.onto_name);\n \tropts.oid = &options.onto->object.oid;\n \tropts.orig_head = &options.orig_head->object.oid;\n-\tropts.flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD |\n+\tropts.flags = RESET_HEAD_DETACH | RESET_HEAD_UPDATE_ORIG_HEAD |\n \t\t\tRESET_HEAD_RUN_POST_CHECKOUT_HOOK;\n \tropts.head_msg = msg.buf;\n \tropts.default_reflog_action = options.reflog_action;\ndiff --git a/reset.c b/reset.c\nindex 3b3cb74dab..228cad5f42 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -18,7 +18,7 @@ static int update_refs(struct repository *repo,\n {\n \tunsigned detach_head = opts->flags & RESET_HEAD_DETACH;\n \tunsigned run_hook = opts->flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK;\n-\tunsigned update_orig_head = opts->flags & RESET_ORIG_HEAD;\n+\tunsigned update_orig_head = opts->flags & RESET_HEAD_UPDATE_ORIG_HEAD;\n \tconst struct object_id *orig_head = opts->orig_head;\n \tconst char *switch_to_branch = opts->branch;\n \tconst char *reflog_branch = opts->branch_msg;\n@@ -91,7 +91,7 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \tconst char *switch_to_branch = opts->branch;\n \tunsigned reset_hard = opts->flags & RESET_HEAD_HARD;\n \tunsigned refs_only = opts->flags & RESET_HEAD_REFS_ONLY;\n-\tunsigned update_orig_head = opts->flags & RESET_ORIG_HEAD;\n+\tunsigned update_orig_head = opts->flags & RESET_HEAD_UPDATE_ORIG_HEAD;\n \tstruct object_id *head = NULL, head_oid;\n \tstruct tree_desc desc[2] = { { NULL }, { NULL } };\n \tstruct lock_file lock = LOCK_INIT;\ndiff --git a/reset.h b/reset.h\nindex a28f81829d..0bf25c51de 100644\n--- a/reset.h\n+++ b/reset.h\n@@ -6,16 +6,22 @@\n \n #define GIT_REFLOG_ACTION_ENVIRONMENT \"GIT_REFLOG_ACTION\"\n \n-/* Request a detached checkout */\n-#define RESET_HEAD_DETACH (1<<0)\n-/* Request a reset rather than a checkout */\n-#define RESET_HEAD_HARD (1<<1)\n-/* Run the post-checkout hook */\n-#define RESET_HEAD_RUN_POST_CHECKOUT_HOOK (1<<2)\n-/* Only update refs, do not touch the worktree */\n-#define RESET_HEAD_REFS_ONLY (1<<3)\n-/* Update ORIG_HEAD as well as HEAD */\n-#define RESET_ORIG_HEAD (1<<4)\n+enum reset_head_flags {\n+\t/* Request a detached checkout */\n+\tRESET_HEAD_DETACH = (1 << 0),\n+\n+\t/* Request a reset rather than a checkout */\n+\tRESET_HEAD_HARD = (1 << 1),\n+\n+\t/* Run the post-checkout hook */\n+\tRESET_HEAD_RUN_POST_CHECKOUT_HOOK = (1 << 2),\n+\n+\t/* Only update refs, do not touch the worktree */\n+\tRESET_HEAD_REFS_ONLY = (1 << 3),\n+\n+\t/* Update ORIG_HEAD as well as HEAD */\n+\tRESET_HEAD_UPDATE_ORIG_HEAD = (1 << 4),\n+};\n \n struct reset_head_opts {\n \t/*\n@@ -33,7 +39,7 @@ struct reset_head_opts {\n \t/*\n \t * Flags defined above.\n \t */\n-\tunsigned flags;\n+\tenum reset_head_flags flags;\n \t/*\n \t * Optional reflog message for branch, defaults to head_msg.\n \t */\n@@ -45,7 +51,7 @@ struct reset_head_opts {\n \tconst char *head_msg;\n \t/*\n \t * Optional reflog message for ORIG_HEAD, if this omitted and flags\n-\t * contains RESET_ORIG_HEAD then default_reflog_action must be given.\n+\t * contains RESET_HEAD_UPDATE_ORIG_HEAD then default_reflog_action must be given.\n \t */\n \tconst char *orig_head_msg;\n \t/*\ndiff --git a/sequencer.c b/sequencer.c\nindex 1ee4b2875b..c46c606b3e 100644\n--- a/sequencer.c\n+++ b/sequencer.c\n@@ -4870,7 +4870,7 @@ static int checkout_onto(struct repository *r, struct replay_opts *opts,\n \tstruct reset_head_opts ropts = {\n \t\t.oid = onto,\n \t\t.orig_head = orig_head,\n-\t\t.flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD |\n+\t\t.flags = RESET_HEAD_DETACH | RESET_HEAD_UPDATE_ORIG_HEAD |\n \t\t\t\tRESET_HEAD_RUN_POST_CHECKOUT_HOOK,\n \t\t.head_msg = reflog_message(opts, \"start\", \"checkout %s\",\n \t\t\t\t\t   onto_name),\n\n-- \n2.54.0.1136.gdb2ca164c4.dirty\n\n"},{"id":"544898","messageId":"20260608-b4-pks-history-drop-v3-4-84ca8e43e937@pks.im","threadId":"65726","inReplyTo":"20260608-b4-pks-history-drop-v3-0-84ca8e43e937@pks.im","subject":"[PATCH v3 4/9] reset: introduce dry-run mode","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-08T10:23:28Z","receivedAt":"2026-06-08T10:23:42Z","isPatch":true,"body":"In a subsequent commit we'll add another caller to `reset_head()` that\nwants to perform a dry-run check of whether it would be possible to\nupdate the index and working tree when moving to a new commit. Introduce\na new flag that lets the caller perform this operation.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reset.c | 44 +++++++++++++++++++++++++++++++++-----------\n reset.h |  6 ++++++\n 2 files changed, 39 insertions(+), 11 deletions(-)\n\ndiff --git a/reset.c b/reset.c\nindex 228cad5f42..8fb39d4c51 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -92,11 +92,14 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \tunsigned reset_hard = opts->flags & RESET_HEAD_HARD;\n \tunsigned refs_only = opts->flags & RESET_HEAD_REFS_ONLY;\n \tunsigned update_orig_head = opts->flags & RESET_HEAD_UPDATE_ORIG_HEAD;\n+\tunsigned dry_run = opts->flags & RESET_HEAD_DRY_RUN;\n \tstruct object_id *head = NULL, head_oid;\n \tstruct tree_desc desc[2] = { { NULL }, { NULL } };\n \tstruct lock_file lock = LOCK_INIT;\n \tstruct unpack_trees_options unpack_tree_opts = { 0 };\n \tstruct tree *tree;\n+\tstruct index_state scratch_index = INDEX_STATE_INIT(r);\n+\tstruct index_state *istate;\n \tconst char *action;\n \tint ret = 0, nr = 0;\n \n@@ -109,7 +112,7 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \tif (opts->branch_msg && !opts->branch)\n \t\tBUG(\"branch reflog message given without a branch\");\n \n-\tif (!refs_only && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {\n+\tif (!refs_only && !dry_run && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {\n \t\tret = -1;\n \t\tgoto leave_reset_head;\n \t}\n@@ -124,16 +127,36 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \tif (!oid)\n \t\toid = &head_oid;\n \n-\tif (refs_only)\n-\t\treturn update_refs(r, opts, oid, head);\n+\tif (refs_only) {\n+\t\tif (!dry_run)\n+\t\t\treturn update_refs(r, opts, oid, head);\n+\t\treturn 0;\n+\t}\n+\n+\tif (dry_run) {\n+\t\tif (read_index_from(&scratch_index, r->index_file, r->gitdir) < 0 ||\n+\t\t    index_state_unmerged_to_stage0(&scratch_index) < 0) {\n+\t\t\tret = error(_(\"could not read index\"));\n+\t\t\tgoto leave_reset_head;\n+\t\t}\n+\n+\t\tistate = &scratch_index;\n+\t} else {\n+\t\tif (repo_read_index_unmerged(r) < 0) {\n+\t\t\tret = error(_(\"could not read index\"));\n+\t\t\tgoto leave_reset_head;\n+\t\t}\n+\t\tistate = r->index;\n+\t}\n \n \taction = reset_hard ? \"reset\" : \"checkout\";\n \tsetup_unpack_trees_porcelain(&unpack_tree_opts, action);\n \tunpack_tree_opts.head_idx = 1;\n-\tunpack_tree_opts.src_index = r->index;\n-\tunpack_tree_opts.dst_index = r->index;\n+\tunpack_tree_opts.src_index = istate;\n+\tunpack_tree_opts.dst_index = istate;\n \tunpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge;\n-\tunpack_tree_opts.update = 1;\n+\tunpack_tree_opts.update = !dry_run;\n+\tunpack_tree_opts.dry_run = dry_run;\n \tunpack_tree_opts.merge = 1;\n \tunpack_tree_opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */\n \tunpack_tree_opts.skip_cache_tree_update = 1;\n@@ -141,11 +164,6 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \tif (reset_hard)\n \t\tunpack_tree_opts.reset = UNPACK_RESET_PROTECT_UNTRACKED;\n \n-\tif (repo_read_index_unmerged(r) < 0) {\n-\t\tret = error(_(\"could not read index\"));\n-\t\tgoto leave_reset_head;\n-\t}\n-\n \tif (!reset_hard && !fill_tree_descriptor(r, &desc[nr++], &head_oid)) {\n \t\tret = error(_(\"failed to find tree of %s\"),\n \t\t\t    oid_to_hex(&head_oid));\n@@ -162,6 +180,9 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \t\tgoto leave_reset_head;\n \t}\n \n+\tif (dry_run)\n+\t\tgoto leave_reset_head;\n+\n \ttree = repo_parse_tree_indirect(r, oid);\n \tif (!tree) {\n \t\tret = error(_(\"unable to read tree (%s)\"), oid_to_hex(oid));\n@@ -181,6 +202,7 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n leave_reset_head:\n \trollback_lock_file(&lock);\n \tclear_unpack_trees_porcelain(&unpack_tree_opts);\n+\trelease_index(&scratch_index);\n \twhile (nr)\n \t\tfree((void *)desc[--nr].buffer);\n \treturn ret;\ndiff --git a/reset.h b/reset.h\nindex 0bf25c51de..cc9fd4378a 100644\n--- a/reset.h\n+++ b/reset.h\n@@ -21,6 +21,12 @@ enum reset_head_flags {\n \n \t/* Update ORIG_HEAD as well as HEAD */\n \tRESET_HEAD_UPDATE_ORIG_HEAD = (1 << 4),\n+\n+\t/*\n+\t * Perform a dry-run by performing the operation without updating\n+\t * any user-visible state.\n+\t */\n+\tRESET_HEAD_DRY_RUN = (1 << 5),\n };\n \n struct reset_head_opts {\n\n-- \n2.54.0.1136.gdb2ca164c4.dirty\n\n"},{"id":"544899","messageId":"20260608-b4-pks-history-drop-v3-5-84ca8e43e937@pks.im","threadId":"65726","inReplyTo":"20260608-b4-pks-history-drop-v3-0-84ca8e43e937@pks.im","subject":"[PATCH v3 5/9] reset: introduce ability to skip reference updates","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-08T10:23:29Z","receivedAt":"2026-06-08T10:23:45Z","isPatch":true,"body":"In a subsequent commit we'll introduce a new caller to `reset_head()`\nthat really only wants to update the index and working tree, without\nupdating any references. Introduce a new flag that lets the caller\nperform this operation.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reset.c | 7 ++++++-\n reset.h | 3 +++\n 2 files changed, 9 insertions(+), 1 deletion(-)\n\ndiff --git a/reset.c b/reset.c\nindex 8fb39d4c51..f88f32d563 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -93,6 +93,7 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \tunsigned refs_only = opts->flags & RESET_HEAD_REFS_ONLY;\n \tunsigned update_orig_head = opts->flags & RESET_HEAD_UPDATE_ORIG_HEAD;\n \tunsigned dry_run = opts->flags & RESET_HEAD_DRY_RUN;\n+\tunsigned skip_ref_updates = opts->flags & RESET_HEAD_SKIP_REF_UPDATES;\n \tstruct object_id *head = NULL, head_oid;\n \tstruct tree_desc desc[2] = { { NULL }, { NULL } };\n \tstruct lock_file lock = LOCK_INIT;\n@@ -112,6 +113,9 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \tif (opts->branch_msg && !opts->branch)\n \t\tBUG(\"branch reflog message given without a branch\");\n \n+\tif (skip_ref_updates && (opts->branch || refs_only || update_orig_head))\n+\t\tBUG(\"asked to perform ref updates and skip them at the same time\");\n+\n \tif (!refs_only && !dry_run && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {\n \t\tret = -1;\n \t\tgoto leave_reset_head;\n@@ -196,7 +200,8 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \t\tgoto leave_reset_head;\n \t}\n \n-\tif (oid != &head_oid || update_orig_head || switch_to_branch)\n+\tif (!skip_ref_updates &&\n+\t    (oid != &head_oid || update_orig_head || switch_to_branch))\n \t\tret = update_refs(r, opts, oid, head);\n \n leave_reset_head:\ndiff --git a/reset.h b/reset.h\nindex cc9fd4378a..d2f8546844 100644\n--- a/reset.h\n+++ b/reset.h\n@@ -27,6 +27,9 @@ enum reset_head_flags {\n \t * any user-visible state.\n \t */\n \tRESET_HEAD_DRY_RUN = (1 << 5),\n+\n+\t/* Skip updating any references, only update the worktree and index. */\n+\tRESET_HEAD_SKIP_REF_UPDATES = (1 << 6),\n };\n \n struct reset_head_opts {\n\n-- \n2.54.0.1136.gdb2ca164c4.dirty\n\n"},{"id":"544900","messageId":"20260608-b4-pks-history-drop-v3-6-84ca8e43e937@pks.im","threadId":"65726","inReplyTo":"20260608-b4-pks-history-drop-v3-0-84ca8e43e937@pks.im","subject":"[PATCH v3 6/9] reset: allow the caller to specify the current HEAD object","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-08T10:23:30Z","receivedAt":"2026-06-08T10:23:48Z","isPatch":true,"body":"When calling `reset_head()` we automatically derive the commit that the\ncallers wants to move from by reading the HEAD commit. Some callers may\nalready have resolved it, or they may want to move from a different\ncommit that doesn't match HEAD.\n\nIntroduce a new `oid_from` option that lets the caller specify the\ncommit.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reset.c | 5 ++++-\n reset.h | 5 +++++\n 2 files changed, 9 insertions(+), 1 deletion(-)\n\ndiff --git a/reset.c b/reset.c\nindex f88f32d563..5ba9a3a574 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -121,7 +121,10 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \t\tgoto leave_reset_head;\n \t}\n \n-\tif (!repo_get_oid(r, \"HEAD\", &head_oid)) {\n+\tif (opts->oid_from) {\n+\t\toidcpy(&head_oid, opts->oid_from);\n+\t\thead = &head_oid;\n+\t} else if (!repo_get_oid(r, \"HEAD\", &head_oid)) {\n \t\thead = &head_oid;\n \t} else if (!oid || !reset_hard) {\n \t\tret = error(_(\"could not determine HEAD revision\"));\ndiff --git a/reset.h b/reset.h\nindex d2f8546844..9387fc7dce 100644\n--- a/reset.h\n+++ b/reset.h\n@@ -37,6 +37,11 @@ struct reset_head_opts {\n \t * The commit to checkout/reset to. Defaults to HEAD.\n \t */\n \tconst struct object_id *oid;\n+\t/*\n+\t * The commit to checkout/reset from when doing a two-way merge. This\n+\t * is used as one of the sides to merge.\n+\t */\n+\tconst struct object_id *oid_from;\n \t/*\n \t * Optional value to set ORIG_HEAD. Defaults to HEAD.\n \t */\n\n-- \n2.54.0.1136.gdb2ca164c4.dirty\n\n"},{"id":"544901","messageId":"20260608-b4-pks-history-drop-v3-7-84ca8e43e937@pks.im","threadId":"65726","inReplyTo":"20260608-b4-pks-history-drop-v3-0-84ca8e43e937@pks.im","subject":"[PATCH v3 7/9] reset: stop assuming that the caller passes in a clean index","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-08T10:23:31Z","receivedAt":"2026-06-08T10:23:50Z","isPatch":true,"body":"In 652bd0211d (rebase: use 'skip_cache_tree_update' option, 2022-11-10),\nwe updated `reset_head()` to stop updating the index tree cache. This\nwas done as a performance optimization: the function is only called by\n\"sequencer.c\" and \"rebase.c\", both of which assume a clean index before\nthey perform their operation, so we know that the end result will be a\nclean index, too. Consequently, we can skip recomputing the cache as we\ncan instead use `prime_cache_tree()` directly.\n\nIn a subsequent commit we're about to add a new caller though where the\nassumption doesn't hold anymore: the index may be dirty before calling\n`reset_head()`, and consequently we cannot prime the cache with a given\ntree anymore as the index and tree will mismatch.\n\nAdapt the logic so that we only skip the cache tree update in case we're\ndoing a hard reset. While we could introduce logic that only skips the\nupdate in case the incoming index was dirty already, that doesn't really\nfeel worth it: after all, the mentioned commit says itself that the\nperformance improvement was negligible anyway.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reset.c | 8 +++++---\n 1 file changed, 5 insertions(+), 3 deletions(-)\n\ndiff --git a/reset.c b/reset.c\nindex 5ba9a3a574..2fabc54d9b 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -166,10 +166,11 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \tunpack_tree_opts.dry_run = dry_run;\n \tunpack_tree_opts.merge = 1;\n \tunpack_tree_opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */\n-\tunpack_tree_opts.skip_cache_tree_update = 1;\n \tinit_checkout_metadata(&unpack_tree_opts.meta, switch_to_branch, oid, NULL);\n-\tif (reset_hard)\n+\tif (reset_hard) {\n+\t\tunpack_tree_opts.skip_cache_tree_update = 1;\n \t\tunpack_tree_opts.reset = UNPACK_RESET_PROTECT_UNTRACKED;\n+\t}\n \n \tif (!reset_hard && !fill_tree_descriptor(r, &desc[nr++], &head_oid)) {\n \t\tret = error(_(\"failed to find tree of %s\"),\n@@ -196,7 +197,8 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \t\tgoto leave_reset_head;\n \t}\n \n-\tprime_cache_tree(r, r->index, tree);\n+\tif (reset_hard)\n+\t\tprime_cache_tree(r, r->index, tree);\n \n \tif (write_locked_index(r->index, &lock, COMMIT_LOCK) < 0) {\n \t\tret = error(_(\"could not write index\"));\n\n-- \n2.54.0.1136.gdb2ca164c4.dirty\n\n"},{"id":"544902","messageId":"20260608-b4-pks-history-drop-v3-8-84ca8e43e937@pks.im","threadId":"65726","inReplyTo":"20260608-b4-pks-history-drop-v3-0-84ca8e43e937@pks.im","subject":"[PATCH v3 8/9] builtin/history: split handling of ref updates into two phases","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-08T10:23:32Z","receivedAt":"2026-06-08T10:23:53Z","isPatch":true,"body":"The function `handle_reference_updates()` is used by git-history(1) to\nupdate all references that refer to commits that have been rewritten. As\nsuch, it performs two steps:\n\n  - It gathers the references that need to be updated in the first\n    place.\n\n  - It prepares and commits the reference transaction.\n\nIn a subsequent commit we'll want to handle those two steps separately.\nPrepare for this by splitting up the function into two.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n builtin/history.c | 102 ++++++++++++++++++++++++++++++++++--------------------\n 1 file changed, 64 insertions(+), 38 deletions(-)\n\ndiff --git a/builtin/history.c b/builtin/history.c\nindex 0fc06fb204..4fadf38c32 100644\n--- a/builtin/history.c\n+++ b/builtin/history.c\n@@ -333,21 +333,17 @@ static int handle_ref_update(struct ref_transaction *transaction,\n \t\t\t\t      NULL, NULL, 0, reflog_msg, err);\n }\n \n-static int handle_reference_updates(struct rev_info *revs,\n-\t\t\t\t    enum ref_action action,\n-\t\t\t\t    struct commit *original,\n-\t\t\t\t    struct commit *rewritten,\n-\t\t\t\t    const char *reflog_msg,\n-\t\t\t\t    int dry_run,\n-\t\t\t\t    enum replay_empty_commit_action empty)\n+static int compute_pending_ref_updates(struct rev_info *revs,\n+\t\t\t\t       enum ref_action action,\n+\t\t\t\t       struct commit *original,\n+\t\t\t\t       struct commit *rewritten,\n+\t\t\t\t       enum replay_empty_commit_action empty,\n+\t\t\t\t       struct replay_result *result)\n {\n \tconst struct name_decoration *decoration;\n \tstruct replay_revisions_options opts = {\n \t\t.empty = empty,\n \t};\n-\tstruct replay_result result = { 0 };\n-\tstruct ref_transaction *transaction = NULL;\n-\tstruct strbuf err = STRBUF_INIT;\n \tchar hex[GIT_MAX_HEXSZ + 1];\n \tbool detached_head;\n \tint head_flags = 0;\n@@ -359,34 +355,13 @@ static int handle_reference_updates(struct rev_info *revs,\n \n \topts.onto = oid_to_hex_r(hex, &rewritten->object.oid);\n \n-\tret = replay_revisions(revs, &opts, &result);\n+\tret = replay_revisions(revs, &opts, result);\n \tif (ret)\n-\t\tgoto out;\n+\t\treturn ret;\n \n \tif (action != REF_ACTION_BRANCHES && action != REF_ACTION_HEAD)\n \t\tBUG(\"unsupported ref action %d\", action);\n \n-\tif (!dry_run) {\n-\t\ttransaction = ref_store_transaction_begin(get_main_ref_store(revs->repo), 0, &err);\n-\t\tif (!transaction) {\n-\t\t\tret = error(_(\"failed to begin ref transaction: %s\"), err.buf);\n-\t\t\tgoto out;\n-\t\t}\n-\t}\n-\n-\tfor (size_t i = 0; i < result.updates_nr; i++) {\n-\t\tret = handle_ref_update(transaction,\n-\t\t\t\t\tresult.updates[i].refname,\n-\t\t\t\t\t&result.updates[i].new_oid,\n-\t\t\t\t\t&result.updates[i].old_oid,\n-\t\t\t\t\treflog_msg, &err);\n-\t\tif (ret) {\n-\t\t\tret = error(_(\"failed to update ref '%s': %s\"),\n-\t\t\t\t    result.updates[i].refname, err.buf);\n-\t\t\tgoto out;\n-\t\t}\n-\t}\n-\n \t/*\n \t * `replay_revisions()` only updates references that are\n \t * ancestors of `rewritten`, so we need to manually\n@@ -414,14 +389,43 @@ static int handle_reference_updates(struct rev_info *revs,\n \t\t    !detached_head)\n \t\t\tcontinue;\n \n+\t\tALLOC_GROW(result->updates, result->updates_nr + 1, result->updates_alloc);\n+\t\tresult->updates[result->updates_nr].refname = xstrdup(decoration->name);\n+\t\tresult->updates[result->updates_nr].old_oid = original->object.oid;\n+\t\tresult->updates[result->updates_nr].new_oid = rewritten->object.oid;\n+\t\tresult->updates_nr++;\n+\t}\n+\n+\treturn 0;\n+}\n+\n+static int apply_pending_ref_updates(struct repository *repo,\n+\t\t\t\t     const struct replay_result *result,\n+\t\t\t\t     const char *reflog_msg,\n+\t\t\t\t     int dry_run)\n+{\n+\tstruct ref_transaction *transaction = NULL;\n+\tstruct strbuf err = STRBUF_INIT;\n+\tint ret;\n+\n+\tif (!dry_run) {\n+\t\ttransaction = ref_store_transaction_begin(get_main_ref_store(repo),\n+\t\t\t\t\t\t\t  0, &err);\n+\t\tif (!transaction) {\n+\t\t\tret = error(_(\"failed to begin ref transaction: %s\"), err.buf);\n+\t\t\tgoto out;\n+\t\t}\n+\t}\n+\n+\tfor (size_t i = 0; i < result->updates_nr; i++) {\n \t\tret = handle_ref_update(transaction,\n-\t\t\t\t\tdecoration->name,\n-\t\t\t\t\t&rewritten->object.oid,\n-\t\t\t\t\t&original->object.oid,\n+\t\t\t\t\tresult->updates[i].refname,\n+\t\t\t\t\t&result->updates[i].new_oid,\n+\t\t\t\t\t&result->updates[i].old_oid,\n \t\t\t\t\treflog_msg, &err);\n \t\tif (ret) {\n \t\t\tret = error(_(\"failed to update ref '%s': %s\"),\n-\t\t\t\t    decoration->name, err.buf);\n+\t\t\t\t    result->updates[i].refname, err.buf);\n \t\t\tgoto out;\n \t\t}\n \t}\n@@ -435,11 +439,33 @@ static int handle_reference_updates(struct rev_info *revs,\n \n out:\n \tref_transaction_free(transaction);\n-\treplay_result_release(&result);\n \tstrbuf_release(&err);\n \treturn ret;\n }\n \n+static int handle_reference_updates(struct rev_info *revs,\n+\t\t\t\t    enum ref_action action,\n+\t\t\t\t    struct commit *original,\n+\t\t\t\t    struct commit *rewritten,\n+\t\t\t\t    const char *reflog_msg,\n+\t\t\t\t    int dry_run,\n+\t\t\t\t    enum replay_empty_commit_action empty)\n+{\n+\tstruct replay_result result = { 0 };\n+\tint ret;\n+\n+\tret = compute_pending_ref_updates(revs, action, original, rewritten,\n+\t\t\t\t\t  empty, &result);\n+\tif (ret)\n+\t\tgoto out;\n+\n+\tret = apply_pending_ref_updates(revs->repo, &result, reflog_msg, dry_run);\n+\n+out:\n+\treplay_result_release(&result);\n+\treturn ret;\n+}\n+\n static int commit_became_empty(struct repository *repo,\n \t\t\t       struct commit *original,\n \t\t\t       struct tree *result)\n\n-- \n2.54.0.1136.gdb2ca164c4.dirty\n\n"},{"id":"544903","messageId":"20260608-b4-pks-history-drop-v3-9-84ca8e43e937@pks.im","threadId":"65726","inReplyTo":"20260608-b4-pks-history-drop-v3-0-84ca8e43e937@pks.im","subject":"[PATCH v3 9/9] builtin/history: implement \"drop\" subcommand","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-08T10:23:33Z","receivedAt":"2026-06-08T10:23:55Z","isPatch":true,"body":"A common operation when editing the commit history is to drop a specific\ncommit from the history entirely, but this operation is not currently\ncovered by git-history(1).\n\nA couple of noteworthy bits:\n\n  - This is the first git-history(1) command that will ultimately result\n    in changes to both the index and the working tree. We thus have to\n    add logic to merge resulting changes into those.\n\n  - It is still not possible to replay merge commits, so this limitation\n    is inherited for the new \"drop\" command.\n\n  - For now we refuse to drop root commits. While we _can_ indeed drop\n    root commits in the general case, there are edge cases where the\n    resulting history would become completely empty. This is thus left\n    to a subsequent patch series.\n\nOther than that, most of the logic is rather straight-forward as we can\ncontinue to build on the preexisting logic in git-history(1) for most of\nthe part.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n Documentation/git-history.adoc |  38 ++-\n builtin/history.c              | 187 ++++++++++++++\n t/meson.build                  |   1 +\n t/t3454-history-drop.sh        | 537 +++++++++++++++++++++++++++++++++++++++++\n 4 files changed, 762 insertions(+), 1 deletion(-)\n\ndiff --git a/Documentation/git-history.adoc b/Documentation/git-history.adoc\nindex 2ba8121795..28b477cd37 100644\n--- a/Documentation/git-history.adoc\n+++ b/Documentation/git-history.adoc\n@@ -8,6 +8,7 @@ git-history - EXPERIMENTAL: Rewrite history\n SYNOPSIS\n --------\n [synopsis]\n+git history drop <commit> [--dry-run] [--update-refs=(branches|head)] [--empty=(drop|keep|abort)]\n git history fixup <commit> [--dry-run] [--update-refs=(branches|head)] [--reedit-message] [--empty=(drop|keep|abort)]\n git history reword <commit> [--dry-run] [--update-refs=(branches|head)]\n git history split <commit> [--dry-run] [--update-refs=(branches|head)] [--] [<pathspec>...]\n@@ -51,13 +52,28 @@ be stateful operations. The limitation can be lifted once (if) Git learns about\n first-class conflicts.\n \n When using `fixup` with `--empty=drop`, dropping the root commit is not yet\n-supported.\n+supported. Likewise, `drop` cannot remove the root commit or a merge commit.\n \n COMMANDS\n --------\n \n The following commands are available to rewrite history in different ways:\n \n+`drop <commit>`::\n+\tRemove the specified commit from the history. All descendants of the\n+\tcommit are replayed directly onto its parent.\n++\n+The root commit cannot be dropped as that may lead to edge cases where refs\n+end up with no commits anymore. Merge commits cannot be dropped either; see\n+LIMITATIONS.\n++\n+If `HEAD` points at a commit that is to be rewritten, the index and working\n+tree are updated to match the new `HEAD`. The command aborts before any\n+references are updated in case local modifications would be overwritten.\n++\n+If replaying any descendant would result in a conflict, the command aborts\n+with an error.\n+\n `fixup <commit>`::\n \tApply the currently staged changes to the specified commit. This is\n \tsimilar in nature to `git commit --fixup=<commit>` followed by `git\n@@ -170,6 +186,26 @@ The staged addition of `unrelated.txt` has been incorporated into the `first`\n commit. All descendant commits have been replayed on top of the rewritten\n history.\n \n+Drop a commit\n+~~~~~~~~~~~~~\n+\n+----------\n+$ git log --oneline\n+abc1234 (HEAD -> main) third\n+def5678 second\n+ghi9012 first\n+\n+$ git history drop 'main^{/second}'\n+\n+$ git log --oneline\n+jkl3456 (HEAD -> main) third\n+ghi9012 first\n+----------\n+\n+The `second` commit has been removed from the history, and `third` has been\n+replayed directly on top of `first`. All branches that pointed at the dropped\n+commit have been moved to its parent.\n+\n Split a commit\n ~~~~~~~~~~~~~~\n \ndiff --git a/builtin/history.c b/builtin/history.c\nindex 4fadf38c32..5c2cdc673b 100644\n--- a/builtin/history.c\n+++ b/builtin/history.c\n@@ -17,13 +17,17 @@\n #include \"read-cache.h\"\n #include \"refs.h\"\n #include \"replay.h\"\n+#include \"reset.h\"\n #include \"revision.h\"\n #include \"sequencer.h\"\n #include \"strvec.h\"\n #include \"tree.h\"\n+#include \"tree-walk.h\"\n #include \"unpack-trees.h\"\n #include \"wt-status.h\"\n \n+#define GIT_HISTORY_DROP_USAGE \\\n+\tN_(\"git history drop <commit> [--dry-run] [--update-refs=(branches|head)] [--empty=(drop|keep|abort)]\")\n #define GIT_HISTORY_FIXUP_USAGE \\\n \tN_(\"git history fixup <commit> [--dry-run] [--update-refs=(branches|head)] [--reedit-message] [--empty=(drop|keep|abort)]\")\n #define GIT_HISTORY_REWORD_USAGE \\\n@@ -1001,12 +1005,194 @@ static int cmd_history_split(int argc,\n \treturn ret;\n }\n \n+static int update_worktree(struct repository *repo,\n+\t\t\t   const struct commit *old_head,\n+\t\t\t   const struct commit *new_head,\n+\t\t\t   bool dry_run)\n+{\n+\tstruct reset_head_opts opts = {\n+\t\t.oid_from = &old_head->object.oid,\n+\t\t.oid = &new_head->object.oid,\n+\t\t.flags = RESET_HEAD_SKIP_REF_UPDATES,\n+\t};\n+\tif (dry_run)\n+\t\topts.flags |= RESET_HEAD_DRY_RUN;\n+\treturn reset_head(repo, &opts);\n+}\n+\n+static int find_head_tree_change(struct repository *repo,\n+\t\t\t\t const struct replay_result *result,\n+\t\t\t\t struct commit **old_head,\n+\t\t\t\t struct commit **new_head,\n+\t\t\t\t bool *changed)\n+{\n+\tconst struct replay_ref_update *head_update = NULL;\n+\tstruct commit *old_head_commit, *new_head_commit;\n+\tstruct tree *old_head_tree, *new_head_tree;\n+\tconst char *head_target;\n+\tint head_flags;\n+\n+\t*changed = false;\n+\n+\thead_target = refs_resolve_ref_unsafe(get_main_ref_store(repo),\n+\t\t\t\t\t      \"HEAD\", RESOLVE_REF_NO_RECURSE,\n+\t\t\t\t\t      NULL, &head_flags);\n+\tif (!head_target)\n+\t\treturn error(_(\"cannot look up HEAD\"));\n+\tif (!(head_flags & REF_ISSYMREF))\n+\t\thead_target = \"HEAD\";\n+\n+\tfor (size_t i = 0; i < result->updates_nr; i++) {\n+\t\tif (!strcmp(result->updates[i].refname, head_target)) {\n+\t\t\thead_update = &result->updates[i];\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\n+\tif (!head_update)\n+\t\treturn 0;\n+\n+\told_head_commit = lookup_commit_reference(repo, &head_update->old_oid);\n+\tnew_head_commit = lookup_commit_reference(repo, &head_update->new_oid);\n+\tif (!old_head_commit || !new_head_commit)\n+\t\treturn error(_(\"cannot resolve HEAD commit\"));\n+\n+\told_head_tree = repo_get_commit_tree(repo, old_head_commit);\n+\tnew_head_tree = repo_get_commit_tree(repo, new_head_commit);\n+\tif (!old_head_tree || !new_head_tree)\n+\t\treturn error(_(\"cannot resolve tree for HEAD\"));\n+\n+\tif (oideq(&old_head_tree->object.oid, &new_head_tree->object.oid))\n+\t\treturn 0;\n+\n+\t*old_head = old_head_commit;\n+\t*new_head = new_head_commit;\n+\t*changed = true;\n+\n+\treturn 0;\n+}\n+\n+static int cmd_history_drop(int argc,\n+\t\t\t    const char **argv,\n+\t\t\t    const char *prefix,\n+\t\t\t    struct repository *repo)\n+{\n+\tconst char * const usage[] = {\n+\t\tGIT_HISTORY_DROP_USAGE,\n+\t\tNULL,\n+\t};\n+\tenum replay_empty_commit_action empty = REPLAY_EMPTY_COMMIT_DROP;\n+\tenum ref_action action = REF_ACTION_DEFAULT;\n+\tint dry_run = 0;\n+\tstruct option options[] = {\n+\t\tOPT_CALLBACK_F(0, \"update-refs\", &action, \"(branches|head)\",\n+\t\t\t       N_(\"control which refs should be updated\"),\n+\t\t\t       PARSE_OPT_NONEG, parse_ref_action),\n+\t\tOPT_BOOL('n', \"dry-run\", &dry_run,\n+\t\t\t N_(\"perform a dry-run without updating any refs\")),\n+\t\tOPT_CALLBACK_F(0, \"empty\", &empty, \"(drop|keep|abort)\",\n+\t\t\t       N_(\"how to handle descendants that become empty\"),\n+\t\t\t       PARSE_OPT_NONEG, parse_opt_empty),\n+\t\tOPT_END(),\n+\t};\n+\tstruct strbuf reflog_msg = STRBUF_INIT;\n+\tstruct commit *original, *rewritten;\n+\tstruct rev_info revs = { 0 };\n+\tstruct replay_result result = { 0 };\n+\tstruct commit *old_head, *new_head;\n+\tbool head_moves = false;\n+\tint ret;\n+\n+\targc = parse_options(argc, argv, prefix, options, usage, 0);\n+\tif (argc != 1) {\n+\t\tret = error(_(\"command expects a single revision\"));\n+\t\tgoto out;\n+\t}\n+\trepo_config(repo, git_default_config, NULL);\n+\n+\tif (action == REF_ACTION_DEFAULT)\n+\t\taction = REF_ACTION_BRANCHES;\n+\n+\toriginal = lookup_commit_reference_by_name(argv[0]);\n+\tif (!original) {\n+\t\tret = error(_(\"commit cannot be found: %s\"), argv[0]);\n+\t\tgoto out;\n+\t}\n+\n+\tif (!original->parents) {\n+\t\tret = error(_(\"cannot drop root commit %s: \"\n+\t\t\t      \"it has no parent to replay onto\"),\n+\t\t\t    argv[0]);\n+\t\tgoto out;\n+\t} else if (original->parents->next) {\n+\t\tret = error(_(\"cannot drop merge commit: %s\"), argv[0]);\n+\t\tgoto out;\n+\t}\n+\n+\tret = setup_revwalk(repo, action, original, &revs);\n+\tif (ret)\n+\t\tgoto out;\n+\n+\trewritten = original->parents->item;\n+\n+\tret = compute_pending_ref_updates(&revs, action, original, rewritten,\n+\t\t\t\t\t  empty, &result);\n+\tif (ret) {\n+\t\tret = error(_(\"failed replaying descendants\"));\n+\t\tgoto out;\n+\t}\n+\n+\t/*\n+\t * If HEAD will move as a result of the rewrite then we'll have to\n+\t * merge in the changes into the worktree and index. This merge can of\n+\t * course conflict, which will cause the whole operation to abort.\n+\t *\n+\t * If we had already updated the refs at that point then we'd have an\n+\t * inconsistent repository state. So we first perform a dry-run merge\n+\t * here before updating refs.\n+\t */\n+\tif (!is_bare_repository()) {\n+\t\tret = find_head_tree_change(repo, &result, &old_head,\n+\t\t\t\t\t    &new_head, &head_moves);\n+\t\tif (ret < 0)\n+\t\t\tgoto out;\n+\n+\t\tif (head_moves && update_worktree(repo, old_head, new_head, true) < 0) {\n+\t\t\tret = error(_(\"dropping this commit would \"\n+\t\t\t\t      \"overwrite local changes; aborting\"));\n+\t\t\tgoto out;\n+\t\t}\n+\t}\n+\n+\tstrbuf_addf(&reflog_msg, \"drop: dropping %s\", argv[0]);\n+\tret = apply_pending_ref_updates(repo, &result, reflog_msg.buf, dry_run);\n+\tif (ret < 0) {\n+\t\tret = error(_(\"failed to update references\"));\n+\t\tgoto out;\n+\t}\n+\n+\tif (!dry_run && head_moves && update_worktree(repo, old_head, new_head, false) < 0) {\n+\t\tret = error(_(\"could not update working tree to new commit %s\"),\n+\t\t\t    oid_to_hex(&new_head->object.oid));\n+\t\tgoto out;\n+\t}\n+\n+\tret = 0;\n+\n+out:\n+\treplay_result_release(&result);\n+\tstrbuf_release(&reflog_msg);\n+\trelease_revisions(&revs);\n+\treturn ret;\n+}\n+\n int cmd_history(int argc,\n \t\tconst char **argv,\n \t\tconst char *prefix,\n \t\tstruct repository *repo)\n {\n \tconst char * const usage[] = {\n+\t\tGIT_HISTORY_DROP_USAGE,\n \t\tGIT_HISTORY_FIXUP_USAGE,\n \t\tGIT_HISTORY_REWORD_USAGE,\n \t\tGIT_HISTORY_SPLIT_USAGE,\n@@ -1014,6 +1200,7 @@ int cmd_history(int argc,\n \t};\n \tparse_opt_subcommand_fn *fn = NULL;\n \tstruct option options[] = {\n+\t\tOPT_SUBCOMMAND(\"drop\", &fn, cmd_history_drop),\n \t\tOPT_SUBCOMMAND(\"fixup\", &fn, cmd_history_fixup),\n \t\tOPT_SUBCOMMAND(\"reword\", &fn, cmd_history_reword),\n \t\tOPT_SUBCOMMAND(\"split\", &fn, cmd_history_split),\ndiff --git a/t/meson.build b/t/meson.build\nindex 2af8d01279..d5e71056b2 100644\n--- a/t/meson.build\n+++ b/t/meson.build\n@@ -399,6 +399,7 @@ integration_tests = [\n   't3451-history-reword.sh',\n   't3452-history-split.sh',\n   't3453-history-fixup.sh',\n+  't3454-history-drop.sh',\n   't3500-cherry.sh',\n   't3501-revert-cherry-pick.sh',\n   't3502-cherry-pick-merge.sh',\ndiff --git a/t/t3454-history-drop.sh b/t/t3454-history-drop.sh\nnew file mode 100755\nindex 0000000000..0f33247212\n--- /dev/null\n+++ b/t/t3454-history-drop.sh\n@@ -0,0 +1,537 @@\n+#!/bin/sh\n+\n+test_description='tests for git-history drop subcommand'\n+\n+. ./test-lib.sh\n+. \"$TEST_DIRECTORY/lib-log-graph.sh\"\n+\n+expect_graph () {\n+\tcat >expect &&\n+\tlib_test_cmp_graph --format=%s \"$@\"\n+}\n+\n+expect_log () {\n+\tgit log --format=\"%s\" \"$@\" >actual &&\n+\tcat >expect &&\n+\ttest_cmp expect actual\n+}\n+\n+test_expect_success 'errors on missing commit argument' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit initial &&\n+\t\ttest_must_fail git history drop 2>err &&\n+\t\ttest_grep \"command expects a single revision\" err\n+\t)\n+'\n+\n+test_expect_success 'errors on too many arguments' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit initial &&\n+\t\ttest_must_fail git history drop HEAD HEAD 2>err &&\n+\t\ttest_grep \"command expects a single revision\" err\n+\t)\n+'\n+\n+test_expect_success 'errors on unknown revision' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit initial &&\n+\t\ttest_must_fail git history drop does-not-exist 2>err &&\n+\t\ttest_grep \"commit cannot be found: does-not-exist\" err\n+\t)\n+'\n+\n+test_expect_success 'errors with invalid --empty= value' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit initial &&\n+\t\ttest_commit second &&\n+\t\ttest_must_fail git history drop --empty=bogus HEAD 2>err &&\n+\t\ttest_grep \"unrecognized.*--empty.*bogus\" err\n+\t)\n+'\n+\n+test_expect_success 'drops a commit in the middle and replays descendants' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\n+\t\tgit symbolic-ref HEAD >expect &&\n+\t\tgit history drop HEAD~ &&\n+\t\tgit symbolic-ref HEAD >actual &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\texpect_log <<-\\EOF &&\n+\t\tthird\n+\t\tfirst\n+\t\tEOF\n+\n+\t\ttest_must_fail git show HEAD:second.t &&\n+\t\ttest_path_is_missing second.t &&\n+\n+\t\tgit reflog >reflog &&\n+\t\ttest_grep \"drop: dropping HEAD~\" reflog\n+\t)\n+'\n+\n+test_expect_success 'drops the HEAD commit' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\n+\t\tgit history drop HEAD &&\n+\n+\t\texpect_log <<-\\EOF\n+\t\tfirst\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'drops a commit on detached HEAD' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\t\tgit checkout --detach HEAD &&\n+\n+\t\tgit history drop HEAD~ &&\n+\n+\t\texpect_log <<-\\EOF\n+\t\tthird\n+\t\tfirst\n+\t\tEOF\n+\t)\n+'\n+\n+# Note: in this case it would actually be fine to drop the root commit, as we\n+# do have a descendant commit, and no reference points to the root commit\n+# directly. So this is something that we may relax eventually.\n+test_expect_success 'refuses to drop the root commit' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\n+\t\ttest_must_fail git history drop HEAD~ 2>err &&\n+\t\ttest_grep \"cannot drop root commit\" err\n+\t)\n+'\n+\n+# In contrast to the above case, we actually don't want to drop the root commit\n+# here as that would cause us to end up with an empty commit graph.\n+test_expect_success 'refuses to drop the root commit when branch becomes empty' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\n+\t\ttest_must_fail git history drop HEAD 2>err &&\n+\t\ttest_grep \"cannot drop root commit\" err\n+\t)\n+'\n+\n+test_expect_success 'refuses to drop a merge commit' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\tgit branch branch &&\n+\t\ttest_commit ours &&\n+\t\tgit switch branch &&\n+\t\ttest_commit theirs &&\n+\t\tgit switch - &&\n+\t\tgit merge theirs &&\n+\n+\t\ttest_must_fail git history drop HEAD 2>err &&\n+\t\ttest_grep \"cannot drop merge commit\" err\n+\t)\n+'\n+\n+test_expect_success 'refuses when descendants contain a merge commit' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\ttest_commit middle &&\n+\t\tgit branch branch &&\n+\t\ttest_commit ours &&\n+\t\tgit switch branch &&\n+\t\ttest_commit theirs &&\n+\t\tgit switch - &&\n+\t\tgit merge theirs &&\n+\n+\t\ttest_must_fail git history drop middle 2>err &&\n+\t\ttest_grep \"replaying merge commits is not supported yet\" err\n+\t)\n+'\n+\n+test_expect_success 'works in a bare repository' '\n+\ttest_when_finished \"rm -rf repo repo.git\" &&\n+\n+\tgit init repo &&\n+\ttest_commit -C repo first &&\n+\ttest_commit -C repo second &&\n+\ttest_commit -C repo third &&\n+\n+\tgit clone --bare repo repo.git &&\n+\t(\n+\t\tcd repo.git &&\n+\n+\t\tgit history drop HEAD~ &&\n+\t\texpect_log <<-\\EOF\n+\t\tthird\n+\t\tfirst\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'updates branches on other lines of descent' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\ttest_commit target &&\n+\t\tgit branch theirs &&\n+\t\ttest_commit ours &&\n+\t\tgit switch theirs &&\n+\t\ttest_commit theirs &&\n+\n+\t\texpect_graph --branches <<-\\EOF &&\n+\t\t* theirs\n+\t\t| * ours\n+\t\t|/\n+\t\t* target\n+\t\t* base\n+\t\tEOF\n+\n+\t\tgit history drop target &&\n+\n+\t\texpect_graph --branches <<-\\EOF\n+\t\t* ours\n+\t\t| * theirs\n+\t\t|/\n+\t\t* base\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'moves branch pointing at dropped commit to its parent' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo --initial-branch=main &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\tgit branch points-at-second &&\n+\t\ttest_commit third &&\n+\n+\t\tgit rev-parse first >expect &&\n+\t\tgit history drop second &&\n+\t\tgit rev-parse points-at-second >actual &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\texpect_log --format=\"%s %D\" --branches <<-\\EOF\n+\t\tthird HEAD -> main\n+\t\tfirst tag: first, points-at-second\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--dry-run prints ref updates without modifying repo' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo --initial-branch=main &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\tgit branch branch &&\n+\t\ttest_commit middle &&\n+\t\ttest_commit ours &&\n+\t\tgit switch branch &&\n+\t\ttest_commit theirs &&\n+\n+\t\tgit refs list >refs-expect &&\n+\t\tgit history drop --dry-run main~ >updates &&\n+\t\tgit refs list >refs-actual &&\n+\t\ttest_cmp refs-expect refs-actual &&\n+\t\ttest_grep \"update refs/heads/main\" updates &&\n+\n+\t\tgit update-ref --stdin <updates &&\n+\t\texpect_log main <<-\\EOF\n+\t\tours\n+\t\tbase\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--dry-run detects conflicts with modified working tree' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo --initial-branch=main &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second modify-me &&\n+\t\techo modified >modify-me &&\n+\n+\t\tgit refs list >refs-expect &&\n+\t\tgit diff >diff-expect &&\n+\t\ttest_must_fail git history drop --dry-run HEAD 2>err &&\n+\t\ttest_grep \"dropping this commit would overwrite local changes\" err &&\n+\t\tgit diff >diff-actual &&\n+\t\tgit refs list >refs-actual &&\n+\n+\t\ttest_cmp diff-expect diff-actual &&\n+\t\ttest_cmp refs-expect refs-actual\n+\t)\n+'\n+\n+test_expect_success '--update-refs=head updates only HEAD' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo --initial-branch=main &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\ttest_commit target &&\n+\t\tgit branch theirs &&\n+\t\ttest_commit ours &&\n+\t\tgit switch theirs &&\n+\t\ttest_commit theirs &&\n+\n+\t\t# When told to update HEAD only, the command refuses to\n+\t\t# rewrite commits that are not an ancestor of HEAD.\n+\t\ttest_must_fail git history drop --update-refs=head main 2>err &&\n+\t\ttest_grep \"rewritten commit must be an ancestor of HEAD\" err &&\n+\n+\t\texpect_graph --branches <<-\\EOF &&\n+\t\t* theirs\n+\t\t| * ours\n+\t\t|/\n+\t\t* target\n+\t\t* base\n+\t\tEOF\n+\n+\t\tgit switch main &&\n+\t\tgit history drop --update-refs=head target &&\n+\n+\t\texpect_graph --branches <<-\\EOF\n+\t\t* ours\n+\t\t| * theirs\n+\t\t| * target\n+\t\t|/\n+\t\t* base\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'conflict with replayed commit aborts cleanly' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\ttest_commit conflict-a file &&\n+\t\ttest_commit conflict-b file &&\n+\n+\t\tgit refs list >refs-expect &&\n+\t\ttest_must_fail git history drop HEAD~ 2>err &&\n+\t\ttest_grep \"failed replaying descendants\" err &&\n+\t\tgit refs list >refs-actual &&\n+\t\ttest_cmp refs-expect refs-actual\n+\t)\n+'\n+\n+# Build a history where a descendant of the drop target reverts the change\n+# introduced by the drop target. After dropping, the descendant's diff applies\n+# against a tree that already lacks the change, so it becomes empty.\n+setup_empty_descendant_repo () {\n+\tgit init \"$1\" &&\n+\t(\n+\t\tcd \"$1\" &&\n+\t\techo C1 >file &&\n+\t\tgit add file &&\n+\t\tgit commit -m \"base\" &&\n+\t\tgit tag base &&\n+\t\techo C2 >file &&\n+\t\tgit add file &&\n+\t\tgit commit -m \"drop-me\" &&\n+\t\tgit tag drop-me &&\n+\t\ttest_commit middle &&\n+\t\techo C1 >file &&\n+\t\tgit add file &&\n+\t\tgit commit -m \"revert-drop-me\" &&\n+\t\tgit tag revert-drop-me\n+\t)\n+}\n+\n+test_expect_success '--empty=drop drops descendants that become empty' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tsetup_empty_descendant_repo repo &&\n+\t(\n+\t\tcd repo &&\n+\n+\t\tgit history drop --empty=drop drop-me &&\n+\n+\t\texpect_log <<-\\EOF\n+\t\tmiddle\n+\t\tbase\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--empty=keep keeps descendants that become empty' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tsetup_empty_descendant_repo repo &&\n+\t(\n+\t\tcd repo &&\n+\n+\t\tgit history drop --empty=keep drop-me &&\n+\n+\t\texpect_log <<-\\EOF &&\n+\t\trevert-drop-me\n+\t\tmiddle\n+\t\tbase\n+\t\tEOF\n+\t\tgit diff HEAD~ HEAD >diff &&\n+\t\ttest_must_be_empty diff\n+\t)\n+'\n+\n+test_expect_success '--empty=abort errors out when a descendant becomes empty' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tsetup_empty_descendant_repo repo &&\n+\t(\n+\t\tcd repo &&\n+\n+\t\ttest_must_fail git history drop --empty=abort drop-me 2>err &&\n+\t\ttest_grep \"became empty after replay\" err\n+\t)\n+'\n+\n+test_expect_success 'updates index and worktree when HEAD moves' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\n+\t\tgit history drop second &&\n+\n+\t\t# Worktree should no longer contain second.t.\n+\t\ttest_path_is_missing second.t &&\n+\t\ttest_path_is_file first.t &&\n+\t\ttest_path_is_file third.t &&\n+\n+\t\t# Index and worktree should both match the new HEAD.\n+\t\tgit status --porcelain --untracked-files=no >status &&\n+\t\ttest_must_be_empty status\n+\t)\n+'\n+\n+test_expect_success 'updates worktree when dropping HEAD itself' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\n+\t\tgit history drop HEAD &&\n+\n+\t\ttest_path_is_missing second.t &&\n+\t\ttest_path_is_file first.t &&\n+\n+\t\tgit status --porcelain --untracked-files=no >status &&\n+\t\ttest_must_be_empty status\n+\t)\n+'\n+\n+test_expect_success 'preserves unrelated unstaged modifications' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\techo first-content >unrelated.txt &&\n+\t\tgit add unrelated.txt &&\n+\t\tgit commit -m \"add unrelated\" &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\n+\t\techo locally-modified >unrelated.txt &&\n+\n+\t\tgit diff >diff-expect &&\n+\t\tgit history drop second &&\n+\t\tgit diff >diff-actual &&\n+\t\ttest_cmp diff-expect diff-actual &&\n+\t\ttest_path_is_missing second.t\n+\t)\n+'\n+\n+test_expect_success 'preserves unrelated staged changes' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\techo first-content >unrelated.txt &&\n+\t\tgit add unrelated.txt &&\n+\t\tgit commit -m \"add unrelated\" &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\n+\t\techo staged-change >unrelated.txt &&\n+\t\tgit add unrelated.txt &&\n+\n+\t\tgit diff --cached >diff-expect &&\n+\t\tgit history drop second &&\n+\t\tgit diff --cached >diff-actual &&\n+\t\ttest_cmp diff-expect diff-actual &&\n+\t\ttest_path_is_missing second.t\n+\t)\n+'\n+\n+test_expect_success 'aborts when local modifications would be overwritten' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\ttest_commit conflict &&\n+\n+\t\techo local-edit >conflict.t &&\n+\t\tgit diff >diff-expect &&\n+\t\ttest_must_fail git history drop HEAD 2>err &&\n+\t\ttest_grep \"would overwrite local changes\" err &&\n+\t\tgit diff >diff-actual &&\n+\t\ttest_cmp diff-expect diff-actual\n+\t)\n+'\n+\n+test_done\n\n-- \n2.54.0.1136.gdb2ca164c4.dirty\n\n"},{"id":"545043","messageId":"b9055227-f6e8-452d-928c-d944ad840f31@gmail.com","threadId":"65726","inReplyTo":"aiaI6thDj8y_EekG@pks.im","subject":"Re: [PATCH v2 5/9] reset: introduce ability to skip reference updates","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-06-09T10:03:24Z","receivedAt":"2026-06-09T10:03:28Z","isPatch":true,"body":"Hi Patrick\n\nOn 08/06/2026 10:18, Patrick Steinhardt wrote:\n> On Mon, Jun 08, 2026 at 11:14:08AM +0200, Patrick Steinhardt wrote:\n>> On Fri, Jun 05, 2026 at 04:12:42PM +0100, Phillip Wood wrote:\n>>> Hi Patrick\n>>>\n>>> On 03/06/2026 17:14, Patrick Steinhardt wrote:\n>>>> In a subsequent commit we'll introduce a new caller to `reset_head()`\n>>>> that really only wants to update the index and working tree, without\n>>>> updating any references. Introduce a new flag that lets the caller\n>>>> perform this operation.\n>>>\n>>> We already have a flag to update ORIG_HEAD so would it make more sense to\n>>> have a flag to update HEAD, rather than adding a flag to disable the\n>>> updates? It would mean updating the existing callers but I think it is a\n>>> clearer api and it avoids the pitfall of\n>>>\n>>> \tRESET_HEAD_ORIG_HEAD | RESET_HEAD_SKIP_REF_UPDATES\n>>\n>> Hm. The question is whether it's sensible to have\n>> `!RESET_HEAD_UPDATE_HEAD && RESET_HEAD_UPDATE_ORIG_HEAD`. That feels\n>> like a somewhat weird request, too, and we'd have to introduce extra\n>> logic to make that combination work.\n\nAs there are no users at the moment we could make that a BUG() and \nimplement it later if required. At least that way we're not introducing \nconflicting flags, we're just not implementing setting a certain \ncombination of refs.\n\n>>> I wonder about the function name as well if we make updating HEAD optional\n>>> then what does reset_head() mean? Maybe we should rename it something along\n>>> the lines of reset_worktree() or update_working_copy()? I'm not really sure\n>>> what a good name would be.\n>>\n>> That's a good point, the name does get somewhat awkward. I think we\n>> should keep \"reset\" in there, but `reset_worktree()` to me reads as it\n>> if was rather related to git-worktree(1) than anything else. Maybe\n>> `reset_working_tree()`?\n> \n> I think I'll skip these changes for the next iteration for now. The\n> patch series has already exploded quite a bit in its scope due to the\n> refactorings of `reset_head()`, so I'd prefer to maybe do such changes\n> as a follow up.\n\nRenaming the function can certainly wait. I'd quite like to sort out the \nflags though as the new flag is being added in this series.\n\nThanks\n\nPhillip\n> \n> Let me know in case you feel strongly about this though. Thanks!\n> \n> Patrick\n\n"},{"id":"545107","messageId":"aikS4huB4Qe9tEa4@pks.im","threadId":"65726","inReplyTo":"b9055227-f6e8-452d-928c-d944ad840f31@gmail.com","subject":"Re: [PATCH v2 5/9] reset: introduce ability to skip reference updates","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-10T07:31:46Z","receivedAt":"2026-06-10T07:31:52Z","isPatch":true,"body":"On Tue, Jun 09, 2026 at 11:03:24AM +0100, Phillip Wood wrote:\n> Hi Patrick\n> \n> On 08/06/2026 10:18, Patrick Steinhardt wrote:\n> > On Mon, Jun 08, 2026 at 11:14:08AM +0200, Patrick Steinhardt wrote:\n> > > On Fri, Jun 05, 2026 at 04:12:42PM +0100, Phillip Wood wrote:\n> > > > Hi Patrick\n> > > > \n> > > > On 03/06/2026 17:14, Patrick Steinhardt wrote:\n> > > > > In a subsequent commit we'll introduce a new caller to `reset_head()`\n> > > > > that really only wants to update the index and working tree, without\n> > > > > updating any references. Introduce a new flag that lets the caller\n> > > > > perform this operation.\n> > > > \n> > > > We already have a flag to update ORIG_HEAD so would it make more sense to\n> > > > have a flag to update HEAD, rather than adding a flag to disable the\n> > > > updates? It would mean updating the existing callers but I think it is a\n> > > > clearer api and it avoids the pitfall of\n> > > > \n> > > > \tRESET_HEAD_ORIG_HEAD | RESET_HEAD_SKIP_REF_UPDATES\n> > > \n> > > Hm. The question is whether it's sensible to have\n> > > `!RESET_HEAD_UPDATE_HEAD && RESET_HEAD_UPDATE_ORIG_HEAD`. That feels\n> > > like a somewhat weird request, too, and we'd have to introduce extra\n> > > logic to make that combination work.\n> \n> As there are no users at the moment we could make that a BUG() and implement\n> it later if required. At least that way we're not introducing conflicting\n> flags, we're just not implementing setting a certain combination of refs.\n\nHm, okay, I'll have a look at this.\n\n> > > > I wonder about the function name as well if we make updating HEAD optional\n> > > > then what does reset_head() mean? Maybe we should rename it something along\n> > > > the lines of reset_worktree() or update_working_copy()? I'm not really sure\n> > > > what a good name would be.\n> > > \n> > > That's a good point, the name does get somewhat awkward. I think we\n> > > should keep \"reset\" in there, but `reset_worktree()` to me reads as it\n> > > if was rather related to git-worktree(1) than anything else. Maybe\n> > > `reset_working_tree()`?\n> > \n> > I think I'll skip these changes for the next iteration for now. The\n> > patch series has already exploded quite a bit in its scope due to the\n> > refactorings of `reset_head()`, so I'd prefer to maybe do such changes\n> > as a follow up.\n> \n> Renaming the function can certainly wait. I'd quite like to sort out the\n> flags though as the new flag is being added in this series.\n\nIf we introduce RESET_HEAD_UPDATE_HEAD I'd also like to rename so that\nthere's not a silent new caller where we don't have the new flag.\n\nPatrick\n"},{"id":"545110","messageId":"20260610-b4-pks-history-drop-v4-0-70d5f0ae8c25@pks.im","threadId":"65726","inReplyTo":"20260601-b4-pks-history-drop-v1-0-643e32340d55@pks.im","subject":"[PATCH v4 00/10] builtin/history: introduce \"drop\" subcommand","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-10T08:52:20Z","receivedAt":"2026-06-10T08:52:36Z","isPatch":true,"body":"Hi,\n\nthis small patch series introduces the new \"drop\" subcommand for\ngit-history(1). As a reader might guess, the command does exactly that:\ngiven a commit, it will drop that commit from the commit history and\nreplay descendant branches on top of it.\n\nChanges in v4:\n  - Remove the `SKIP_REF_UPDATES` flag in favor of a new `UPDATE_HEAD`\n    flag, as suggested by Phillip.\n  - Rename `reset_head()` to `reset_working_tree()`. This better matches\n    the new scope of the function, and it helps us to catch any\n    in-flight patches that would now have to set the `UPDATE_HEAD` flag.\n  - Link to v3: https://patch.msgid.link/20260608-b4-pks-history-drop-v3-0-84ca8e43e937@pks.im\n\nChanges in v3:\n  - Fix commit message typos.\n  - Make `update_orig_head` and `skip_ref_updates` mutually exclusive.\n  - Use fancy revisions to specify the commit to drop in the example\n    section.\n  - Detect conflicting changes in the index/working tree in dry-run\n    mode.\n  - Consistently use a subshell.\n  - Rename `RESET_HEAD_ORIG_HEAD` to `RESET_HEAD_UPDATE_ORIG_HEAD`.\n  - \n  - Link to v2: https://patch.msgid.link/20260603-b4-pks-history-drop-v2-0-742cb5b5176d@pks.im\n\nChanges in v2:\n  - Reworked `update_worktree()` to use `reset_head()`, which required a\n    bunch of changes to `reset_head()`.\n  - Consistently mention the commit that cannot be dropped as part of\n    error messages.\n  - Adapt error message to not use backticks anymore.\n  - Drop redundant \"--graph\" flag in a test helper.\n  - Link to v1: https://patch.msgid.link/20260601-b4-pks-history-drop-v1-0-643e32340d55@pks.im\n\nThanks!\n\nPatrick\n\n---\nPatrick Steinhardt (10):\n      read-cache: split out function to drop unmerged entries to stage 0\n      reset: drop `USE_THE_REPOSITORY_VARIABLE`\n      reset: rename `reset_head()`\n      reset: modernize flags passed to `reset_working_tree()`\n      reset: introduce dry-run mode\n      reset: introduce ability to skip updating HEAD\n      reset: allow the caller to specify the current HEAD object\n      reset: stop assuming that the caller passes in a clean index\n      builtin/history: split handling of ref updates into two phases\n      builtin/history: implement \"drop\" subcommand\n\n Documentation/git-history.adoc |  38 ++-\n builtin/history.c              | 288 +++++++++++++++++++---\n builtin/rebase.c               |  41 ++--\n read-cache-ll.h                |   1 +\n read-cache.c                   |  12 +-\n reset.c                        |  99 +++++---\n reset.h                        |  51 ++--\n sequencer.c                    |  17 +-\n t/meson.build                  |   1 +\n t/t3454-history-drop.sh        | 537 +++++++++++++++++++++++++++++++++++++++++\n 10 files changed, 968 insertions(+), 117 deletions(-)\n\nRange-diff versus v3:\n\n 1:  e9291b8592 =  1:  63c1a10ca0 read-cache: split out function to drop unmerged entries to stage 0\n 2:  6ae06725ca =  2:  b83e2124e8 reset: drop `USE_THE_REPOSITORY_VARIABLE`\n 3:  53bf10c527 <  -:  ---------- reset: modernize flags passed to `reset_head()`\n -:  ---------- >  3:  0488effe71 reset: rename `reset_head()`\n -:  ---------- >  4:  364e71ee9c reset: modernize flags passed to `reset_working_tree()`\n 4:  b8e5464d06 !  5:  0fa45bd263 reset: introduce dry-run mode\n    @@ Metadata\n      ## Commit message ##\n         reset: introduce dry-run mode\n     \n    -    In a subsequent commit we'll add another caller to `reset_head()` that\n    -    wants to perform a dry-run check of whether it would be possible to\n    +    In a subsequent commit we'll add another caller to `reset_working_tree()`\n    +    that wants to perform a dry-run check of whether it would be possible to\n         update the index and working tree when moving to a new commit. Introduce\n         a new flag that lets the caller perform this operation.\n     \n         Signed-off-by: Patrick Steinhardt <ps@pks.im>\n     \n      ## reset.c ##\n    -@@ reset.c: int reset_head(struct repository *r, const struct reset_head_opts *opts)\n    - \tunsigned reset_hard = opts->flags & RESET_HEAD_HARD;\n    - \tunsigned refs_only = opts->flags & RESET_HEAD_REFS_ONLY;\n    - \tunsigned update_orig_head = opts->flags & RESET_HEAD_UPDATE_ORIG_HEAD;\n    -+\tunsigned dry_run = opts->flags & RESET_HEAD_DRY_RUN;\n    +@@ reset.c: int reset_working_tree(struct repository *r,\n    + \tunsigned reset_hard = opts->flags & RESET_WORKING_TREE_HARD;\n    + \tunsigned refs_only = opts->flags & RESET_WORKING_TREE_REFS_ONLY;\n    + \tunsigned update_orig_head = opts->flags & RESET_WORKING_TREE_UPDATE_ORIG_HEAD;\n    ++\tunsigned dry_run = opts->flags & RESET_WORKING_TREE_DRY_RUN;\n      \tstruct object_id *head = NULL, head_oid;\n      \tstruct tree_desc desc[2] = { { NULL }, { NULL } };\n      \tstruct lock_file lock = LOCK_INIT;\n    @@ reset.c: int reset_head(struct repository *r, const struct reset_head_opts *opts\n      \tconst char *action;\n      \tint ret = 0, nr = 0;\n      \n    -@@ reset.c: int reset_head(struct repository *r, const struct reset_head_opts *opts)\n    +@@ reset.c: int reset_working_tree(struct repository *r,\n      \tif (opts->branch_msg && !opts->branch)\n      \t\tBUG(\"branch reflog message given without a branch\");\n      \n    @@ reset.c: int reset_head(struct repository *r, const struct reset_head_opts *opts\n      \t\tret = -1;\n      \t\tgoto leave_reset_head;\n      \t}\n    -@@ reset.c: int reset_head(struct repository *r, const struct reset_head_opts *opts)\n    +@@ reset.c: int reset_working_tree(struct repository *r,\n      \tif (!oid)\n      \t\toid = &head_oid;\n      \n    @@ reset.c: int reset_head(struct repository *r, const struct reset_head_opts *opts\n      \tunpack_tree_opts.merge = 1;\n      \tunpack_tree_opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */\n      \tunpack_tree_opts.skip_cache_tree_update = 1;\n    -@@ reset.c: int reset_head(struct repository *r, const struct reset_head_opts *opts)\n    +@@ reset.c: int reset_working_tree(struct repository *r,\n      \tif (reset_hard)\n      \t\tunpack_tree_opts.reset = UNPACK_RESET_PROTECT_UNTRACKED;\n      \n    @@ reset.c: int reset_head(struct repository *r, const struct reset_head_opts *opts\n      \tif (!reset_hard && !fill_tree_descriptor(r, &desc[nr++], &head_oid)) {\n      \t\tret = error(_(\"failed to find tree of %s\"),\n      \t\t\t    oid_to_hex(&head_oid));\n    -@@ reset.c: int reset_head(struct repository *r, const struct reset_head_opts *opts)\n    +@@ reset.c: int reset_working_tree(struct repository *r,\n      \t\tgoto leave_reset_head;\n      \t}\n      \n    @@ reset.c: int reset_head(struct repository *r, const struct reset_head_opts *opts\n      \ttree = repo_parse_tree_indirect(r, oid);\n      \tif (!tree) {\n      \t\tret = error(_(\"unable to read tree (%s)\"), oid_to_hex(oid));\n    -@@ reset.c: int reset_head(struct repository *r, const struct reset_head_opts *opts)\n    +@@ reset.c: int reset_working_tree(struct repository *r,\n      leave_reset_head:\n      \trollback_lock_file(&lock);\n      \tclear_unpack_trees_porcelain(&unpack_tree_opts);\n    @@ reset.c: int reset_head(struct repository *r, const struct reset_head_opts *opts\n      \treturn ret;\n     \n      ## reset.h ##\n    -@@ reset.h: enum reset_head_flags {\n    +@@ reset.h: enum reset_working_tree_flags {\n      \n      \t/* Update ORIG_HEAD as well as HEAD */\n    - \tRESET_HEAD_UPDATE_ORIG_HEAD = (1 << 4),\n    + \tRESET_WORKING_TREE_UPDATE_ORIG_HEAD = (1 << 4),\n     +\n     +\t/*\n     +\t * Perform a dry-run by performing the operation without updating\n     +\t * any user-visible state.\n     +\t */\n    -+\tRESET_HEAD_DRY_RUN = (1 << 5),\n    ++\tRESET_WORKING_TREE_DRY_RUN = (1 << 5),\n      };\n      \n    - struct reset_head_opts {\n    + struct reset_working_tree_options {\n 5:  bbb8b98f8e <  -:  ---------- reset: introduce ability to skip reference updates\n -:  ---------- >  6:  4f844adaa7 reset: introduce ability to skip updating HEAD\n 6:  24cccf728f !  7:  28e7dd039e reset: allow the caller to specify the current HEAD object\n    @@ Metadata\n      ## Commit message ##\n         reset: allow the caller to specify the current HEAD object\n     \n    -    When calling `reset_head()` we automatically derive the commit that the\n    -    callers wants to move from by reading the HEAD commit. Some callers may\n    -    already have resolved it, or they may want to move from a different\n    -    commit that doesn't match HEAD.\n    +    When calling `reset_working_tree()` we automatically derive the commit\n    +    that the callers wants to move from by reading the HEAD commit. Some\n    +    callers may already have resolved it, or they may want to move from a\n    +    different commit that doesn't match HEAD.\n     \n         Introduce a new `oid_from` option that lets the caller specify the\n         commit.\n    @@ Commit message\n         Signed-off-by: Patrick Steinhardt <ps@pks.im>\n     \n      ## reset.c ##\n    -@@ reset.c: int reset_head(struct repository *r, const struct reset_head_opts *opts)\n    +@@ reset.c: int reset_working_tree(struct repository *r,\n      \t\tgoto leave_reset_head;\n      \t}\n      \n    @@ reset.c: int reset_head(struct repository *r, const struct reset_head_opts *opts\n      \t\tret = error(_(\"could not determine HEAD revision\"));\n     \n      ## reset.h ##\n    -@@ reset.h: struct reset_head_opts {\n    +@@ reset.h: struct reset_working_tree_options {\n      \t * The commit to checkout/reset to. Defaults to HEAD.\n      \t */\n      \tconst struct object_id *oid;\n 7:  4b79437a50 !  8:  6b590da34e reset: stop assuming that the caller passes in a clean index\n    @@ Commit message\n         reset: stop assuming that the caller passes in a clean index\n     \n         In 652bd0211d (rebase: use 'skip_cache_tree_update' option, 2022-11-10),\n    -    we updated `reset_head()` to stop updating the index tree cache. This\n    -    was done as a performance optimization: the function is only called by\n    -    \"sequencer.c\" and \"rebase.c\", both of which assume a clean index before\n    -    they perform their operation, so we know that the end result will be a\n    -    clean index, too. Consequently, we can skip recomputing the cache as we\n    -    can instead use `prime_cache_tree()` directly.\n    +    we updated `reset_working_tree()` to stop updating the index tree cache.\n    +    This was done as a performance optimization: the function is only called\n    +    by \"sequencer.c\" and \"rebase.c\", both of which assume a clean index\n    +    before they perform their operation, so we know that the end result will\n    +    be a clean index, too. Consequently, we can skip recomputing the cache\n    +    as we can instead use `prime_cache_tree()` directly.\n     \n         In a subsequent commit we're about to add a new caller though where the\n         assumption doesn't hold anymore: the index may be dirty before calling\n    -    `reset_head()`, and consequently we cannot prime the cache with a given\n    -    tree anymore as the index and tree will mismatch.\n    +    `reset_working_tree()`, and consequently we cannot prime the cache with\n    +    a given tree anymore as the index and tree will mismatch.\n     \n         Adapt the logic so that we only skip the cache tree update in case we're\n         doing a hard reset. While we could introduce logic that only skips the\n    @@ Commit message\n         Signed-off-by: Patrick Steinhardt <ps@pks.im>\n     \n      ## reset.c ##\n    -@@ reset.c: int reset_head(struct repository *r, const struct reset_head_opts *opts)\n    +@@ reset.c: int reset_working_tree(struct repository *r,\n      \tunpack_tree_opts.dry_run = dry_run;\n      \tunpack_tree_opts.merge = 1;\n      \tunpack_tree_opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */\n    @@ reset.c: int reset_head(struct repository *r, const struct reset_head_opts *opts\n      \n      \tif (!reset_hard && !fill_tree_descriptor(r, &desc[nr++], &head_oid)) {\n      \t\tret = error(_(\"failed to find tree of %s\"),\n    -@@ reset.c: int reset_head(struct repository *r, const struct reset_head_opts *opts)\n    +@@ reset.c: int reset_working_tree(struct repository *r,\n      \t\tgoto leave_reset_head;\n      \t}\n      \n 8:  d01c7ffb29 =  9:  fddadbf87f builtin/history: split handling of ref updates into two phases\n 9:  c75925f7ed ! 10:  cb09216db3 builtin/history: implement \"drop\" subcommand\n    @@ builtin/history.c: static int cmd_history_split(int argc,\n     +\t\t\t   const struct commit *new_head,\n     +\t\t\t   bool dry_run)\n     +{\n    -+\tstruct reset_head_opts opts = {\n    ++\tstruct reset_working_tree_options opts = {\n     +\t\t.oid_from = &old_head->object.oid,\n     +\t\t.oid = &new_head->object.oid,\n    -+\t\t.flags = RESET_HEAD_SKIP_REF_UPDATES,\n     +\t};\n     +\tif (dry_run)\n    -+\t\topts.flags |= RESET_HEAD_DRY_RUN;\n    -+\treturn reset_head(repo, &opts);\n    ++\t\topts.flags |= RESET_WORKING_TREE_DRY_RUN;\n    ++\treturn reset_working_tree(repo, &opts);\n     +}\n     +\n     +static int find_head_tree_change(struct repository *repo,\n\n---\nbase-commit: 1666c1265231b0bc5f613fbbf3f0a9896cdef76e\nchange-id: 20260601-b4-pks-history-drop-28f6c6399e7b\n\n"},{"id":"545111","messageId":"20260610-b4-pks-history-drop-v4-1-70d5f0ae8c25@pks.im","threadId":"65726","inReplyTo":"20260610-b4-pks-history-drop-v4-0-70d5f0ae8c25@pks.im","subject":"[PATCH v4 01/10] read-cache: split out function to drop unmerged entries to stage 0","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-10T08:52:21Z","receivedAt":"2026-06-10T08:52:38Z","isPatch":true,"body":"In `repo_read_index_unmerged()` we read the index and then drop any\nunmerged entries to stage 0. In a subsequent commit we'll want to\nperform this operation on arbitrary indexes, not only the one of the\ngiven repository.\n\nPrepare for this by splitting out the functionality into a new function\nthat can act on an arbitrary index.\n\nWhile at it, fix a signedness mismatch when iterating through the index\ncache entries.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n read-cache-ll.h |  1 +\n read-cache.c    | 12 +++++++-----\n 2 files changed, 8 insertions(+), 5 deletions(-)\n\ndiff --git a/read-cache-ll.h b/read-cache-ll.h\nindex 2c8b4b21b1..71b87615eb 100644\n--- a/read-cache-ll.h\n+++ b/read-cache-ll.h\n@@ -309,6 +309,7 @@ int write_locked_index(struct index_state *, struct lock_file *lock, unsigned fl\n void discard_index(struct index_state *);\n void move_index_extensions(struct index_state *dst, struct index_state *src);\n int unmerged_index(const struct index_state *);\n+int index_state_unmerged_to_stage0(struct index_state *istate);\n \n /**\n  * Returns 1 if istate differs from tree, 0 otherwise.  If tree is NULL,\ndiff --git a/read-cache.c b/read-cache.c\nindex 21829102ae..799a5bc719 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -3403,13 +3403,15 @@ int write_locked_index(struct index_state *istate, struct lock_file *lock,\n  */\n int repo_read_index_unmerged(struct repository *repo)\n {\n-\tstruct index_state *istate;\n-\tint i;\n+\trepo_read_index(repo);\n+\treturn index_state_unmerged_to_stage0(repo->index);\n+}\n+\n+int index_state_unmerged_to_stage0(struct index_state *istate)\n+{\n \tint unmerged = 0;\n \n-\trepo_read_index(repo);\n-\tistate = repo->index;\n-\tfor (i = 0; i < istate->cache_nr; i++) {\n+\tfor (unsigned int i = 0; i < istate->cache_nr; i++) {\n \t\tstruct cache_entry *ce = istate->cache[i];\n \t\tstruct cache_entry *new_ce;\n \t\tint len;\n\n-- \n2.54.0.1189.g8c84645362.dirty\n\n"},{"id":"545112","messageId":"20260610-b4-pks-history-drop-v4-2-70d5f0ae8c25@pks.im","threadId":"65726","inReplyTo":"20260610-b4-pks-history-drop-v4-0-70d5f0ae8c25@pks.im","subject":"[PATCH v4 02/10] reset: drop `USE_THE_REPOSITORY_VARIABLE`","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-10T08:52:22Z","receivedAt":"2026-06-10T08:52:39Z","isPatch":true,"body":"In \"reset.c\" we still have references to `the_repository`, even though\nthe only entry point into the file already receives a repository as\nparameter.\n\nUpdate all uses of `the_repository` to instead use the passed-in repo\nand drop `USE_THE_REPOSITORY_VARIABLE`.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reset.c | 27 +++++++++++++--------------\n 1 file changed, 13 insertions(+), 14 deletions(-)\n\ndiff --git a/reset.c b/reset.c\nindex 46e30e6394..3b3cb74dab 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -1,5 +1,3 @@\n-#define USE_THE_REPOSITORY_VARIABLE\n-\n #include \"git-compat-util.h\"\n #include \"cache-tree.h\"\n #include \"gettext.h\"\n@@ -13,7 +11,8 @@\n #include \"unpack-trees.h\"\n #include \"hook.h\"\n \n-static int update_refs(const struct reset_head_opts *opts,\n+static int update_refs(struct repository *repo,\n+\t\t       const struct reset_head_opts *opts,\n \t\t       const struct object_id *oid,\n \t\t       const struct object_id *head)\n {\n@@ -42,19 +41,19 @@ static int update_refs(const struct reset_head_opts *opts,\n \tprefix_len = msg.len;\n \n \tif (update_orig_head) {\n-\t\tif (!repo_get_oid(the_repository, \"ORIG_HEAD\", &oid_old_orig))\n+\t\tif (!repo_get_oid(repo, \"ORIG_HEAD\", &oid_old_orig))\n \t\t\told_orig = &oid_old_orig;\n \t\tif (head) {\n \t\t\tif (!reflog_orig_head) {\n \t\t\t\tstrbuf_addstr(&msg, \"updating ORIG_HEAD\");\n \t\t\t\treflog_orig_head = msg.buf;\n \t\t\t}\n-\t\t\trefs_update_ref(get_main_ref_store(the_repository),\n+\t\t\trefs_update_ref(get_main_ref_store(repo),\n \t\t\t\t\treflog_orig_head, \"ORIG_HEAD\",\n \t\t\t\t\torig_head ? orig_head : head,\n \t\t\t\t\told_orig, 0, UPDATE_REFS_MSG_ON_ERR);\n \t\t} else if (old_orig)\n-\t\t\trefs_delete_ref(get_main_ref_store(the_repository),\n+\t\t\trefs_delete_ref(get_main_ref_store(repo),\n \t\t\t\t\tNULL, \"ORIG_HEAD\", old_orig, 0);\n \t}\n \n@@ -64,23 +63,23 @@ static int update_refs(const struct reset_head_opts *opts,\n \t\treflog_head = msg.buf;\n \t}\n \tif (!switch_to_branch)\n-\t\tret = refs_update_ref(get_main_ref_store(the_repository),\n+\t\tret = refs_update_ref(get_main_ref_store(repo),\n \t\t\t\t      reflog_head, \"HEAD\", oid, head,\n \t\t\t\t      detach_head ? REF_NO_DEREF : 0,\n \t\t\t\t      UPDATE_REFS_MSG_ON_ERR);\n \telse {\n-\t\tret = refs_update_ref(get_main_ref_store(the_repository),\n+\t\tret = refs_update_ref(get_main_ref_store(repo),\n \t\t\t\t      reflog_branch ? reflog_branch : reflog_head,\n \t\t\t\t      switch_to_branch, oid, NULL, 0,\n \t\t\t\t      UPDATE_REFS_MSG_ON_ERR);\n \t\tif (!ret)\n-\t\t\tret = refs_update_symref(get_main_ref_store(the_repository),\n+\t\t\tret = refs_update_symref(get_main_ref_store(repo),\n \t\t\t\t\t\t \"HEAD\", switch_to_branch,\n \t\t\t\t\t\t reflog_head);\n \t}\n \tif (!ret && run_hook)\n-\t\trun_hooks_l(the_repository, \"post-checkout\",\n-\t\t\t    oid_to_hex(head ? head : null_oid(the_hash_algo)),\n+\t\trun_hooks_l(repo, \"post-checkout\",\n+\t\t\t    oid_to_hex(head ? head : null_oid(repo->hash_algo)),\n \t\t\t    oid_to_hex(oid), \"1\", NULL);\n \tstrbuf_release(&msg);\n \treturn ret;\n@@ -126,7 +125,7 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \t\toid = &head_oid;\n \n \tif (refs_only)\n-\t\treturn update_refs(opts, oid, head);\n+\t\treturn update_refs(r, opts, oid, head);\n \n \taction = reset_hard ? \"reset\" : \"checkout\";\n \tsetup_unpack_trees_porcelain(&unpack_tree_opts, action);\n@@ -163,7 +162,7 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \t\tgoto leave_reset_head;\n \t}\n \n-\ttree = repo_parse_tree_indirect(the_repository, oid);\n+\ttree = repo_parse_tree_indirect(r, oid);\n \tif (!tree) {\n \t\tret = error(_(\"unable to read tree (%s)\"), oid_to_hex(oid));\n \t\tgoto leave_reset_head;\n@@ -177,7 +176,7 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \t}\n \n \tif (oid != &head_oid || update_orig_head || switch_to_branch)\n-\t\tret = update_refs(opts, oid, head);\n+\t\tret = update_refs(r, opts, oid, head);\n \n leave_reset_head:\n \trollback_lock_file(&lock);\n\n-- \n2.54.0.1189.g8c84645362.dirty\n\n"},{"id":"545113","messageId":"20260610-b4-pks-history-drop-v4-3-70d5f0ae8c25@pks.im","threadId":"65726","inReplyTo":"20260610-b4-pks-history-drop-v4-0-70d5f0ae8c25@pks.im","subject":"[PATCH v4 03/10] reset: rename `reset_head()`","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-10T08:52:23Z","receivedAt":"2026-06-10T08:52:41Z","isPatch":true,"body":"In a subsequent commit we're about to adapt `reset_head()` so that the\nreference update to HEAD is optional, only. At this point the function\nstarts to feel misnamed, as it doesn't necessarily have anything to do\nwith the HEAD reference anymore. The gist of the function then is that\nwe reset the working tree to a specific new commit, updating both the\nindex and the checked-out files.\n\nRename it to `reset_working_tree()` to better reflect that.\n\nNote that we don't adjust the flags yet. This will happen in a\nsubsequent commit.\n\nSuggested-by: Phillip Wood <phillip.wood123@gmail.com>\n---\n builtin/rebase.c | 20 ++++++++++----------\n reset.c          |  5 +++--\n reset.h          |  4 ++--\n sequencer.c      |  8 ++++----\n 4 files changed, 19 insertions(+), 18 deletions(-)\n\ndiff --git a/builtin/rebase.c b/builtin/rebase.c\nindex fa4f5d9306..22fbba3c62 100644\n--- a/builtin/rebase.c\n+++ b/builtin/rebase.c\n@@ -592,7 +592,7 @@ static int finish_rebase(struct rebase_options *opts)\n static int move_to_original_branch(struct rebase_options *opts)\n {\n \tstruct strbuf branch_reflog = STRBUF_INIT, head_reflog = STRBUF_INIT;\n-\tstruct reset_head_opts ropts = { 0 };\n+\tstruct reset_working_tree_options ropts = { 0 };\n \tint ret;\n \n \tif (!opts->head_name)\n@@ -610,7 +610,7 @@ static int move_to_original_branch(struct rebase_options *opts)\n \tropts.flags = RESET_HEAD_REFS_ONLY;\n \tropts.branch_msg = branch_reflog.buf;\n \tropts.head_msg = head_reflog.buf;\n-\tret = reset_head(the_repository, &ropts);\n+\tret = reset_working_tree(the_repository, &ropts);\n \n \tstrbuf_release(&branch_reflog);\n \tstrbuf_release(&head_reflog);\n@@ -685,7 +685,7 @@ static int run_am(struct rebase_options *opts)\n \n \tstatus = run_command(&format_patch);\n \tif (status) {\n-\t\tstruct reset_head_opts ropts = { 0 };\n+\t\tstruct reset_working_tree_options ropts = { 0 };\n \t\tunlink(rebased_patches);\n \t\tfree(rebased_patches);\n \t\tchild_process_clear(&am);\n@@ -693,7 +693,7 @@ static int run_am(struct rebase_options *opts)\n \t\tropts.oid = &opts->orig_head->object.oid;\n \t\tropts.branch = opts->head_name;\n \t\tropts.default_reflog_action = opts->reflog_action;\n-\t\treset_head(the_repository, &ropts);\n+\t\treset_working_tree(the_repository, &ropts);\n \t\terror(_(\"\\ngit encountered an error while preparing the \"\n \t\t\t\"patches to replay\\n\"\n \t\t\t\"these revisions:\\n\"\n@@ -855,7 +855,7 @@ static int rebase_config(const char *var, const char *value,\n static int checkout_up_to_date(struct rebase_options *options)\n {\n \tstruct strbuf buf = STRBUF_INIT;\n-\tstruct reset_head_opts ropts = { 0 };\n+\tstruct reset_working_tree_options ropts = { 0 };\n \tint ret = 0;\n \n \tstrbuf_addf(&buf, \"%s: checkout %s\",\n@@ -866,7 +866,7 @@ static int checkout_up_to_date(struct rebase_options *options)\n \tif (!ropts.branch)\n \t\tropts.flags |=  RESET_HEAD_DETACH;\n \tropts.head_msg = buf.buf;\n-\tif (reset_head(the_repository, &ropts) < 0)\n+\tif (reset_working_tree(the_repository, &ropts) < 0)\n \t\tret = error(_(\"could not switch to %s\"), options->switch_to);\n \tstrbuf_release(&buf);\n \n@@ -1116,7 +1116,7 @@ int cmd_rebase(int argc,\n \tint reschedule_failed_exec = -1;\n \tint allow_preemptive_ff = 1;\n \tint preserve_merges_selected = 0;\n-\tstruct reset_head_opts ropts = { 0 };\n+\tstruct reset_working_tree_options ropts = { 0 };\n \tstruct option builtin_rebase_options[] = {\n \t\tOPT_STRING(0, \"onto\", &options.onto_name,\n \t\t\t   N_(\"revision\"),\n@@ -1385,7 +1385,7 @@ int cmd_rebase(int argc,\n \t\trerere_clear(the_repository, &merge_rr);\n \t\tstring_list_clear(&merge_rr, 1);\n \t\tropts.flags = RESET_HEAD_HARD;\n-\t\tif (reset_head(the_repository, &ropts) < 0)\n+\t\tif (reset_working_tree(the_repository, &ropts) < 0)\n \t\t\tdie(_(\"could not discard worktree changes\"));\n \t\tremove_branch_state(the_repository, 0);\n \t\tif (read_basic_state(&options))\n@@ -1410,7 +1410,7 @@ int cmd_rebase(int argc,\n \t\tropts.head_msg = head_msg.buf;\n \t\tropts.branch = options.head_name;\n \t\tropts.flags = RESET_HEAD_HARD;\n-\t\tif (reset_head(the_repository, &ropts) < 0)\n+\t\tif (reset_working_tree(the_repository, &ropts) < 0)\n \t\t\tdie(_(\"could not move back to %s\"),\n \t\t\t    oid_to_hex(&options.orig_head->object.oid));\n \t\tstrbuf_release(&head_msg);\n@@ -1880,7 +1880,7 @@ int cmd_rebase(int argc,\n \t\t\tRESET_HEAD_RUN_POST_CHECKOUT_HOOK;\n \tropts.head_msg = msg.buf;\n \tropts.default_reflog_action = options.reflog_action;\n-\tif (reset_head(the_repository, &ropts)) {\n+\tif (reset_working_tree(the_repository, &ropts)) {\n \t\tret = error(_(\"Could not detach HEAD\"));\n \t\tgoto cleanup_autostash;\n \t}\ndiff --git a/reset.c b/reset.c\nindex 3b3cb74dab..799596398b 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -12,7 +12,7 @@\n #include \"hook.h\"\n \n static int update_refs(struct repository *repo,\n-\t\t       const struct reset_head_opts *opts,\n+\t\t       const struct reset_working_tree_options *opts,\n \t\t       const struct object_id *oid,\n \t\t       const struct object_id *head)\n {\n@@ -85,7 +85,8 @@ static int update_refs(struct repository *repo,\n \treturn ret;\n }\n \n-int reset_head(struct repository *r, const struct reset_head_opts *opts)\n+int reset_working_tree(struct repository *r,\n+\t\t       const struct reset_working_tree_options *opts)\n {\n \tconst struct object_id *oid = opts->oid;\n \tconst char *switch_to_branch = opts->branch;\ndiff --git a/reset.h b/reset.h\nindex a28f81829d..f130152014 100644\n--- a/reset.h\n+++ b/reset.h\n@@ -17,7 +17,7 @@\n /* Update ORIG_HEAD as well as HEAD */\n #define RESET_ORIG_HEAD (1<<4)\n \n-struct reset_head_opts {\n+struct reset_working_tree_options {\n \t/*\n \t * The commit to checkout/reset to. Defaults to HEAD.\n \t */\n@@ -55,6 +55,6 @@ struct reset_head_opts {\n \tconst char *default_reflog_action;\n };\n \n-int reset_head(struct repository *r, const struct reset_head_opts *opts);\n+int reset_working_tree(struct repository *r, const struct reset_working_tree_options *opts);\n \n #endif\ndiff --git a/sequencer.c b/sequencer.c\nindex 1ee4b2875b..d73ecf0384 100644\n--- a/sequencer.c\n+++ b/sequencer.c\n@@ -4677,7 +4677,7 @@ static void create_autostash_internal(struct repository *r,\n \tif (has_unstaged_changes(r, 1) ||\n \t    has_uncommitted_changes(r, 1)) {\n \t\tstruct child_process stash = CHILD_PROCESS_INIT;\n-\t\tstruct reset_head_opts ropts = { .flags = RESET_HEAD_HARD };\n+\t\tstruct reset_working_tree_options ropts = { .flags = RESET_HEAD_HARD };\n \t\tstruct object_id oid;\n \n \t\tstrvec_pushl(&stash.args,\n@@ -4707,7 +4707,7 @@ static void create_autostash_internal(struct repository *r,\n \n \t\tif (!silent)\n \t\t\tprintf(_(\"Created autostash: %s\\n\"), buf.buf);\n-\t\tif (reset_head(r, &ropts) < 0)\n+\t\tif (reset_working_tree(r, &ropts) < 0)\n \t\t\tdie(_(\"could not reset --hard\"));\n \t\tdiscard_index(r->index);\n \t\tif (repo_read_index(r) < 0)\n@@ -4867,7 +4867,7 @@ static int checkout_onto(struct repository *r, struct replay_opts *opts,\n \t\t\t const char *onto_name, const struct object_id *onto,\n \t\t\t const struct object_id *orig_head)\n {\n-\tstruct reset_head_opts ropts = {\n+\tstruct reset_working_tree_options ropts = {\n \t\t.oid = onto,\n \t\t.orig_head = orig_head,\n \t\t.flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD |\n@@ -4876,7 +4876,7 @@ static int checkout_onto(struct repository *r, struct replay_opts *opts,\n \t\t\t\t\t   onto_name),\n \t\t.default_reflog_action = sequencer_reflog_action(opts)\n \t};\n-\tif (reset_head(r, &ropts)) {\n+\tif (reset_working_tree(r, &ropts)) {\n \t\tapply_autostash(rebase_path_autostash());\n \t\tsequencer_remove_state(opts);\n \t\treturn error(_(\"could not detach HEAD\"));\n\n-- \n2.54.0.1189.g8c84645362.dirty\n\n"},{"id":"545114","messageId":"20260610-b4-pks-history-drop-v4-4-70d5f0ae8c25@pks.im","threadId":"65726","inReplyTo":"20260610-b4-pks-history-drop-v4-0-70d5f0ae8c25@pks.im","subject":"[PATCH v4 04/10] reset: modernize flags passed to `reset_working_tree()`","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-10T08:52:24Z","receivedAt":"2026-06-10T08:52:43Z","isPatch":true,"body":"The flags passed to `reset_working_tree()` are declared as defines. This\nhas fallen a bit out of practice nowadays, where we instead prefer to\nuse enums. Furthermore, the prefix of those flags does not match the\nfunction name anymore after the rename in the preceding commit.\n\nAdapt the code to follow modern best practices and adapt the flag names.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n builtin/rebase.c | 15 ++++++++-------\n reset.c          | 12 ++++++------\n reset.h          | 31 +++++++++++++++++++------------\n sequencer.c      |  9 ++++++---\n 4 files changed, 39 insertions(+), 28 deletions(-)\n\ndiff --git a/builtin/rebase.c b/builtin/rebase.c\nindex 22fbba3c62..06dcbaf5e8 100644\n--- a/builtin/rebase.c\n+++ b/builtin/rebase.c\n@@ -607,7 +607,7 @@ static int move_to_original_branch(struct rebase_options *opts)\n \tstrbuf_addf(&head_reflog, \"%s (finish): returning to %s\",\n \t\t    opts->reflog_action, opts->head_name);\n \tropts.branch = opts->head_name;\n-\tropts.flags = RESET_HEAD_REFS_ONLY;\n+\tropts.flags = RESET_WORKING_TREE_REFS_ONLY;\n \tropts.branch_msg = branch_reflog.buf;\n \tropts.head_msg = head_reflog.buf;\n \tret = reset_working_tree(the_repository, &ropts);\n@@ -862,9 +862,9 @@ static int checkout_up_to_date(struct rebase_options *options)\n \t\t    options->reflog_action, options->switch_to);\n \tropts.oid = &options->orig_head->object.oid;\n \tropts.branch = options->head_name;\n-\tropts.flags = RESET_HEAD_RUN_POST_CHECKOUT_HOOK;\n+\tropts.flags = RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK;\n \tif (!ropts.branch)\n-\t\tropts.flags |=  RESET_HEAD_DETACH;\n+\t\tropts.flags |=  RESET_WORKING_TREE_DETACH;\n \tropts.head_msg = buf.buf;\n \tif (reset_working_tree(the_repository, &ropts) < 0)\n \t\tret = error(_(\"could not switch to %s\"), options->switch_to);\n@@ -1384,7 +1384,7 @@ int cmd_rebase(int argc,\n \n \t\trerere_clear(the_repository, &merge_rr);\n \t\tstring_list_clear(&merge_rr, 1);\n-\t\tropts.flags = RESET_HEAD_HARD;\n+\t\tropts.flags = RESET_WORKING_TREE_HARD;\n \t\tif (reset_working_tree(the_repository, &ropts) < 0)\n \t\t\tdie(_(\"could not discard worktree changes\"));\n \t\tremove_branch_state(the_repository, 0);\n@@ -1409,7 +1409,7 @@ int cmd_rebase(int argc,\n \t\tropts.oid = &options.orig_head->object.oid;\n \t\tropts.head_msg = head_msg.buf;\n \t\tropts.branch = options.head_name;\n-\t\tropts.flags = RESET_HEAD_HARD;\n+\t\tropts.flags = RESET_WORKING_TREE_HARD;\n \t\tif (reset_working_tree(the_repository, &ropts) < 0)\n \t\t\tdie(_(\"could not move back to %s\"),\n \t\t\t    oid_to_hex(&options.orig_head->object.oid));\n@@ -1876,8 +1876,9 @@ int cmd_rebase(int argc,\n \t\t    options.reflog_action, options.onto_name);\n \tropts.oid = &options.onto->object.oid;\n \tropts.orig_head = &options.orig_head->object.oid;\n-\tropts.flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD |\n-\t\t\tRESET_HEAD_RUN_POST_CHECKOUT_HOOK;\n+\tropts.flags = RESET_WORKING_TREE_DETACH |\n+\t\t      RESET_WORKING_TREE_UPDATE_ORIG_HEAD |\n+\t\t      RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK;\n \tropts.head_msg = msg.buf;\n \tropts.default_reflog_action = options.reflog_action;\n \tif (reset_working_tree(the_repository, &ropts)) {\ndiff --git a/reset.c b/reset.c\nindex 799596398b..4ca7f23a25 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -16,9 +16,9 @@ static int update_refs(struct repository *repo,\n \t\t       const struct object_id *oid,\n \t\t       const struct object_id *head)\n {\n-\tunsigned detach_head = opts->flags & RESET_HEAD_DETACH;\n-\tunsigned run_hook = opts->flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK;\n-\tunsigned update_orig_head = opts->flags & RESET_ORIG_HEAD;\n+\tunsigned detach_head = opts->flags & RESET_WORKING_TREE_DETACH;\n+\tunsigned run_hook = opts->flags & RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK;\n+\tunsigned update_orig_head = opts->flags & RESET_WORKING_TREE_UPDATE_ORIG_HEAD;\n \tconst struct object_id *orig_head = opts->orig_head;\n \tconst char *switch_to_branch = opts->branch;\n \tconst char *reflog_branch = opts->branch_msg;\n@@ -90,9 +90,9 @@ int reset_working_tree(struct repository *r,\n {\n \tconst struct object_id *oid = opts->oid;\n \tconst char *switch_to_branch = opts->branch;\n-\tunsigned reset_hard = opts->flags & RESET_HEAD_HARD;\n-\tunsigned refs_only = opts->flags & RESET_HEAD_REFS_ONLY;\n-\tunsigned update_orig_head = opts->flags & RESET_ORIG_HEAD;\n+\tunsigned reset_hard = opts->flags & RESET_WORKING_TREE_HARD;\n+\tunsigned refs_only = opts->flags & RESET_WORKING_TREE_REFS_ONLY;\n+\tunsigned update_orig_head = opts->flags & RESET_WORKING_TREE_UPDATE_ORIG_HEAD;\n \tstruct object_id *head = NULL, head_oid;\n \tstruct tree_desc desc[2] = { { NULL }, { NULL } };\n \tstruct lock_file lock = LOCK_INIT;\ndiff --git a/reset.h b/reset.h\nindex f130152014..2e5826de99 100644\n--- a/reset.h\n+++ b/reset.h\n@@ -6,16 +6,22 @@\n \n #define GIT_REFLOG_ACTION_ENVIRONMENT \"GIT_REFLOG_ACTION\"\n \n-/* Request a detached checkout */\n-#define RESET_HEAD_DETACH (1<<0)\n-/* Request a reset rather than a checkout */\n-#define RESET_HEAD_HARD (1<<1)\n-/* Run the post-checkout hook */\n-#define RESET_HEAD_RUN_POST_CHECKOUT_HOOK (1<<2)\n-/* Only update refs, do not touch the worktree */\n-#define RESET_HEAD_REFS_ONLY (1<<3)\n-/* Update ORIG_HEAD as well as HEAD */\n-#define RESET_ORIG_HEAD (1<<4)\n+enum reset_working_tree_flags {\n+\t/* Request a detached checkout */\n+\tRESET_WORKING_TREE_DETACH = (1 << 0),\n+\n+\t/* Request a reset rather than a checkout */\n+\tRESET_WORKING_TREE_HARD = (1 << 1),\n+\n+\t/* Run the post-checkout hook */\n+\tRESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK = (1 << 2),\n+\n+\t/* Only update refs, do not touch the worktree */\n+\tRESET_WORKING_TREE_REFS_ONLY = (1 << 3),\n+\n+\t/* Update ORIG_HEAD as well as HEAD */\n+\tRESET_WORKING_TREE_UPDATE_ORIG_HEAD = (1 << 4),\n+};\n \n struct reset_working_tree_options {\n \t/*\n@@ -33,7 +39,7 @@ struct reset_working_tree_options {\n \t/*\n \t * Flags defined above.\n \t */\n-\tunsigned flags;\n+\tenum reset_working_tree_flags flags;\n \t/*\n \t * Optional reflog message for branch, defaults to head_msg.\n \t */\n@@ -45,7 +51,8 @@ struct reset_working_tree_options {\n \tconst char *head_msg;\n \t/*\n \t * Optional reflog message for ORIG_HEAD, if this omitted and flags\n-\t * contains RESET_ORIG_HEAD then default_reflog_action must be given.\n+\t * contains RESET_WORKING_TREE_UPDATE_ORIG_HEAD then\n+\t * default_reflog_action must be given.\n \t */\n \tconst char *orig_head_msg;\n \t/*\ndiff --git a/sequencer.c b/sequencer.c\nindex d73ecf0384..4efe831178 100644\n--- a/sequencer.c\n+++ b/sequencer.c\n@@ -4677,7 +4677,9 @@ static void create_autostash_internal(struct repository *r,\n \tif (has_unstaged_changes(r, 1) ||\n \t    has_uncommitted_changes(r, 1)) {\n \t\tstruct child_process stash = CHILD_PROCESS_INIT;\n-\t\tstruct reset_working_tree_options ropts = { .flags = RESET_HEAD_HARD };\n+\t\tstruct reset_working_tree_options ropts = {\n+\t\t\t.flags = RESET_WORKING_TREE_HARD,\n+\t\t};\n \t\tstruct object_id oid;\n \n \t\tstrvec_pushl(&stash.args,\n@@ -4870,8 +4872,9 @@ static int checkout_onto(struct repository *r, struct replay_opts *opts,\n \tstruct reset_working_tree_options ropts = {\n \t\t.oid = onto,\n \t\t.orig_head = orig_head,\n-\t\t.flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD |\n-\t\t\t\tRESET_HEAD_RUN_POST_CHECKOUT_HOOK,\n+\t\t.flags = RESET_WORKING_TREE_DETACH |\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.default_reflog_action = sequencer_reflog_action(opts)\n\n-- \n2.54.0.1189.g8c84645362.dirty\n\n"},{"id":"545115","messageId":"20260610-b4-pks-history-drop-v4-5-70d5f0ae8c25@pks.im","threadId":"65726","inReplyTo":"20260610-b4-pks-history-drop-v4-0-70d5f0ae8c25@pks.im","subject":"[PATCH v4 05/10] reset: introduce dry-run mode","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-10T08:52:25Z","receivedAt":"2026-06-10T08:52:46Z","isPatch":true,"body":"In a subsequent commit we'll add another caller to `reset_working_tree()`\nthat wants to perform a dry-run check of whether it would be possible to\nupdate the index and working tree when moving to a new commit. Introduce\na new flag that lets the caller perform this operation.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reset.c | 44 +++++++++++++++++++++++++++++++++-----------\n reset.h |  6 ++++++\n 2 files changed, 39 insertions(+), 11 deletions(-)\n\ndiff --git a/reset.c b/reset.c\nindex 4ca7f23a25..99f2c1b012 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -93,11 +93,14 @@ int reset_working_tree(struct repository *r,\n \tunsigned reset_hard = opts->flags & RESET_WORKING_TREE_HARD;\n \tunsigned refs_only = opts->flags & RESET_WORKING_TREE_REFS_ONLY;\n \tunsigned update_orig_head = opts->flags & RESET_WORKING_TREE_UPDATE_ORIG_HEAD;\n+\tunsigned dry_run = opts->flags & RESET_WORKING_TREE_DRY_RUN;\n \tstruct object_id *head = NULL, head_oid;\n \tstruct tree_desc desc[2] = { { NULL }, { NULL } };\n \tstruct lock_file lock = LOCK_INIT;\n \tstruct unpack_trees_options unpack_tree_opts = { 0 };\n \tstruct tree *tree;\n+\tstruct index_state scratch_index = INDEX_STATE_INIT(r);\n+\tstruct index_state *istate;\n \tconst char *action;\n \tint ret = 0, nr = 0;\n \n@@ -110,7 +113,7 @@ int reset_working_tree(struct repository *r,\n \tif (opts->branch_msg && !opts->branch)\n \t\tBUG(\"branch reflog message given without a branch\");\n \n-\tif (!refs_only && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {\n+\tif (!refs_only && !dry_run && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {\n \t\tret = -1;\n \t\tgoto leave_reset_head;\n \t}\n@@ -125,16 +128,36 @@ int reset_working_tree(struct repository *r,\n \tif (!oid)\n \t\toid = &head_oid;\n \n-\tif (refs_only)\n-\t\treturn update_refs(r, opts, oid, head);\n+\tif (refs_only) {\n+\t\tif (!dry_run)\n+\t\t\treturn update_refs(r, opts, oid, head);\n+\t\treturn 0;\n+\t}\n+\n+\tif (dry_run) {\n+\t\tif (read_index_from(&scratch_index, r->index_file, r->gitdir) < 0 ||\n+\t\t    index_state_unmerged_to_stage0(&scratch_index) < 0) {\n+\t\t\tret = error(_(\"could not read index\"));\n+\t\t\tgoto leave_reset_head;\n+\t\t}\n+\n+\t\tistate = &scratch_index;\n+\t} else {\n+\t\tif (repo_read_index_unmerged(r) < 0) {\n+\t\t\tret = error(_(\"could not read index\"));\n+\t\t\tgoto leave_reset_head;\n+\t\t}\n+\t\tistate = r->index;\n+\t}\n \n \taction = reset_hard ? \"reset\" : \"checkout\";\n \tsetup_unpack_trees_porcelain(&unpack_tree_opts, action);\n \tunpack_tree_opts.head_idx = 1;\n-\tunpack_tree_opts.src_index = r->index;\n-\tunpack_tree_opts.dst_index = r->index;\n+\tunpack_tree_opts.src_index = istate;\n+\tunpack_tree_opts.dst_index = istate;\n \tunpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge;\n-\tunpack_tree_opts.update = 1;\n+\tunpack_tree_opts.update = !dry_run;\n+\tunpack_tree_opts.dry_run = dry_run;\n \tunpack_tree_opts.merge = 1;\n \tunpack_tree_opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */\n \tunpack_tree_opts.skip_cache_tree_update = 1;\n@@ -142,11 +165,6 @@ int reset_working_tree(struct repository *r,\n \tif (reset_hard)\n \t\tunpack_tree_opts.reset = UNPACK_RESET_PROTECT_UNTRACKED;\n \n-\tif (repo_read_index_unmerged(r) < 0) {\n-\t\tret = error(_(\"could not read index\"));\n-\t\tgoto leave_reset_head;\n-\t}\n-\n \tif (!reset_hard && !fill_tree_descriptor(r, &desc[nr++], &head_oid)) {\n \t\tret = error(_(\"failed to find tree of %s\"),\n \t\t\t    oid_to_hex(&head_oid));\n@@ -163,6 +181,9 @@ int reset_working_tree(struct repository *r,\n \t\tgoto leave_reset_head;\n \t}\n \n+\tif (dry_run)\n+\t\tgoto leave_reset_head;\n+\n \ttree = repo_parse_tree_indirect(r, oid);\n \tif (!tree) {\n \t\tret = error(_(\"unable to read tree (%s)\"), oid_to_hex(oid));\n@@ -182,6 +203,7 @@ int reset_working_tree(struct repository *r,\n leave_reset_head:\n \trollback_lock_file(&lock);\n \tclear_unpack_trees_porcelain(&unpack_tree_opts);\n+\trelease_index(&scratch_index);\n \twhile (nr)\n \t\tfree((void *)desc[--nr].buffer);\n \treturn ret;\ndiff --git a/reset.h b/reset.h\nindex 2e5826de99..898e4a1e95 100644\n--- a/reset.h\n+++ b/reset.h\n@@ -21,6 +21,12 @@ enum reset_working_tree_flags {\n \n \t/* Update ORIG_HEAD as well as HEAD */\n \tRESET_WORKING_TREE_UPDATE_ORIG_HEAD = (1 << 4),\n+\n+\t/*\n+\t * Perform a dry-run by performing the operation without updating\n+\t * any user-visible state.\n+\t */\n+\tRESET_WORKING_TREE_DRY_RUN = (1 << 5),\n };\n \n struct reset_working_tree_options {\n\n-- \n2.54.0.1189.g8c84645362.dirty\n\n"},{"id":"545116","messageId":"20260610-b4-pks-history-drop-v4-6-70d5f0ae8c25@pks.im","threadId":"65726","inReplyTo":"20260610-b4-pks-history-drop-v4-0-70d5f0ae8c25@pks.im","subject":"[PATCH v4 06/10] reset: introduce ability to skip updating HEAD","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-10T08:52:26Z","receivedAt":"2026-06-10T08:52:49Z","isPatch":true,"body":"In a subsequent commit we'll introduce a new caller to\n`reset_working_tree()` that really only wants to update the index and\nworking tree, without updating any references. Introduce a new flag that\nmakes the caller opt in to updating HEAD and adapt all callers to set\nthat flag.\n\nNote that in a previous iteration we instead introduced a flag that made\ncallers opt out of updating any references. This was somewhat awkward\nthough because we already have the `UPDATE_ORIG_HEAD` flag, so the\nresult was somewhat inconsistent.\n\nSuggested-by: Phillip Wood <phillip.wood123@gmail.com>\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n builtin/rebase.c | 14 ++++++++++----\n reset.c          |  6 ++++--\n reset.h          |  9 ++++++---\n sequencer.c      |  4 +++-\n 4 files changed, 23 insertions(+), 10 deletions(-)\n\ndiff --git a/builtin/rebase.c b/builtin/rebase.c\nindex 06dcbaf5e8..10a306310c 100644\n--- a/builtin/rebase.c\n+++ b/builtin/rebase.c\n@@ -607,7 +607,8 @@ static int move_to_original_branch(struct rebase_options *opts)\n \tstrbuf_addf(&head_reflog, \"%s (finish): returning to %s\",\n \t\t    opts->reflog_action, opts->head_name);\n \tropts.branch = opts->head_name;\n-\tropts.flags = RESET_WORKING_TREE_REFS_ONLY;\n+\tropts.flags = RESET_WORKING_TREE_REFS_ONLY |\n+\t\t      RESET_WORKING_TREE_UPDATE_HEAD;\n \tropts.branch_msg = branch_reflog.buf;\n \tropts.head_msg = head_reflog.buf;\n \tret = reset_working_tree(the_repository, &ropts);\n@@ -693,6 +694,7 @@ static int run_am(struct rebase_options *opts)\n \t\tropts.oid = &opts->orig_head->object.oid;\n \t\tropts.branch = opts->head_name;\n \t\tropts.default_reflog_action = opts->reflog_action;\n+\t\tropts.flags = RESET_WORKING_TREE_UPDATE_HEAD;\n \t\treset_working_tree(the_repository, &ropts);\n \t\terror(_(\"\\ngit encountered an error while preparing the \"\n \t\t\t\"patches to replay\\n\"\n@@ -862,7 +864,8 @@ static int checkout_up_to_date(struct rebase_options *options)\n \t\t    options->reflog_action, options->switch_to);\n \tropts.oid = &options->orig_head->object.oid;\n \tropts.branch = options->head_name;\n-\tropts.flags = RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK;\n+\tropts.flags = RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK |\n+\t\t      RESET_WORKING_TREE_UPDATE_HEAD;\n \tif (!ropts.branch)\n \t\tropts.flags |=  RESET_WORKING_TREE_DETACH;\n \tropts.head_msg = buf.buf;\n@@ -1384,7 +1387,8 @@ int cmd_rebase(int argc,\n \n \t\trerere_clear(the_repository, &merge_rr);\n \t\tstring_list_clear(&merge_rr, 1);\n-\t\tropts.flags = RESET_WORKING_TREE_HARD;\n+\t\tropts.flags = RESET_WORKING_TREE_HARD |\n+\t\t\t      RESET_WORKING_TREE_UPDATE_HEAD;\n \t\tif (reset_working_tree(the_repository, &ropts) < 0)\n \t\t\tdie(_(\"could not discard worktree changes\"));\n \t\tremove_branch_state(the_repository, 0);\n@@ -1409,7 +1413,8 @@ int cmd_rebase(int argc,\n \t\tropts.oid = &options.orig_head->object.oid;\n \t\tropts.head_msg = head_msg.buf;\n \t\tropts.branch = options.head_name;\n-\t\tropts.flags = RESET_WORKING_TREE_HARD;\n+\t\tropts.flags = RESET_WORKING_TREE_HARD |\n+\t\t\t      RESET_WORKING_TREE_UPDATE_HEAD;\n \t\tif (reset_working_tree(the_repository, &ropts) < 0)\n \t\t\tdie(_(\"could not move back to %s\"),\n \t\t\t    oid_to_hex(&options.orig_head->object.oid));\n@@ -1877,6 +1882,7 @@ int cmd_rebase(int argc,\n \tropts.oid = &options.onto->object.oid;\n \tropts.orig_head = &options.orig_head->object.oid;\n \tropts.flags = RESET_WORKING_TREE_DETACH |\n+\t\t      RESET_WORKING_TREE_UPDATE_HEAD |\n \t\t      RESET_WORKING_TREE_UPDATE_ORIG_HEAD |\n \t\t      RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK;\n \tropts.head_msg = msg.buf;\ndiff --git a/reset.c b/reset.c\nindex 99f2c1b012..3ac99a51c0 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -92,6 +92,7 @@ int reset_working_tree(struct repository *r,\n \tconst char *switch_to_branch = opts->branch;\n \tunsigned reset_hard = opts->flags & RESET_WORKING_TREE_HARD;\n \tunsigned refs_only = opts->flags & RESET_WORKING_TREE_REFS_ONLY;\n+\tunsigned update_head = opts->flags & RESET_WORKING_TREE_UPDATE_HEAD;\n \tunsigned update_orig_head = opts->flags & RESET_WORKING_TREE_UPDATE_ORIG_HEAD;\n \tunsigned dry_run = opts->flags & RESET_WORKING_TREE_DRY_RUN;\n \tstruct object_id *head = NULL, head_oid;\n@@ -129,7 +130,7 @@ int reset_working_tree(struct repository *r,\n \t\toid = &head_oid;\n \n \tif (refs_only) {\n-\t\tif (!dry_run)\n+\t\tif (update_head)\n \t\t\treturn update_refs(r, opts, oid, head);\n \t\treturn 0;\n \t}\n@@ -197,7 +198,8 @@ int reset_working_tree(struct repository *r,\n \t\tgoto leave_reset_head;\n \t}\n \n-\tif (oid != &head_oid || update_orig_head || switch_to_branch)\n+\tif (update_head &&\n+\t    (oid != &head_oid || update_orig_head || switch_to_branch))\n \t\tret = update_refs(r, opts, oid, head);\n \n leave_reset_head:\ndiff --git a/reset.h b/reset.h\nindex 898e4a1e95..38b2891b53 100644\n--- a/reset.h\n+++ b/reset.h\n@@ -19,14 +19,17 @@ enum reset_working_tree_flags {\n \t/* Only update refs, do not touch the worktree */\n \tRESET_WORKING_TREE_REFS_ONLY = (1 << 3),\n \n-\t/* Update ORIG_HEAD as well as HEAD */\n-\tRESET_WORKING_TREE_UPDATE_ORIG_HEAD = (1 << 4),\n+\t/* Update HEAD */\n+\tRESET_WORKING_TREE_UPDATE_HEAD = (1 << 4),\n+\n+\t/* Update ORIG_HEAD */\n+\tRESET_WORKING_TREE_UPDATE_ORIG_HEAD = (1 << 5),\n \n \t/*\n \t * Perform a dry-run by performing the operation without updating\n \t * any user-visible state.\n \t */\n-\tRESET_WORKING_TREE_DRY_RUN = (1 << 5),\n+\tRESET_WORKING_TREE_DRY_RUN = (1 << 6),\n };\n \n struct reset_working_tree_options {\ndiff --git a/sequencer.c b/sequencer.c\nindex 4efe831178..e905b1b2d9 100644\n--- a/sequencer.c\n+++ b/sequencer.c\n@@ -4678,7 +4678,8 @@ static void create_autostash_internal(struct repository *r,\n \t    has_uncommitted_changes(r, 1)) {\n \t\tstruct child_process stash = CHILD_PROCESS_INIT;\n \t\tstruct reset_working_tree_options ropts = {\n-\t\t\t.flags = RESET_WORKING_TREE_HARD,\n+\t\t\t.flags = RESET_WORKING_TREE_HARD |\n+\t\t\t\t RESET_WORKING_TREE_UPDATE_HEAD,\n \t\t};\n \t\tstruct object_id oid;\n \n@@ -4873,6 +4874,7 @@ static int checkout_onto(struct repository *r, struct replay_opts *opts,\n \t\t.oid = onto,\n \t\t.orig_head = orig_head,\n \t\t.flags = RESET_WORKING_TREE_DETACH |\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\n-- \n2.54.0.1189.g8c84645362.dirty\n\n"},{"id":"545117","messageId":"20260610-b4-pks-history-drop-v4-7-70d5f0ae8c25@pks.im","threadId":"65726","inReplyTo":"20260610-b4-pks-history-drop-v4-0-70d5f0ae8c25@pks.im","subject":"[PATCH v4 07/10] reset: allow the caller to specify the current HEAD object","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-10T08:52:27Z","receivedAt":"2026-06-10T08:52:51Z","isPatch":true,"body":"When calling `reset_working_tree()` we automatically derive the commit\nthat the callers wants to move from by reading the HEAD commit. Some\ncallers may already have resolved it, or they may want to move from a\ndifferent commit that doesn't match HEAD.\n\nIntroduce a new `oid_from` option that lets the caller specify the\ncommit.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reset.c | 5 ++++-\n reset.h | 5 +++++\n 2 files changed, 9 insertions(+), 1 deletion(-)\n\ndiff --git a/reset.c b/reset.c\nindex 3ac99a51c0..abe36ba116 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -119,7 +119,10 @@ int reset_working_tree(struct repository *r,\n \t\tgoto leave_reset_head;\n \t}\n \n-\tif (!repo_get_oid(r, \"HEAD\", &head_oid)) {\n+\tif (opts->oid_from) {\n+\t\toidcpy(&head_oid, opts->oid_from);\n+\t\thead = &head_oid;\n+\t} else if (!repo_get_oid(r, \"HEAD\", &head_oid)) {\n \t\thead = &head_oid;\n \t} else if (!oid || !reset_hard) {\n \t\tret = error(_(\"could not determine HEAD revision\"));\ndiff --git a/reset.h b/reset.h\nindex 38b2891b53..4c992ba671 100644\n--- a/reset.h\n+++ b/reset.h\n@@ -37,6 +37,11 @@ struct reset_working_tree_options {\n \t * The commit to checkout/reset to. Defaults to HEAD.\n \t */\n \tconst struct object_id *oid;\n+\t/*\n+\t * The commit to checkout/reset from when doing a two-way merge. This\n+\t * is used as one of the sides to merge.\n+\t */\n+\tconst struct object_id *oid_from;\n \t/*\n \t * Optional value to set ORIG_HEAD. Defaults to HEAD.\n \t */\n\n-- \n2.54.0.1189.g8c84645362.dirty\n\n"},{"id":"545118","messageId":"20260610-b4-pks-history-drop-v4-8-70d5f0ae8c25@pks.im","threadId":"65726","inReplyTo":"20260610-b4-pks-history-drop-v4-0-70d5f0ae8c25@pks.im","subject":"[PATCH v4 08/10] reset: stop assuming that the caller passes in a clean index","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-10T08:52:28Z","receivedAt":"2026-06-10T08:52:54Z","isPatch":true,"body":"In 652bd0211d (rebase: use 'skip_cache_tree_update' option, 2022-11-10),\nwe updated `reset_working_tree()` to stop updating the index tree cache.\nThis was done as a performance optimization: the function is only called\nby \"sequencer.c\" and \"rebase.c\", both of which assume a clean index\nbefore they perform their operation, so we know that the end result will\nbe a clean index, too. Consequently, we can skip recomputing the cache\nas we can instead use `prime_cache_tree()` directly.\n\nIn a subsequent commit we're about to add a new caller though where the\nassumption doesn't hold anymore: the index may be dirty before calling\n`reset_working_tree()`, and consequently we cannot prime the cache with\na given tree anymore as the index and tree will mismatch.\n\nAdapt the logic so that we only skip the cache tree update in case we're\ndoing a hard reset. While we could introduce logic that only skips the\nupdate in case the incoming index was dirty already, that doesn't really\nfeel worth it: after all, the mentioned commit says itself that the\nperformance improvement was negligible anyway.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reset.c | 8 +++++---\n 1 file changed, 5 insertions(+), 3 deletions(-)\n\ndiff --git a/reset.c b/reset.c\nindex abe36ba116..2b82e80c67 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -164,10 +164,11 @@ int reset_working_tree(struct repository *r,\n \tunpack_tree_opts.dry_run = dry_run;\n \tunpack_tree_opts.merge = 1;\n \tunpack_tree_opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */\n-\tunpack_tree_opts.skip_cache_tree_update = 1;\n \tinit_checkout_metadata(&unpack_tree_opts.meta, switch_to_branch, oid, NULL);\n-\tif (reset_hard)\n+\tif (reset_hard) {\n+\t\tunpack_tree_opts.skip_cache_tree_update = 1;\n \t\tunpack_tree_opts.reset = UNPACK_RESET_PROTECT_UNTRACKED;\n+\t}\n \n \tif (!reset_hard && !fill_tree_descriptor(r, &desc[nr++], &head_oid)) {\n \t\tret = error(_(\"failed to find tree of %s\"),\n@@ -194,7 +195,8 @@ int reset_working_tree(struct repository *r,\n \t\tgoto leave_reset_head;\n \t}\n \n-\tprime_cache_tree(r, r->index, tree);\n+\tif (reset_hard)\n+\t\tprime_cache_tree(r, r->index, tree);\n \n \tif (write_locked_index(r->index, &lock, COMMIT_LOCK) < 0) {\n \t\tret = error(_(\"could not write index\"));\n\n-- \n2.54.0.1189.g8c84645362.dirty\n\n"},{"id":"545119","messageId":"20260610-b4-pks-history-drop-v4-9-70d5f0ae8c25@pks.im","threadId":"65726","inReplyTo":"20260610-b4-pks-history-drop-v4-0-70d5f0ae8c25@pks.im","subject":"[PATCH v4 09/10] builtin/history: split handling of ref updates into two phases","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-10T08:52:29Z","receivedAt":"2026-06-10T08:52:57Z","isPatch":true,"body":"The function `handle_reference_updates()` is used by git-history(1) to\nupdate all references that refer to commits that have been rewritten. As\nsuch, it performs two steps:\n\n  - It gathers the references that need to be updated in the first\n    place.\n\n  - It prepares and commits the reference transaction.\n\nIn a subsequent commit we'll want to handle those two steps separately.\nPrepare for this by splitting up the function into two.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n builtin/history.c | 102 ++++++++++++++++++++++++++++++++++--------------------\n 1 file changed, 64 insertions(+), 38 deletions(-)\n\ndiff --git a/builtin/history.c b/builtin/history.c\nindex 0fc06fb204..4fadf38c32 100644\n--- a/builtin/history.c\n+++ b/builtin/history.c\n@@ -333,21 +333,17 @@ static int handle_ref_update(struct ref_transaction *transaction,\n \t\t\t\t      NULL, NULL, 0, reflog_msg, err);\n }\n \n-static int handle_reference_updates(struct rev_info *revs,\n-\t\t\t\t    enum ref_action action,\n-\t\t\t\t    struct commit *original,\n-\t\t\t\t    struct commit *rewritten,\n-\t\t\t\t    const char *reflog_msg,\n-\t\t\t\t    int dry_run,\n-\t\t\t\t    enum replay_empty_commit_action empty)\n+static int compute_pending_ref_updates(struct rev_info *revs,\n+\t\t\t\t       enum ref_action action,\n+\t\t\t\t       struct commit *original,\n+\t\t\t\t       struct commit *rewritten,\n+\t\t\t\t       enum replay_empty_commit_action empty,\n+\t\t\t\t       struct replay_result *result)\n {\n \tconst struct name_decoration *decoration;\n \tstruct replay_revisions_options opts = {\n \t\t.empty = empty,\n \t};\n-\tstruct replay_result result = { 0 };\n-\tstruct ref_transaction *transaction = NULL;\n-\tstruct strbuf err = STRBUF_INIT;\n \tchar hex[GIT_MAX_HEXSZ + 1];\n \tbool detached_head;\n \tint head_flags = 0;\n@@ -359,34 +355,13 @@ static int handle_reference_updates(struct rev_info *revs,\n \n \topts.onto = oid_to_hex_r(hex, &rewritten->object.oid);\n \n-\tret = replay_revisions(revs, &opts, &result);\n+\tret = replay_revisions(revs, &opts, result);\n \tif (ret)\n-\t\tgoto out;\n+\t\treturn ret;\n \n \tif (action != REF_ACTION_BRANCHES && action != REF_ACTION_HEAD)\n \t\tBUG(\"unsupported ref action %d\", action);\n \n-\tif (!dry_run) {\n-\t\ttransaction = ref_store_transaction_begin(get_main_ref_store(revs->repo), 0, &err);\n-\t\tif (!transaction) {\n-\t\t\tret = error(_(\"failed to begin ref transaction: %s\"), err.buf);\n-\t\t\tgoto out;\n-\t\t}\n-\t}\n-\n-\tfor (size_t i = 0; i < result.updates_nr; i++) {\n-\t\tret = handle_ref_update(transaction,\n-\t\t\t\t\tresult.updates[i].refname,\n-\t\t\t\t\t&result.updates[i].new_oid,\n-\t\t\t\t\t&result.updates[i].old_oid,\n-\t\t\t\t\treflog_msg, &err);\n-\t\tif (ret) {\n-\t\t\tret = error(_(\"failed to update ref '%s': %s\"),\n-\t\t\t\t    result.updates[i].refname, err.buf);\n-\t\t\tgoto out;\n-\t\t}\n-\t}\n-\n \t/*\n \t * `replay_revisions()` only updates references that are\n \t * ancestors of `rewritten`, so we need to manually\n@@ -414,14 +389,43 @@ static int handle_reference_updates(struct rev_info *revs,\n \t\t    !detached_head)\n \t\t\tcontinue;\n \n+\t\tALLOC_GROW(result->updates, result->updates_nr + 1, result->updates_alloc);\n+\t\tresult->updates[result->updates_nr].refname = xstrdup(decoration->name);\n+\t\tresult->updates[result->updates_nr].old_oid = original->object.oid;\n+\t\tresult->updates[result->updates_nr].new_oid = rewritten->object.oid;\n+\t\tresult->updates_nr++;\n+\t}\n+\n+\treturn 0;\n+}\n+\n+static int apply_pending_ref_updates(struct repository *repo,\n+\t\t\t\t     const struct replay_result *result,\n+\t\t\t\t     const char *reflog_msg,\n+\t\t\t\t     int dry_run)\n+{\n+\tstruct ref_transaction *transaction = NULL;\n+\tstruct strbuf err = STRBUF_INIT;\n+\tint ret;\n+\n+\tif (!dry_run) {\n+\t\ttransaction = ref_store_transaction_begin(get_main_ref_store(repo),\n+\t\t\t\t\t\t\t  0, &err);\n+\t\tif (!transaction) {\n+\t\t\tret = error(_(\"failed to begin ref transaction: %s\"), err.buf);\n+\t\t\tgoto out;\n+\t\t}\n+\t}\n+\n+\tfor (size_t i = 0; i < result->updates_nr; i++) {\n \t\tret = handle_ref_update(transaction,\n-\t\t\t\t\tdecoration->name,\n-\t\t\t\t\t&rewritten->object.oid,\n-\t\t\t\t\t&original->object.oid,\n+\t\t\t\t\tresult->updates[i].refname,\n+\t\t\t\t\t&result->updates[i].new_oid,\n+\t\t\t\t\t&result->updates[i].old_oid,\n \t\t\t\t\treflog_msg, &err);\n \t\tif (ret) {\n \t\t\tret = error(_(\"failed to update ref '%s': %s\"),\n-\t\t\t\t    decoration->name, err.buf);\n+\t\t\t\t    result->updates[i].refname, err.buf);\n \t\t\tgoto out;\n \t\t}\n \t}\n@@ -435,11 +439,33 @@ static int handle_reference_updates(struct rev_info *revs,\n \n out:\n \tref_transaction_free(transaction);\n-\treplay_result_release(&result);\n \tstrbuf_release(&err);\n \treturn ret;\n }\n \n+static int handle_reference_updates(struct rev_info *revs,\n+\t\t\t\t    enum ref_action action,\n+\t\t\t\t    struct commit *original,\n+\t\t\t\t    struct commit *rewritten,\n+\t\t\t\t    const char *reflog_msg,\n+\t\t\t\t    int dry_run,\n+\t\t\t\t    enum replay_empty_commit_action empty)\n+{\n+\tstruct replay_result result = { 0 };\n+\tint ret;\n+\n+\tret = compute_pending_ref_updates(revs, action, original, rewritten,\n+\t\t\t\t\t  empty, &result);\n+\tif (ret)\n+\t\tgoto out;\n+\n+\tret = apply_pending_ref_updates(revs->repo, &result, reflog_msg, dry_run);\n+\n+out:\n+\treplay_result_release(&result);\n+\treturn ret;\n+}\n+\n static int commit_became_empty(struct repository *repo,\n \t\t\t       struct commit *original,\n \t\t\t       struct tree *result)\n\n-- \n2.54.0.1189.g8c84645362.dirty\n\n"},{"id":"545120","messageId":"20260610-b4-pks-history-drop-v4-10-70d5f0ae8c25@pks.im","threadId":"65726","inReplyTo":"20260610-b4-pks-history-drop-v4-0-70d5f0ae8c25@pks.im","subject":"[PATCH v4 10/10] builtin/history: implement \"drop\" subcommand","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-10T08:52:30Z","receivedAt":"2026-06-10T08:52:59Z","isPatch":true,"body":"A common operation when editing the commit history is to drop a specific\ncommit from the history entirely, but this operation is not currently\ncovered by git-history(1).\n\nA couple of noteworthy bits:\n\n  - This is the first git-history(1) command that will ultimately result\n    in changes to both the index and the working tree. We thus have to\n    add logic to merge resulting changes into those.\n\n  - It is still not possible to replay merge commits, so this limitation\n    is inherited for the new \"drop\" command.\n\n  - For now we refuse to drop root commits. While we _can_ indeed drop\n    root commits in the general case, there are edge cases where the\n    resulting history would become completely empty. This is thus left\n    to a subsequent patch series.\n\nOther than that, most of the logic is rather straight-forward as we can\ncontinue to build on the preexisting logic in git-history(1) for most of\nthe part.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n Documentation/git-history.adoc |  38 ++-\n builtin/history.c              | 186 ++++++++++++++\n t/meson.build                  |   1 +\n t/t3454-history-drop.sh        | 537 +++++++++++++++++++++++++++++++++++++++++\n 4 files changed, 761 insertions(+), 1 deletion(-)\n\ndiff --git a/Documentation/git-history.adoc b/Documentation/git-history.adoc\nindex 2ba8121795..28b477cd37 100644\n--- a/Documentation/git-history.adoc\n+++ b/Documentation/git-history.adoc\n@@ -8,6 +8,7 @@ git-history - EXPERIMENTAL: Rewrite history\n SYNOPSIS\n --------\n [synopsis]\n+git history drop <commit> [--dry-run] [--update-refs=(branches|head)] [--empty=(drop|keep|abort)]\n git history fixup <commit> [--dry-run] [--update-refs=(branches|head)] [--reedit-message] [--empty=(drop|keep|abort)]\n git history reword <commit> [--dry-run] [--update-refs=(branches|head)]\n git history split <commit> [--dry-run] [--update-refs=(branches|head)] [--] [<pathspec>...]\n@@ -51,13 +52,28 @@ be stateful operations. The limitation can be lifted once (if) Git learns about\n first-class conflicts.\n \n When using `fixup` with `--empty=drop`, dropping the root commit is not yet\n-supported.\n+supported. Likewise, `drop` cannot remove the root commit or a merge commit.\n \n COMMANDS\n --------\n \n The following commands are available to rewrite history in different ways:\n \n+`drop <commit>`::\n+\tRemove the specified commit from the history. All descendants of the\n+\tcommit are replayed directly onto its parent.\n++\n+The root commit cannot be dropped as that may lead to edge cases where refs\n+end up with no commits anymore. Merge commits cannot be dropped either; see\n+LIMITATIONS.\n++\n+If `HEAD` points at a commit that is to be rewritten, the index and working\n+tree are updated to match the new `HEAD`. The command aborts before any\n+references are updated in case local modifications would be overwritten.\n++\n+If replaying any descendant would result in a conflict, the command aborts\n+with an error.\n+\n `fixup <commit>`::\n \tApply the currently staged changes to the specified commit. This is\n \tsimilar in nature to `git commit --fixup=<commit>` followed by `git\n@@ -170,6 +186,26 @@ The staged addition of `unrelated.txt` has been incorporated into the `first`\n commit. All descendant commits have been replayed on top of the rewritten\n history.\n \n+Drop a commit\n+~~~~~~~~~~~~~\n+\n+----------\n+$ git log --oneline\n+abc1234 (HEAD -> main) third\n+def5678 second\n+ghi9012 first\n+\n+$ git history drop 'main^{/second}'\n+\n+$ git log --oneline\n+jkl3456 (HEAD -> main) third\n+ghi9012 first\n+----------\n+\n+The `second` commit has been removed from the history, and `third` has been\n+replayed directly on top of `first`. All branches that pointed at the dropped\n+commit have been moved to its parent.\n+\n Split a commit\n ~~~~~~~~~~~~~~\n \ndiff --git a/builtin/history.c b/builtin/history.c\nindex 4fadf38c32..e59f69233b 100644\n--- a/builtin/history.c\n+++ b/builtin/history.c\n@@ -17,13 +17,17 @@\n #include \"read-cache.h\"\n #include \"refs.h\"\n #include \"replay.h\"\n+#include \"reset.h\"\n #include \"revision.h\"\n #include \"sequencer.h\"\n #include \"strvec.h\"\n #include \"tree.h\"\n+#include \"tree-walk.h\"\n #include \"unpack-trees.h\"\n #include \"wt-status.h\"\n \n+#define GIT_HISTORY_DROP_USAGE \\\n+\tN_(\"git history drop <commit> [--dry-run] [--update-refs=(branches|head)] [--empty=(drop|keep|abort)]\")\n #define GIT_HISTORY_FIXUP_USAGE \\\n \tN_(\"git history fixup <commit> [--dry-run] [--update-refs=(branches|head)] [--reedit-message] [--empty=(drop|keep|abort)]\")\n #define GIT_HISTORY_REWORD_USAGE \\\n@@ -1001,12 +1005,193 @@ static int cmd_history_split(int argc,\n \treturn ret;\n }\n \n+static int update_worktree(struct repository *repo,\n+\t\t\t   const struct commit *old_head,\n+\t\t\t   const struct commit *new_head,\n+\t\t\t   bool dry_run)\n+{\n+\tstruct reset_working_tree_options opts = {\n+\t\t.oid_from = &old_head->object.oid,\n+\t\t.oid = &new_head->object.oid,\n+\t};\n+\tif (dry_run)\n+\t\topts.flags |= RESET_WORKING_TREE_DRY_RUN;\n+\treturn reset_working_tree(repo, &opts);\n+}\n+\n+static int find_head_tree_change(struct repository *repo,\n+\t\t\t\t const struct replay_result *result,\n+\t\t\t\t struct commit **old_head,\n+\t\t\t\t struct commit **new_head,\n+\t\t\t\t bool *changed)\n+{\n+\tconst struct replay_ref_update *head_update = NULL;\n+\tstruct commit *old_head_commit, *new_head_commit;\n+\tstruct tree *old_head_tree, *new_head_tree;\n+\tconst char *head_target;\n+\tint head_flags;\n+\n+\t*changed = false;\n+\n+\thead_target = refs_resolve_ref_unsafe(get_main_ref_store(repo),\n+\t\t\t\t\t      \"HEAD\", RESOLVE_REF_NO_RECURSE,\n+\t\t\t\t\t      NULL, &head_flags);\n+\tif (!head_target)\n+\t\treturn error(_(\"cannot look up HEAD\"));\n+\tif (!(head_flags & REF_ISSYMREF))\n+\t\thead_target = \"HEAD\";\n+\n+\tfor (size_t i = 0; i < result->updates_nr; i++) {\n+\t\tif (!strcmp(result->updates[i].refname, head_target)) {\n+\t\t\thead_update = &result->updates[i];\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\n+\tif (!head_update)\n+\t\treturn 0;\n+\n+\told_head_commit = lookup_commit_reference(repo, &head_update->old_oid);\n+\tnew_head_commit = lookup_commit_reference(repo, &head_update->new_oid);\n+\tif (!old_head_commit || !new_head_commit)\n+\t\treturn error(_(\"cannot resolve HEAD commit\"));\n+\n+\told_head_tree = repo_get_commit_tree(repo, old_head_commit);\n+\tnew_head_tree = repo_get_commit_tree(repo, new_head_commit);\n+\tif (!old_head_tree || !new_head_tree)\n+\t\treturn error(_(\"cannot resolve tree for HEAD\"));\n+\n+\tif (oideq(&old_head_tree->object.oid, &new_head_tree->object.oid))\n+\t\treturn 0;\n+\n+\t*old_head = old_head_commit;\n+\t*new_head = new_head_commit;\n+\t*changed = true;\n+\n+\treturn 0;\n+}\n+\n+static int cmd_history_drop(int argc,\n+\t\t\t    const char **argv,\n+\t\t\t    const char *prefix,\n+\t\t\t    struct repository *repo)\n+{\n+\tconst char * const usage[] = {\n+\t\tGIT_HISTORY_DROP_USAGE,\n+\t\tNULL,\n+\t};\n+\tenum replay_empty_commit_action empty = REPLAY_EMPTY_COMMIT_DROP;\n+\tenum ref_action action = REF_ACTION_DEFAULT;\n+\tint dry_run = 0;\n+\tstruct option options[] = {\n+\t\tOPT_CALLBACK_F(0, \"update-refs\", &action, \"(branches|head)\",\n+\t\t\t       N_(\"control which refs should be updated\"),\n+\t\t\t       PARSE_OPT_NONEG, parse_ref_action),\n+\t\tOPT_BOOL('n', \"dry-run\", &dry_run,\n+\t\t\t N_(\"perform a dry-run without updating any refs\")),\n+\t\tOPT_CALLBACK_F(0, \"empty\", &empty, \"(drop|keep|abort)\",\n+\t\t\t       N_(\"how to handle descendants that become empty\"),\n+\t\t\t       PARSE_OPT_NONEG, parse_opt_empty),\n+\t\tOPT_END(),\n+\t};\n+\tstruct strbuf reflog_msg = STRBUF_INIT;\n+\tstruct commit *original, *rewritten;\n+\tstruct rev_info revs = { 0 };\n+\tstruct replay_result result = { 0 };\n+\tstruct commit *old_head, *new_head;\n+\tbool head_moves = false;\n+\tint ret;\n+\n+\targc = parse_options(argc, argv, prefix, options, usage, 0);\n+\tif (argc != 1) {\n+\t\tret = error(_(\"command expects a single revision\"));\n+\t\tgoto out;\n+\t}\n+\trepo_config(repo, git_default_config, NULL);\n+\n+\tif (action == REF_ACTION_DEFAULT)\n+\t\taction = REF_ACTION_BRANCHES;\n+\n+\toriginal = lookup_commit_reference_by_name(argv[0]);\n+\tif (!original) {\n+\t\tret = error(_(\"commit cannot be found: %s\"), argv[0]);\n+\t\tgoto out;\n+\t}\n+\n+\tif (!original->parents) {\n+\t\tret = error(_(\"cannot drop root commit %s: \"\n+\t\t\t      \"it has no parent to replay onto\"),\n+\t\t\t    argv[0]);\n+\t\tgoto out;\n+\t} else if (original->parents->next) {\n+\t\tret = error(_(\"cannot drop merge commit: %s\"), argv[0]);\n+\t\tgoto out;\n+\t}\n+\n+\tret = setup_revwalk(repo, action, original, &revs);\n+\tif (ret)\n+\t\tgoto out;\n+\n+\trewritten = original->parents->item;\n+\n+\tret = compute_pending_ref_updates(&revs, action, original, rewritten,\n+\t\t\t\t\t  empty, &result);\n+\tif (ret) {\n+\t\tret = error(_(\"failed replaying descendants\"));\n+\t\tgoto out;\n+\t}\n+\n+\t/*\n+\t * If HEAD will move as a result of the rewrite then we'll have to\n+\t * merge in the changes into the worktree and index. This merge can of\n+\t * course conflict, which will cause the whole operation to abort.\n+\t *\n+\t * If we had already updated the refs at that point then we'd have an\n+\t * inconsistent repository state. So we first perform a dry-run merge\n+\t * here before updating refs.\n+\t */\n+\tif (!is_bare_repository()) {\n+\t\tret = find_head_tree_change(repo, &result, &old_head,\n+\t\t\t\t\t    &new_head, &head_moves);\n+\t\tif (ret < 0)\n+\t\t\tgoto out;\n+\n+\t\tif (head_moves && update_worktree(repo, old_head, new_head, true) < 0) {\n+\t\t\tret = error(_(\"dropping this commit would \"\n+\t\t\t\t      \"overwrite local changes; aborting\"));\n+\t\t\tgoto out;\n+\t\t}\n+\t}\n+\n+\tstrbuf_addf(&reflog_msg, \"drop: dropping %s\", argv[0]);\n+\tret = apply_pending_ref_updates(repo, &result, reflog_msg.buf, dry_run);\n+\tif (ret < 0) {\n+\t\tret = error(_(\"failed to update references\"));\n+\t\tgoto out;\n+\t}\n+\n+\tif (!dry_run && head_moves && update_worktree(repo, old_head, new_head, false) < 0) {\n+\t\tret = error(_(\"could not update working tree to new commit %s\"),\n+\t\t\t    oid_to_hex(&new_head->object.oid));\n+\t\tgoto out;\n+\t}\n+\n+\tret = 0;\n+\n+out:\n+\treplay_result_release(&result);\n+\tstrbuf_release(&reflog_msg);\n+\trelease_revisions(&revs);\n+\treturn ret;\n+}\n+\n int cmd_history(int argc,\n \t\tconst char **argv,\n \t\tconst char *prefix,\n \t\tstruct repository *repo)\n {\n \tconst char * const usage[] = {\n+\t\tGIT_HISTORY_DROP_USAGE,\n \t\tGIT_HISTORY_FIXUP_USAGE,\n \t\tGIT_HISTORY_REWORD_USAGE,\n \t\tGIT_HISTORY_SPLIT_USAGE,\n@@ -1014,6 +1199,7 @@ int cmd_history(int argc,\n \t};\n \tparse_opt_subcommand_fn *fn = NULL;\n \tstruct option options[] = {\n+\t\tOPT_SUBCOMMAND(\"drop\", &fn, cmd_history_drop),\n \t\tOPT_SUBCOMMAND(\"fixup\", &fn, cmd_history_fixup),\n \t\tOPT_SUBCOMMAND(\"reword\", &fn, cmd_history_reword),\n \t\tOPT_SUBCOMMAND(\"split\", &fn, cmd_history_split),\ndiff --git a/t/meson.build b/t/meson.build\nindex 2af8d01279..d5e71056b2 100644\n--- a/t/meson.build\n+++ b/t/meson.build\n@@ -399,6 +399,7 @@ integration_tests = [\n   't3451-history-reword.sh',\n   't3452-history-split.sh',\n   't3453-history-fixup.sh',\n+  't3454-history-drop.sh',\n   't3500-cherry.sh',\n   't3501-revert-cherry-pick.sh',\n   't3502-cherry-pick-merge.sh',\ndiff --git a/t/t3454-history-drop.sh b/t/t3454-history-drop.sh\nnew file mode 100755\nindex 0000000000..0f33247212\n--- /dev/null\n+++ b/t/t3454-history-drop.sh\n@@ -0,0 +1,537 @@\n+#!/bin/sh\n+\n+test_description='tests for git-history drop subcommand'\n+\n+. ./test-lib.sh\n+. \"$TEST_DIRECTORY/lib-log-graph.sh\"\n+\n+expect_graph () {\n+\tcat >expect &&\n+\tlib_test_cmp_graph --format=%s \"$@\"\n+}\n+\n+expect_log () {\n+\tgit log --format=\"%s\" \"$@\" >actual &&\n+\tcat >expect &&\n+\ttest_cmp expect actual\n+}\n+\n+test_expect_success 'errors on missing commit argument' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit initial &&\n+\t\ttest_must_fail git history drop 2>err &&\n+\t\ttest_grep \"command expects a single revision\" err\n+\t)\n+'\n+\n+test_expect_success 'errors on too many arguments' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit initial &&\n+\t\ttest_must_fail git history drop HEAD HEAD 2>err &&\n+\t\ttest_grep \"command expects a single revision\" err\n+\t)\n+'\n+\n+test_expect_success 'errors on unknown revision' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit initial &&\n+\t\ttest_must_fail git history drop does-not-exist 2>err &&\n+\t\ttest_grep \"commit cannot be found: does-not-exist\" err\n+\t)\n+'\n+\n+test_expect_success 'errors with invalid --empty= value' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit initial &&\n+\t\ttest_commit second &&\n+\t\ttest_must_fail git history drop --empty=bogus HEAD 2>err &&\n+\t\ttest_grep \"unrecognized.*--empty.*bogus\" err\n+\t)\n+'\n+\n+test_expect_success 'drops a commit in the middle and replays descendants' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\n+\t\tgit symbolic-ref HEAD >expect &&\n+\t\tgit history drop HEAD~ &&\n+\t\tgit symbolic-ref HEAD >actual &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\texpect_log <<-\\EOF &&\n+\t\tthird\n+\t\tfirst\n+\t\tEOF\n+\n+\t\ttest_must_fail git show HEAD:second.t &&\n+\t\ttest_path_is_missing second.t &&\n+\n+\t\tgit reflog >reflog &&\n+\t\ttest_grep \"drop: dropping HEAD~\" reflog\n+\t)\n+'\n+\n+test_expect_success 'drops the HEAD commit' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\n+\t\tgit history drop HEAD &&\n+\n+\t\texpect_log <<-\\EOF\n+\t\tfirst\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'drops a commit on detached HEAD' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\t\tgit checkout --detach HEAD &&\n+\n+\t\tgit history drop HEAD~ &&\n+\n+\t\texpect_log <<-\\EOF\n+\t\tthird\n+\t\tfirst\n+\t\tEOF\n+\t)\n+'\n+\n+# Note: in this case it would actually be fine to drop the root commit, as we\n+# do have a descendant commit, and no reference points to the root commit\n+# directly. So this is something that we may relax eventually.\n+test_expect_success 'refuses to drop the root commit' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\n+\t\ttest_must_fail git history drop HEAD~ 2>err &&\n+\t\ttest_grep \"cannot drop root commit\" err\n+\t)\n+'\n+\n+# In contrast to the above case, we actually don't want to drop the root commit\n+# here as that would cause us to end up with an empty commit graph.\n+test_expect_success 'refuses to drop the root commit when branch becomes empty' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\n+\t\ttest_must_fail git history drop HEAD 2>err &&\n+\t\ttest_grep \"cannot drop root commit\" err\n+\t)\n+'\n+\n+test_expect_success 'refuses to drop a merge commit' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\tgit branch branch &&\n+\t\ttest_commit ours &&\n+\t\tgit switch branch &&\n+\t\ttest_commit theirs &&\n+\t\tgit switch - &&\n+\t\tgit merge theirs &&\n+\n+\t\ttest_must_fail git history drop HEAD 2>err &&\n+\t\ttest_grep \"cannot drop merge commit\" err\n+\t)\n+'\n+\n+test_expect_success 'refuses when descendants contain a merge commit' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\ttest_commit middle &&\n+\t\tgit branch branch &&\n+\t\ttest_commit ours &&\n+\t\tgit switch branch &&\n+\t\ttest_commit theirs &&\n+\t\tgit switch - &&\n+\t\tgit merge theirs &&\n+\n+\t\ttest_must_fail git history drop middle 2>err &&\n+\t\ttest_grep \"replaying merge commits is not supported yet\" err\n+\t)\n+'\n+\n+test_expect_success 'works in a bare repository' '\n+\ttest_when_finished \"rm -rf repo repo.git\" &&\n+\n+\tgit init repo &&\n+\ttest_commit -C repo first &&\n+\ttest_commit -C repo second &&\n+\ttest_commit -C repo third &&\n+\n+\tgit clone --bare repo repo.git &&\n+\t(\n+\t\tcd repo.git &&\n+\n+\t\tgit history drop HEAD~ &&\n+\t\texpect_log <<-\\EOF\n+\t\tthird\n+\t\tfirst\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'updates branches on other lines of descent' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\ttest_commit target &&\n+\t\tgit branch theirs &&\n+\t\ttest_commit ours &&\n+\t\tgit switch theirs &&\n+\t\ttest_commit theirs &&\n+\n+\t\texpect_graph --branches <<-\\EOF &&\n+\t\t* theirs\n+\t\t| * ours\n+\t\t|/\n+\t\t* target\n+\t\t* base\n+\t\tEOF\n+\n+\t\tgit history drop target &&\n+\n+\t\texpect_graph --branches <<-\\EOF\n+\t\t* ours\n+\t\t| * theirs\n+\t\t|/\n+\t\t* base\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'moves branch pointing at dropped commit to its parent' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo --initial-branch=main &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\tgit branch points-at-second &&\n+\t\ttest_commit third &&\n+\n+\t\tgit rev-parse first >expect &&\n+\t\tgit history drop second &&\n+\t\tgit rev-parse points-at-second >actual &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\texpect_log --format=\"%s %D\" --branches <<-\\EOF\n+\t\tthird HEAD -> main\n+\t\tfirst tag: first, points-at-second\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--dry-run prints ref updates without modifying repo' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo --initial-branch=main &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\tgit branch branch &&\n+\t\ttest_commit middle &&\n+\t\ttest_commit ours &&\n+\t\tgit switch branch &&\n+\t\ttest_commit theirs &&\n+\n+\t\tgit refs list >refs-expect &&\n+\t\tgit history drop --dry-run main~ >updates &&\n+\t\tgit refs list >refs-actual &&\n+\t\ttest_cmp refs-expect refs-actual &&\n+\t\ttest_grep \"update refs/heads/main\" updates &&\n+\n+\t\tgit update-ref --stdin <updates &&\n+\t\texpect_log main <<-\\EOF\n+\t\tours\n+\t\tbase\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--dry-run detects conflicts with modified working tree' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo --initial-branch=main &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second modify-me &&\n+\t\techo modified >modify-me &&\n+\n+\t\tgit refs list >refs-expect &&\n+\t\tgit diff >diff-expect &&\n+\t\ttest_must_fail git history drop --dry-run HEAD 2>err &&\n+\t\ttest_grep \"dropping this commit would overwrite local changes\" err &&\n+\t\tgit diff >diff-actual &&\n+\t\tgit refs list >refs-actual &&\n+\n+\t\ttest_cmp diff-expect diff-actual &&\n+\t\ttest_cmp refs-expect refs-actual\n+\t)\n+'\n+\n+test_expect_success '--update-refs=head updates only HEAD' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo --initial-branch=main &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\ttest_commit target &&\n+\t\tgit branch theirs &&\n+\t\ttest_commit ours &&\n+\t\tgit switch theirs &&\n+\t\ttest_commit theirs &&\n+\n+\t\t# When told to update HEAD only, the command refuses to\n+\t\t# rewrite commits that are not an ancestor of HEAD.\n+\t\ttest_must_fail git history drop --update-refs=head main 2>err &&\n+\t\ttest_grep \"rewritten commit must be an ancestor of HEAD\" err &&\n+\n+\t\texpect_graph --branches <<-\\EOF &&\n+\t\t* theirs\n+\t\t| * ours\n+\t\t|/\n+\t\t* target\n+\t\t* base\n+\t\tEOF\n+\n+\t\tgit switch main &&\n+\t\tgit history drop --update-refs=head target &&\n+\n+\t\texpect_graph --branches <<-\\EOF\n+\t\t* ours\n+\t\t| * theirs\n+\t\t| * target\n+\t\t|/\n+\t\t* base\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'conflict with replayed commit aborts cleanly' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\ttest_commit conflict-a file &&\n+\t\ttest_commit conflict-b file &&\n+\n+\t\tgit refs list >refs-expect &&\n+\t\ttest_must_fail git history drop HEAD~ 2>err &&\n+\t\ttest_grep \"failed replaying descendants\" err &&\n+\t\tgit refs list >refs-actual &&\n+\t\ttest_cmp refs-expect refs-actual\n+\t)\n+'\n+\n+# Build a history where a descendant of the drop target reverts the change\n+# introduced by the drop target. After dropping, the descendant's diff applies\n+# against a tree that already lacks the change, so it becomes empty.\n+setup_empty_descendant_repo () {\n+\tgit init \"$1\" &&\n+\t(\n+\t\tcd \"$1\" &&\n+\t\techo C1 >file &&\n+\t\tgit add file &&\n+\t\tgit commit -m \"base\" &&\n+\t\tgit tag base &&\n+\t\techo C2 >file &&\n+\t\tgit add file &&\n+\t\tgit commit -m \"drop-me\" &&\n+\t\tgit tag drop-me &&\n+\t\ttest_commit middle &&\n+\t\techo C1 >file &&\n+\t\tgit add file &&\n+\t\tgit commit -m \"revert-drop-me\" &&\n+\t\tgit tag revert-drop-me\n+\t)\n+}\n+\n+test_expect_success '--empty=drop drops descendants that become empty' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tsetup_empty_descendant_repo repo &&\n+\t(\n+\t\tcd repo &&\n+\n+\t\tgit history drop --empty=drop drop-me &&\n+\n+\t\texpect_log <<-\\EOF\n+\t\tmiddle\n+\t\tbase\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--empty=keep keeps descendants that become empty' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tsetup_empty_descendant_repo repo &&\n+\t(\n+\t\tcd repo &&\n+\n+\t\tgit history drop --empty=keep drop-me &&\n+\n+\t\texpect_log <<-\\EOF &&\n+\t\trevert-drop-me\n+\t\tmiddle\n+\t\tbase\n+\t\tEOF\n+\t\tgit diff HEAD~ HEAD >diff &&\n+\t\ttest_must_be_empty diff\n+\t)\n+'\n+\n+test_expect_success '--empty=abort errors out when a descendant becomes empty' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tsetup_empty_descendant_repo repo &&\n+\t(\n+\t\tcd repo &&\n+\n+\t\ttest_must_fail git history drop --empty=abort drop-me 2>err &&\n+\t\ttest_grep \"became empty after replay\" err\n+\t)\n+'\n+\n+test_expect_success 'updates index and worktree when HEAD moves' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\n+\t\tgit history drop second &&\n+\n+\t\t# Worktree should no longer contain second.t.\n+\t\ttest_path_is_missing second.t &&\n+\t\ttest_path_is_file first.t &&\n+\t\ttest_path_is_file third.t &&\n+\n+\t\t# Index and worktree should both match the new HEAD.\n+\t\tgit status --porcelain --untracked-files=no >status &&\n+\t\ttest_must_be_empty status\n+\t)\n+'\n+\n+test_expect_success 'updates worktree when dropping HEAD itself' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\n+\t\tgit history drop HEAD &&\n+\n+\t\ttest_path_is_missing second.t &&\n+\t\ttest_path_is_file first.t &&\n+\n+\t\tgit status --porcelain --untracked-files=no >status &&\n+\t\ttest_must_be_empty status\n+\t)\n+'\n+\n+test_expect_success 'preserves unrelated unstaged modifications' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\techo first-content >unrelated.txt &&\n+\t\tgit add unrelated.txt &&\n+\t\tgit commit -m \"add unrelated\" &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\n+\t\techo locally-modified >unrelated.txt &&\n+\n+\t\tgit diff >diff-expect &&\n+\t\tgit history drop second &&\n+\t\tgit diff >diff-actual &&\n+\t\ttest_cmp diff-expect diff-actual &&\n+\t\ttest_path_is_missing second.t\n+\t)\n+'\n+\n+test_expect_success 'preserves unrelated staged changes' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\techo first-content >unrelated.txt &&\n+\t\tgit add unrelated.txt &&\n+\t\tgit commit -m \"add unrelated\" &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\n+\t\techo staged-change >unrelated.txt &&\n+\t\tgit add unrelated.txt &&\n+\n+\t\tgit diff --cached >diff-expect &&\n+\t\tgit history drop second &&\n+\t\tgit diff --cached >diff-actual &&\n+\t\ttest_cmp diff-expect diff-actual &&\n+\t\ttest_path_is_missing second.t\n+\t)\n+'\n+\n+test_expect_success 'aborts when local modifications would be overwritten' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\ttest_commit conflict &&\n+\n+\t\techo local-edit >conflict.t &&\n+\t\tgit diff >diff-expect &&\n+\t\ttest_must_fail git history drop HEAD 2>err &&\n+\t\ttest_grep \"would overwrite local changes\" err &&\n+\t\tgit diff >diff-actual &&\n+\t\ttest_cmp diff-expect diff-actual\n+\t)\n+'\n+\n+test_done\n\n-- \n2.54.0.1189.g8c84645362.dirty\n\n"},{"id":"545140","messageId":"b2c7a210-f6c3-40f4-9c42-e81c7e316bc9@gmail.com","threadId":"65726","inReplyTo":"20260610-b4-pks-history-drop-v4-3-70d5f0ae8c25@pks.im","subject":"Re: [PATCH v4 03/10] reset: rename `reset_head()`","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-06-10T13:10:47Z","receivedAt":"2026-06-10T13:10:53Z","isPatch":true,"body":"Hi Patrick\n\nOn 10/06/2026 09:52, Patrick Steinhardt wrote:\n> In a subsequent commit we're about to adapt `reset_head()` so that the\n> reference update to HEAD is optional, only. At this point the function\n> starts to feel misnamed, as it doesn't necessarily have anything to do\n> with the HEAD reference anymore. The gist of the function then is that\n> we reset the working tree to a specific new commit, updating both the\n> index and the checked-out files.\n> \n> Rename it to `reset_working_tree()` to better reflect that.\n\nThat sounds good. Because we defer renaming the flags this patch is very \nstraight forward.\n\nThanks\n\nPhillip\n\n> Note that we don't adjust the flags yet. This will happen in a\n> subsequent commit.\n> \n> Suggested-by: Phillip Wood <phillip.wood123@gmail.com>\n> ---\n>   builtin/rebase.c | 20 ++++++++++----------\n>   reset.c          |  5 +++--\n>   reset.h          |  4 ++--\n>   sequencer.c      |  8 ++++----\n>   4 files changed, 19 insertions(+), 18 deletions(-)\n> \n> diff --git a/builtin/rebase.c b/builtin/rebase.c\n> index fa4f5d9306..22fbba3c62 100644\n> --- a/builtin/rebase.c\n> +++ b/builtin/rebase.c\n> @@ -592,7 +592,7 @@ static int finish_rebase(struct rebase_options *opts)\n>   static int move_to_original_branch(struct rebase_options *opts)\n>   {\n>   \tstruct strbuf branch_reflog = STRBUF_INIT, head_reflog = STRBUF_INIT;\n> -\tstruct reset_head_opts ropts = { 0 };\n> +\tstruct reset_working_tree_options ropts = { 0 };\n>   \tint ret;\n>   \n>   \tif (!opts->head_name)\n> @@ -610,7 +610,7 @@ static int move_to_original_branch(struct rebase_options *opts)\n>   \tropts.flags = RESET_HEAD_REFS_ONLY;\n>   \tropts.branch_msg = branch_reflog.buf;\n>   \tropts.head_msg = head_reflog.buf;\n> -\tret = reset_head(the_repository, &ropts);\n> +\tret = reset_working_tree(the_repository, &ropts);\n>   \n>   \tstrbuf_release(&branch_reflog);\n>   \tstrbuf_release(&head_reflog);\n> @@ -685,7 +685,7 @@ static int run_am(struct rebase_options *opts)\n>   \n>   \tstatus = run_command(&format_patch);\n>   \tif (status) {\n> -\t\tstruct reset_head_opts ropts = { 0 };\n> +\t\tstruct reset_working_tree_options ropts = { 0 };\n>   \t\tunlink(rebased_patches);\n>   \t\tfree(rebased_patches);\n>   \t\tchild_process_clear(&am);\n> @@ -693,7 +693,7 @@ static int run_am(struct rebase_options *opts)\n>   \t\tropts.oid = &opts->orig_head->object.oid;\n>   \t\tropts.branch = opts->head_name;\n>   \t\tropts.default_reflog_action = opts->reflog_action;\n> -\t\treset_head(the_repository, &ropts);\n> +\t\treset_working_tree(the_repository, &ropts);\n>   \t\terror(_(\"\\ngit encountered an error while preparing the \"\n>   \t\t\t\"patches to replay\\n\"\n>   \t\t\t\"these revisions:\\n\"\n> @@ -855,7 +855,7 @@ static int rebase_config(const char *var, const char *value,\n>   static int checkout_up_to_date(struct rebase_options *options)\n>   {\n>   \tstruct strbuf buf = STRBUF_INIT;\n> -\tstruct reset_head_opts ropts = { 0 };\n> +\tstruct reset_working_tree_options ropts = { 0 };\n>   \tint ret = 0;\n>   \n>   \tstrbuf_addf(&buf, \"%s: checkout %s\",\n> @@ -866,7 +866,7 @@ static int checkout_up_to_date(struct rebase_options *options)\n>   \tif (!ropts.branch)\n>   \t\tropts.flags |=  RESET_HEAD_DETACH;\n>   \tropts.head_msg = buf.buf;\n> -\tif (reset_head(the_repository, &ropts) < 0)\n> +\tif (reset_working_tree(the_repository, &ropts) < 0)\n>   \t\tret = error(_(\"could not switch to %s\"), options->switch_to);\n>   \tstrbuf_release(&buf);\n>   \n> @@ -1116,7 +1116,7 @@ int cmd_rebase(int argc,\n>   \tint reschedule_failed_exec = -1;\n>   \tint allow_preemptive_ff = 1;\n>   \tint preserve_merges_selected = 0;\n> -\tstruct reset_head_opts ropts = { 0 };\n> +\tstruct reset_working_tree_options ropts = { 0 };\n>   \tstruct option builtin_rebase_options[] = {\n>   \t\tOPT_STRING(0, \"onto\", &options.onto_name,\n>   \t\t\t   N_(\"revision\"),\n> @@ -1385,7 +1385,7 @@ int cmd_rebase(int argc,\n>   \t\trerere_clear(the_repository, &merge_rr);\n>   \t\tstring_list_clear(&merge_rr, 1);\n>   \t\tropts.flags = RESET_HEAD_HARD;\n> -\t\tif (reset_head(the_repository, &ropts) < 0)\n> +\t\tif (reset_working_tree(the_repository, &ropts) < 0)\n>   \t\t\tdie(_(\"could not discard worktree changes\"));\n>   \t\tremove_branch_state(the_repository, 0);\n>   \t\tif (read_basic_state(&options))\n> @@ -1410,7 +1410,7 @@ int cmd_rebase(int argc,\n>   \t\tropts.head_msg = head_msg.buf;\n>   \t\tropts.branch = options.head_name;\n>   \t\tropts.flags = RESET_HEAD_HARD;\n> -\t\tif (reset_head(the_repository, &ropts) < 0)\n> +\t\tif (reset_working_tree(the_repository, &ropts) < 0)\n>   \t\t\tdie(_(\"could not move back to %s\"),\n>   \t\t\t    oid_to_hex(&options.orig_head->object.oid));\n>   \t\tstrbuf_release(&head_msg);\n> @@ -1880,7 +1880,7 @@ int cmd_rebase(int argc,\n>   \t\t\tRESET_HEAD_RUN_POST_CHECKOUT_HOOK;\n>   \tropts.head_msg = msg.buf;\n>   \tropts.default_reflog_action = options.reflog_action;\n> -\tif (reset_head(the_repository, &ropts)) {\n> +\tif (reset_working_tree(the_repository, &ropts)) {\n>   \t\tret = error(_(\"Could not detach HEAD\"));\n>   \t\tgoto cleanup_autostash;\n>   \t}\n> diff --git a/reset.c b/reset.c\n> index 3b3cb74dab..799596398b 100644\n> --- a/reset.c\n> +++ b/reset.c\n> @@ -12,7 +12,7 @@\n>   #include \"hook.h\"\n>   \n>   static int update_refs(struct repository *repo,\n> -\t\t       const struct reset_head_opts *opts,\n> +\t\t       const struct reset_working_tree_options *opts,\n>   \t\t       const struct object_id *oid,\n>   \t\t       const struct object_id *head)\n>   {\n> @@ -85,7 +85,8 @@ static int update_refs(struct repository *repo,\n>   \treturn ret;\n>   }\n>   \n> -int reset_head(struct repository *r, const struct reset_head_opts *opts)\n> +int reset_working_tree(struct repository *r,\n> +\t\t       const struct reset_working_tree_options *opts)\n>   {\n>   \tconst struct object_id *oid = opts->oid;\n>   \tconst char *switch_to_branch = opts->branch;\n> diff --git a/reset.h b/reset.h\n> index a28f81829d..f130152014 100644\n> --- a/reset.h\n> +++ b/reset.h\n> @@ -17,7 +17,7 @@\n>   /* Update ORIG_HEAD as well as HEAD */\n>   #define RESET_ORIG_HEAD (1<<4)\n>   \n> -struct reset_head_opts {\n> +struct reset_working_tree_options {\n>   \t/*\n>   \t * The commit to checkout/reset to. Defaults to HEAD.\n>   \t */\n> @@ -55,6 +55,6 @@ struct reset_head_opts {\n>   \tconst char *default_reflog_action;\n>   };\n>   \n> -int reset_head(struct repository *r, const struct reset_head_opts *opts);\n> +int reset_working_tree(struct repository *r, const struct reset_working_tree_options *opts);\n>   \n>   #endif\n> diff --git a/sequencer.c b/sequencer.c\n> index 1ee4b2875b..d73ecf0384 100644\n> --- a/sequencer.c\n> +++ b/sequencer.c\n> @@ -4677,7 +4677,7 @@ static void create_autostash_internal(struct repository *r,\n>   \tif (has_unstaged_changes(r, 1) ||\n>   \t    has_uncommitted_changes(r, 1)) {\n>   \t\tstruct child_process stash = CHILD_PROCESS_INIT;\n> -\t\tstruct reset_head_opts ropts = { .flags = RESET_HEAD_HARD };\n> +\t\tstruct reset_working_tree_options ropts = { .flags = RESET_HEAD_HARD };\n>   \t\tstruct object_id oid;\n>   \n>   \t\tstrvec_pushl(&stash.args,\n> @@ -4707,7 +4707,7 @@ static void create_autostash_internal(struct repository *r,\n>   \n>   \t\tif (!silent)\n>   \t\t\tprintf(_(\"Created autostash: %s\\n\"), buf.buf);\n> -\t\tif (reset_head(r, &ropts) < 0)\n> +\t\tif (reset_working_tree(r, &ropts) < 0)\n>   \t\t\tdie(_(\"could not reset --hard\"));\n>   \t\tdiscard_index(r->index);\n>   \t\tif (repo_read_index(r) < 0)\n> @@ -4867,7 +4867,7 @@ static int checkout_onto(struct repository *r, struct replay_opts *opts,\n>   \t\t\t const char *onto_name, const struct object_id *onto,\n>   \t\t\t const struct object_id *orig_head)\n>   {\n> -\tstruct reset_head_opts ropts = {\n> +\tstruct reset_working_tree_options ropts = {\n>   \t\t.oid = onto,\n>   \t\t.orig_head = orig_head,\n>   \t\t.flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD |\n> @@ -4876,7 +4876,7 @@ static int checkout_onto(struct repository *r, struct replay_opts *opts,\n>   \t\t\t\t\t   onto_name),\n>   \t\t.default_reflog_action = sequencer_reflog_action(opts)\n>   \t};\n> -\tif (reset_head(r, &ropts)) {\n> +\tif (reset_working_tree(r, &ropts)) {\n>   \t\tapply_autostash(rebase_path_autostash());\n>   \t\tsequencer_remove_state(opts);\n>   \t\treturn error(_(\"could not detach HEAD\"));\n> \n\n"},{"id":"545141","messageId":"f886affe-66fe-4589-babd-937106b9c3fb@gmail.com","threadId":"65726","inReplyTo":"20260610-b4-pks-history-drop-v4-4-70d5f0ae8c25@pks.im","subject":"Re: [PATCH v4 04/10] reset: modernize flags passed to `reset_working_tree()`","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-06-10T13:10:53Z","receivedAt":"2026-06-10T13:10:58Z","isPatch":true,"body":"Hi Patrick\n\nOn 10/06/2026 09:52, Patrick Steinhardt wrote:\n> The flags passed to `reset_working_tree()` are declared as defines. This\n> has fallen a bit out of practice nowadays, where we instead prefer to\n> use enums. Furthermore, the prefix of those flags does not match the\n> function name anymore after the rename in the preceding commit.\n> \n> Adapt the code to follow modern best practices and adapt the flag names.\n\nThis looks good\n\nThanks\n\nPhillip\n\n> \n> Signed-off-by: Patrick Steinhardt <ps@pks.im>\n> ---\n>   builtin/rebase.c | 15 ++++++++-------\n>   reset.c          | 12 ++++++------\n>   reset.h          | 31 +++++++++++++++++++------------\n>   sequencer.c      |  9 ++++++---\n>   4 files changed, 39 insertions(+), 28 deletions(-)\n> \n> diff --git a/builtin/rebase.c b/builtin/rebase.c\n> index 22fbba3c62..06dcbaf5e8 100644\n> --- a/builtin/rebase.c\n> +++ b/builtin/rebase.c\n> @@ -607,7 +607,7 @@ static int move_to_original_branch(struct rebase_options *opts)\n>   \tstrbuf_addf(&head_reflog, \"%s (finish): returning to %s\",\n>   \t\t    opts->reflog_action, opts->head_name);\n>   \tropts.branch = opts->head_name;\n> -\tropts.flags = RESET_HEAD_REFS_ONLY;\n> +\tropts.flags = RESET_WORKING_TREE_REFS_ONLY;\n>   \tropts.branch_msg = branch_reflog.buf;\n>   \tropts.head_msg = head_reflog.buf;\n>   \tret = reset_working_tree(the_repository, &ropts);\n> @@ -862,9 +862,9 @@ static int checkout_up_to_date(struct rebase_options *options)\n>   \t\t    options->reflog_action, options->switch_to);\n>   \tropts.oid = &options->orig_head->object.oid;\n>   \tropts.branch = options->head_name;\n> -\tropts.flags = RESET_HEAD_RUN_POST_CHECKOUT_HOOK;\n> +\tropts.flags = RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK;\n>   \tif (!ropts.branch)\n> -\t\tropts.flags |=  RESET_HEAD_DETACH;\n> +\t\tropts.flags |=  RESET_WORKING_TREE_DETACH;\n>   \tropts.head_msg = buf.buf;\n>   \tif (reset_working_tree(the_repository, &ropts) < 0)\n>   \t\tret = error(_(\"could not switch to %s\"), options->switch_to);\n> @@ -1384,7 +1384,7 @@ int cmd_rebase(int argc,\n>   \n>   \t\trerere_clear(the_repository, &merge_rr);\n>   \t\tstring_list_clear(&merge_rr, 1);\n> -\t\tropts.flags = RESET_HEAD_HARD;\n> +\t\tropts.flags = RESET_WORKING_TREE_HARD;\n>   \t\tif (reset_working_tree(the_repository, &ropts) < 0)\n>   \t\t\tdie(_(\"could not discard worktree changes\"));\n>   \t\tremove_branch_state(the_repository, 0);\n> @@ -1409,7 +1409,7 @@ int cmd_rebase(int argc,\n>   \t\tropts.oid = &options.orig_head->object.oid;\n>   \t\tropts.head_msg = head_msg.buf;\n>   \t\tropts.branch = options.head_name;\n> -\t\tropts.flags = RESET_HEAD_HARD;\n> +\t\tropts.flags = RESET_WORKING_TREE_HARD;\n>   \t\tif (reset_working_tree(the_repository, &ropts) < 0)\n>   \t\t\tdie(_(\"could not move back to %s\"),\n>   \t\t\t    oid_to_hex(&options.orig_head->object.oid));\n> @@ -1876,8 +1876,9 @@ int cmd_rebase(int argc,\n>   \t\t    options.reflog_action, options.onto_name);\n>   \tropts.oid = &options.onto->object.oid;\n>   \tropts.orig_head = &options.orig_head->object.oid;\n> -\tropts.flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD |\n> -\t\t\tRESET_HEAD_RUN_POST_CHECKOUT_HOOK;\n> +\tropts.flags = RESET_WORKING_TREE_DETACH |\n> +\t\t      RESET_WORKING_TREE_UPDATE_ORIG_HEAD |\n> +\t\t      RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK;\n>   \tropts.head_msg = msg.buf;\n>   \tropts.default_reflog_action = options.reflog_action;\n>   \tif (reset_working_tree(the_repository, &ropts)) {\n> diff --git a/reset.c b/reset.c\n> index 799596398b..4ca7f23a25 100644\n> --- a/reset.c\n> +++ b/reset.c\n> @@ -16,9 +16,9 @@ static int update_refs(struct repository *repo,\n>   \t\t       const struct object_id *oid,\n>   \t\t       const struct object_id *head)\n>   {\n> -\tunsigned detach_head = opts->flags & RESET_HEAD_DETACH;\n> -\tunsigned run_hook = opts->flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK;\n> -\tunsigned update_orig_head = opts->flags & RESET_ORIG_HEAD;\n> +\tunsigned detach_head = opts->flags & RESET_WORKING_TREE_DETACH;\n> +\tunsigned run_hook = opts->flags & RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK;\n> +\tunsigned update_orig_head = opts->flags & RESET_WORKING_TREE_UPDATE_ORIG_HEAD;\n>   \tconst struct object_id *orig_head = opts->orig_head;\n>   \tconst char *switch_to_branch = opts->branch;\n>   \tconst char *reflog_branch = opts->branch_msg;\n> @@ -90,9 +90,9 @@ int reset_working_tree(struct repository *r,\n>   {\n>   \tconst struct object_id *oid = opts->oid;\n>   \tconst char *switch_to_branch = opts->branch;\n> -\tunsigned reset_hard = opts->flags & RESET_HEAD_HARD;\n> -\tunsigned refs_only = opts->flags & RESET_HEAD_REFS_ONLY;\n> -\tunsigned update_orig_head = opts->flags & RESET_ORIG_HEAD;\n> +\tunsigned reset_hard = opts->flags & RESET_WORKING_TREE_HARD;\n> +\tunsigned refs_only = opts->flags & RESET_WORKING_TREE_REFS_ONLY;\n> +\tunsigned update_orig_head = opts->flags & RESET_WORKING_TREE_UPDATE_ORIG_HEAD;\n>   \tstruct object_id *head = NULL, head_oid;\n>   \tstruct tree_desc desc[2] = { { NULL }, { NULL } };\n>   \tstruct lock_file lock = LOCK_INIT;\n> diff --git a/reset.h b/reset.h\n> index f130152014..2e5826de99 100644\n> --- a/reset.h\n> +++ b/reset.h\n> @@ -6,16 +6,22 @@\n>   \n>   #define GIT_REFLOG_ACTION_ENVIRONMENT \"GIT_REFLOG_ACTION\"\n>   \n> -/* Request a detached checkout */\n> -#define RESET_HEAD_DETACH (1<<0)\n> -/* Request a reset rather than a checkout */\n> -#define RESET_HEAD_HARD (1<<1)\n> -/* Run the post-checkout hook */\n> -#define RESET_HEAD_RUN_POST_CHECKOUT_HOOK (1<<2)\n> -/* Only update refs, do not touch the worktree */\n> -#define RESET_HEAD_REFS_ONLY (1<<3)\n> -/* Update ORIG_HEAD as well as HEAD */\n> -#define RESET_ORIG_HEAD (1<<4)\n> +enum reset_working_tree_flags {\n> +\t/* Request a detached checkout */\n> +\tRESET_WORKING_TREE_DETACH = (1 << 0),\n> +\n> +\t/* Request a reset rather than a checkout */\n> +\tRESET_WORKING_TREE_HARD = (1 << 1),\n> +\n> +\t/* Run the post-checkout hook */\n> +\tRESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK = (1 << 2),\n> +\n> +\t/* Only update refs, do not touch the worktree */\n> +\tRESET_WORKING_TREE_REFS_ONLY = (1 << 3),\n> +\n> +\t/* Update ORIG_HEAD as well as HEAD */\n> +\tRESET_WORKING_TREE_UPDATE_ORIG_HEAD = (1 << 4),\n> +};\n>   \n>   struct reset_working_tree_options {\n>   \t/*\n> @@ -33,7 +39,7 @@ struct reset_working_tree_options {\n>   \t/*\n>   \t * Flags defined above.\n>   \t */\n> -\tunsigned flags;\n> +\tenum reset_working_tree_flags flags;\n>   \t/*\n>   \t * Optional reflog message for branch, defaults to head_msg.\n>   \t */\n> @@ -45,7 +51,8 @@ struct reset_working_tree_options {\n>   \tconst char *head_msg;\n>   \t/*\n>   \t * Optional reflog message for ORIG_HEAD, if this omitted and flags\n> -\t * contains RESET_ORIG_HEAD then default_reflog_action must be given.\n> +\t * contains RESET_WORKING_TREE_UPDATE_ORIG_HEAD then\n> +\t * default_reflog_action must be given.\n>   \t */\n>   \tconst char *orig_head_msg;\n>   \t/*\n> diff --git a/sequencer.c b/sequencer.c\n> index d73ecf0384..4efe831178 100644\n> --- a/sequencer.c\n> +++ b/sequencer.c\n> @@ -4677,7 +4677,9 @@ static void create_autostash_internal(struct repository *r,\n>   \tif (has_unstaged_changes(r, 1) ||\n>   \t    has_uncommitted_changes(r, 1)) {\n>   \t\tstruct child_process stash = CHILD_PROCESS_INIT;\n> -\t\tstruct reset_working_tree_options ropts = { .flags = RESET_HEAD_HARD };\n> +\t\tstruct reset_working_tree_options ropts = {\n> +\t\t\t.flags = RESET_WORKING_TREE_HARD,\n> +\t\t};\n>   \t\tstruct object_id oid;\n>   \n>   \t\tstrvec_pushl(&stash.args,\n> @@ -4870,8 +4872,9 @@ static int checkout_onto(struct repository *r, struct replay_opts *opts,\n>   \tstruct reset_working_tree_options ropts = {\n>   \t\t.oid = onto,\n>   \t\t.orig_head = orig_head,\n> -\t\t.flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD |\n> -\t\t\t\tRESET_HEAD_RUN_POST_CHECKOUT_HOOK,\n> +\t\t.flags = RESET_WORKING_TREE_DETACH |\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.default_reflog_action = sequencer_reflog_action(opts)\n> \n\n"},{"id":"545142","messageId":"cfade236-75bc-4679-a74a-6da82e6a5135@gmail.com","threadId":"65726","inReplyTo":"20260610-b4-pks-history-drop-v4-6-70d5f0ae8c25@pks.im","subject":"Re: [PATCH v4 06/10] reset: introduce ability to skip updating HEAD","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-06-10T13:11:04Z","receivedAt":"2026-06-10T13:11:10Z","isPatch":true,"body":"Hi Patrick\n\nOn 10/06/2026 09:52, Patrick Steinhardt wrote:\n> In a subsequent commit we'll introduce a new caller to\n> `reset_working_tree()` that really only wants to update the index and\n> working tree, without updating any references. Introduce a new flag that\n> makes the caller opt in to updating HEAD and adapt all callers to set\n> that flag.\n> \n> Note that in a previous iteration we instead introduced a flag that made\n> callers opt out of updating any references. This was somewhat awkward\n> though because we already have the `UPDATE_ORIG_HEAD` flag, so the\n> result was somewhat inconsistent.\n\nThanks for doing this. I've grepped for all the callers of reset_head() \nto confirm this patch adds RESET_HEAD_UPDATE_HEAD to them all.\n\nI wonder if we should add a check for passing \nRESET_HEAD_UPDATE_ORIG_HEAD without RESET_HEAD_UPDATE_HEAD that calls \nBUG() as we don't support that. Everything else looks good.\n\nThanks\n\nPhillip\n\n> Suggested-by: Phillip Wood <phillip.wood123@gmail.com>\n> Signed-off-by: Patrick Steinhardt <ps@pks.im>\n> ---\n>   builtin/rebase.c | 14 ++++++++++----\n>   reset.c          |  6 ++++--\n>   reset.h          |  9 ++++++---\n>   sequencer.c      |  4 +++-\n>   4 files changed, 23 insertions(+), 10 deletions(-)\n> \n> diff --git a/builtin/rebase.c b/builtin/rebase.c\n> index 06dcbaf5e8..10a306310c 100644\n> --- a/builtin/rebase.c\n> +++ b/builtin/rebase.c\n> @@ -607,7 +607,8 @@ static int move_to_original_branch(struct rebase_options *opts)\n>   \tstrbuf_addf(&head_reflog, \"%s (finish): returning to %s\",\n>   \t\t    opts->reflog_action, opts->head_name);\n>   \tropts.branch = opts->head_name;\n> -\tropts.flags = RESET_WORKING_TREE_REFS_ONLY;\n> +\tropts.flags = RESET_WORKING_TREE_REFS_ONLY |\n> +\t\t      RESET_WORKING_TREE_UPDATE_HEAD;\n>   \tropts.branch_msg = branch_reflog.buf;\n>   \tropts.head_msg = head_reflog.buf;\n>   \tret = reset_working_tree(the_repository, &ropts);\n> @@ -693,6 +694,7 @@ static int run_am(struct rebase_options *opts)\n>   \t\tropts.oid = &opts->orig_head->object.oid;\n>   \t\tropts.branch = opts->head_name;\n>   \t\tropts.default_reflog_action = opts->reflog_action;\n> +\t\tropts.flags = RESET_WORKING_TREE_UPDATE_HEAD;\n>   \t\treset_working_tree(the_repository, &ropts);\n>   \t\terror(_(\"\\ngit encountered an error while preparing the \"\n>   \t\t\t\"patches to replay\\n\"\n> @@ -862,7 +864,8 @@ static int checkout_up_to_date(struct rebase_options *options)\n>   \t\t    options->reflog_action, options->switch_to);\n>   \tropts.oid = &options->orig_head->object.oid;\n>   \tropts.branch = options->head_name;\n> -\tropts.flags = RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK;\n> +\tropts.flags = RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK |\n> +\t\t      RESET_WORKING_TREE_UPDATE_HEAD;\n>   \tif (!ropts.branch)\n>   \t\tropts.flags |=  RESET_WORKING_TREE_DETACH;\n>   \tropts.head_msg = buf.buf;\n> @@ -1384,7 +1387,8 @@ int cmd_rebase(int argc,\n>   \n>   \t\trerere_clear(the_repository, &merge_rr);\n>   \t\tstring_list_clear(&merge_rr, 1);\n> -\t\tropts.flags = RESET_WORKING_TREE_HARD;\n> +\t\tropts.flags = RESET_WORKING_TREE_HARD |\n> +\t\t\t      RESET_WORKING_TREE_UPDATE_HEAD;\n>   \t\tif (reset_working_tree(the_repository, &ropts) < 0)\n>   \t\t\tdie(_(\"could not discard worktree changes\"));\n>   \t\tremove_branch_state(the_repository, 0);\n> @@ -1409,7 +1413,8 @@ int cmd_rebase(int argc,\n>   \t\tropts.oid = &options.orig_head->object.oid;\n>   \t\tropts.head_msg = head_msg.buf;\n>   \t\tropts.branch = options.head_name;\n> -\t\tropts.flags = RESET_WORKING_TREE_HARD;\n> +\t\tropts.flags = RESET_WORKING_TREE_HARD |\n> +\t\t\t      RESET_WORKING_TREE_UPDATE_HEAD;\n>   \t\tif (reset_working_tree(the_repository, &ropts) < 0)\n>   \t\t\tdie(_(\"could not move back to %s\"),\n>   \t\t\t    oid_to_hex(&options.orig_head->object.oid));\n> @@ -1877,6 +1882,7 @@ int cmd_rebase(int argc,\n>   \tropts.oid = &options.onto->object.oid;\n>   \tropts.orig_head = &options.orig_head->object.oid;\n>   \tropts.flags = RESET_WORKING_TREE_DETACH |\n> +\t\t      RESET_WORKING_TREE_UPDATE_HEAD |\n>   \t\t      RESET_WORKING_TREE_UPDATE_ORIG_HEAD |\n>   \t\t      RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK;\n>   \tropts.head_msg = msg.buf;\n> diff --git a/reset.c b/reset.c\n> index 99f2c1b012..3ac99a51c0 100644\n> --- a/reset.c\n> +++ b/reset.c\n> @@ -92,6 +92,7 @@ int reset_working_tree(struct repository *r,\n>   \tconst char *switch_to_branch = opts->branch;\n>   \tunsigned reset_hard = opts->flags & RESET_WORKING_TREE_HARD;\n>   \tunsigned refs_only = opts->flags & RESET_WORKING_TREE_REFS_ONLY;\n> +\tunsigned update_head = opts->flags & RESET_WORKING_TREE_UPDATE_HEAD;\n>   \tunsigned update_orig_head = opts->flags & RESET_WORKING_TREE_UPDATE_ORIG_HEAD;\n>   \tunsigned dry_run = opts->flags & RESET_WORKING_TREE_DRY_RUN;\n>   \tstruct object_id *head = NULL, head_oid;\n> @@ -129,7 +130,7 @@ int reset_working_tree(struct repository *r,\n>   \t\toid = &head_oid;\n>   \n>   \tif (refs_only) {\n> -\t\tif (!dry_run)\n> +\t\tif (update_head)\n>   \t\t\treturn update_refs(r, opts, oid, head);\n>   \t\treturn 0;\n>   \t}\n> @@ -197,7 +198,8 @@ int reset_working_tree(struct repository *r,\n>   \t\tgoto leave_reset_head;\n>   \t}\n>   \n> -\tif (oid != &head_oid || update_orig_head || switch_to_branch)\n> +\tif (update_head &&\n> +\t    (oid != &head_oid || update_orig_head || switch_to_branch))\n>   \t\tret = update_refs(r, opts, oid, head);\n>   \n>   leave_reset_head:\n> diff --git a/reset.h b/reset.h\n> index 898e4a1e95..38b2891b53 100644\n> --- a/reset.h\n> +++ b/reset.h\n> @@ -19,14 +19,17 @@ enum reset_working_tree_flags {\n>   \t/* Only update refs, do not touch the worktree */\n>   \tRESET_WORKING_TREE_REFS_ONLY = (1 << 3),\n>   \n> -\t/* Update ORIG_HEAD as well as HEAD */\n> -\tRESET_WORKING_TREE_UPDATE_ORIG_HEAD = (1 << 4),\n> +\t/* Update HEAD */\n> +\tRESET_WORKING_TREE_UPDATE_HEAD = (1 << 4),\n> +\n> +\t/* Update ORIG_HEAD */\n> +\tRESET_WORKING_TREE_UPDATE_ORIG_HEAD = (1 << 5),\n>   \n>   \t/*\n>   \t * Perform a dry-run by performing the operation without updating\n>   \t * any user-visible state.\n>   \t */\n> -\tRESET_WORKING_TREE_DRY_RUN = (1 << 5),\n> +\tRESET_WORKING_TREE_DRY_RUN = (1 << 6),\n>   };\n>   \n>   struct reset_working_tree_options {\n> diff --git a/sequencer.c b/sequencer.c\n> index 4efe831178..e905b1b2d9 100644\n> --- a/sequencer.c\n> +++ b/sequencer.c\n> @@ -4678,7 +4678,8 @@ static void create_autostash_internal(struct repository *r,\n>   \t    has_uncommitted_changes(r, 1)) {\n>   \t\tstruct child_process stash = CHILD_PROCESS_INIT;\n>   \t\tstruct reset_working_tree_options ropts = {\n> -\t\t\t.flags = RESET_WORKING_TREE_HARD,\n> +\t\t\t.flags = RESET_WORKING_TREE_HARD |\n> +\t\t\t\t RESET_WORKING_TREE_UPDATE_HEAD,\n>   \t\t};\n>   \t\tstruct object_id oid;\n>   \n> @@ -4873,6 +4874,7 @@ static int checkout_onto(struct repository *r, struct replay_opts *opts,\n>   \t\t.oid = onto,\n>   \t\t.orig_head = orig_head,\n>   \t\t.flags = RESET_WORKING_TREE_DETACH |\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> \n\n"},{"id":"545170","messageId":"xmqqwlw6weey.fsf@gitster.g","threadId":"65726","inReplyTo":"20260610-b4-pks-history-drop-v4-9-70d5f0ae8c25@pks.im","subject":"Re: [PATCH v4 09/10] builtin/history: split handling of ref updates into two phases","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-06-10T16:25:41Z","receivedAt":"2026-06-10T16:25:44Z","isPatch":true,"body":"Patrick Steinhardt <ps@pks.im> writes:\n\n> The function `handle_reference_updates()` is used by git-history(1) to\n> update all references that refer to commits that have been rewritten. As\n> such, it performs two steps:\n>\n>   - It gathers the references that need to be updated in the first\n>     place.\n>\n>   - It prepares and commits the reference transaction.\n>\n> In a subsequent commit we'll want to handle those two steps separately.\n> Prepare for this by splitting up the function into two.\n\nOK.  I can sees how this will help doing a dry-run mode.\n\n> +static int apply_pending_ref_updates(struct repository *repo,\n> +\t\t\t\t     const struct replay_result *result,\n> +\t\t\t\t     const char *reflog_msg,\n> +\t\t\t\t     int dry_run)\n> +{\n> +\tstruct ref_transaction *transaction = NULL;\n> +\tstruct strbuf err = STRBUF_INIT;\n> +\tint ret;\n> +\n> +\tif (!dry_run) {\n> +\t\ttransaction = ref_store_transaction_begin(get_main_ref_store(repo),\n> +\t\t\t\t\t\t\t  0, &err);\n> +\t\tif (!transaction) {\n> +\t\t\tret = error(_(\"failed to begin ref transaction: %s\"), err.buf);\n> +\t\t\tgoto out;\n> +\t\t}\n> +\t}\n> +\n> +\tfor (size_t i = 0; i < result->updates_nr; i++) {\n>  \t\tret = handle_ref_update(transaction,\n> -\t\t\t\t\tdecoration->name,\n> -\t\t\t\t\t&rewritten->object.oid,\n> -\t\t\t\t\t&original->object.oid,\n> +\t\t\t\t\tresult->updates[i].refname,\n> +\t\t\t\t\t&result->updates[i].new_oid,\n> +\t\t\t\t\t&result->updates[i].old_oid,\n>  \t\t\t\t\treflog_msg, &err);\n\nCute.\n\nhandle_ref_update() uses transaction==NULL not as a signal to update\neach ref as request comes (i.e., non-transactional updates) but as a\nsingal to perform a dry-run, which was unexpected to my taste but it\nhas been that way since at least February this year ;-)\n\nLooking good.\n\nThanks.\n"},{"id":"545261","messageId":"aiqgP7Yust60mCsC@pks.im","threadId":"65726","inReplyTo":"cfade236-75bc-4679-a74a-6da82e6a5135@gmail.com","subject":"Re: [PATCH v4 06/10] reset: introduce ability to skip updating HEAD","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-11T11:47:11Z","receivedAt":"2026-06-11T11:47:18Z","isPatch":true,"body":"On Wed, Jun 10, 2026 at 02:11:04PM +0100, Phillip Wood wrote:\n> On 10/06/2026 09:52, Patrick Steinhardt wrote:\n> > In a subsequent commit we'll introduce a new caller to\n> > `reset_working_tree()` that really only wants to update the index and\n> > working tree, without updating any references. Introduce a new flag that\n> > makes the caller opt in to updating HEAD and adapt all callers to set\n> > that flag.\n> > \n> > Note that in a previous iteration we instead introduced a flag that made\n> > callers opt out of updating any references. This was somewhat awkward\n> > though because we already have the `UPDATE_ORIG_HEAD` flag, so the\n> > result was somewhat inconsistent.\n> \n> Thanks for doing this. I've grepped for all the callers of reset_head() to\n> confirm this patch adds RESET_HEAD_UPDATE_HEAD to them all.\n> \n> I wonder if we should add a check for passing RESET_HEAD_UPDATE_ORIG_HEAD\n> without RESET_HEAD_UPDATE_HEAD that calls BUG() as we don't support that.\n> Everything else looks good.\n\nFair, can do. Thanks!\n\nPatrick\n"},{"id":"545270","messageId":"20260611-b4-pks-history-drop-v5-0-34d35725559c@pks.im","threadId":"65726","inReplyTo":"20260601-b4-pks-history-drop-v1-0-643e32340d55@pks.im","subject":"[PATCH v5 00/10] builtin/history: introduce \"drop\" subcommand","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-11T13:27:03Z","receivedAt":"2026-06-11T13:27:16Z","isPatch":true,"body":"Hi,\n\nthis small patch series introduces the new \"drop\" subcommand for\ngit-history(1). As a reader might guess, the command does exactly that:\ngiven a commit, it will drop that commit from the commit history and\nreplay descendant branches on top of it.\n\nChanges in v5:\n  - Reject UPDATE_ORIG_HEAD without UPDATE_HEAD.\n  - Link to v4: https://patch.msgid.link/20260610-b4-pks-history-drop-v4-0-70d5f0ae8c25@pks.im\n\nChanges in v4:\n  - Remove the `SKIP_REF_UPDATES` flag in favor of a new `UPDATE_HEAD`\n    flag, as suggested by Phillip.\n  - Rename `reset_head()` to `reset_working_tree()`. This better matches\n    the new scope of the function, and it helps us to catch any\n    in-flight patches that would now have to set the `UPDATE_HEAD` flag.\n  - Link to v3: https://patch.msgid.link/20260608-b4-pks-history-drop-v3-0-84ca8e43e937@pks.im\n\nChanges in v3:\n  - Fix commit message typos.\n  - Make `update_orig_head` and `skip_ref_updates` mutually exclusive.\n  - Use fancy revisions to specify the commit to drop in the example\n    section.\n  - Detect conflicting changes in the index/working tree in dry-run\n    mode.\n  - Consistently use a subshell.\n  - Rename `RESET_HEAD_ORIG_HEAD` to `RESET_HEAD_UPDATE_ORIG_HEAD`.\n  - \n  - Link to v2: https://patch.msgid.link/20260603-b4-pks-history-drop-v2-0-742cb5b5176d@pks.im\n\nChanges in v2:\n  - Reworked `update_worktree()` to use `reset_head()`, which required a\n    bunch of changes to `reset_head()`.\n  - Consistently mention the commit that cannot be dropped as part of\n    error messages.\n  - Adapt error message to not use backticks anymore.\n  - Drop redundant \"--graph\" flag in a test helper.\n  - Link to v1: https://patch.msgid.link/20260601-b4-pks-history-drop-v1-0-643e32340d55@pks.im\n\nThanks!\n\nPatrick\n\n---\nPatrick Steinhardt (10):\n      read-cache: split out function to drop unmerged entries to stage 0\n      reset: drop `USE_THE_REPOSITORY_VARIABLE`\n      reset: rename `reset_head()`\n      reset: modernize flags passed to `reset_working_tree()`\n      reset: introduce dry-run mode\n      reset: introduce ability to skip updating HEAD\n      reset: allow the caller to specify the current HEAD object\n      reset: stop assuming that the caller passes in a clean index\n      builtin/history: split handling of ref updates into two phases\n      builtin/history: implement \"drop\" subcommand\n\n Documentation/git-history.adoc |  38 ++-\n builtin/history.c              | 288 +++++++++++++++++++---\n builtin/rebase.c               |  41 ++--\n read-cache-ll.h                |   1 +\n read-cache.c                   |  12 +-\n reset.c                        | 102 +++++---\n reset.h                        |  51 ++--\n sequencer.c                    |  17 +-\n t/meson.build                  |   1 +\n t/t3454-history-drop.sh        | 537 +++++++++++++++++++++++++++++++++++++++++\n 10 files changed, 971 insertions(+), 117 deletions(-)\n\nRange-diff versus v4:\n\n 1:  82e26ff318 =  1:  6f74de2025 read-cache: split out function to drop unmerged entries to stage 0\n 2:  93e6e7276c =  2:  673658d03c reset: drop `USE_THE_REPOSITORY_VARIABLE`\n 3:  79c2877a65 =  3:  3a4745775a reset: rename `reset_head()`\n 4:  9e67c7ee84 =  4:  d53df7446e reset: modernize flags passed to `reset_working_tree()`\n 5:  4da39e7cec =  5:  2c06054013 reset: introduce dry-run mode\n 6:  683897ac8d !  6:  b9f3438140 reset: introduce ability to skip updating HEAD\n    @@ reset.c: int reset_working_tree(struct repository *r,\n      \tunsigned update_orig_head = opts->flags & RESET_WORKING_TREE_UPDATE_ORIG_HEAD;\n      \tunsigned dry_run = opts->flags & RESET_WORKING_TREE_DRY_RUN;\n      \tstruct object_id *head = NULL, head_oid;\n    +@@ reset.c: int reset_working_tree(struct repository *r,\n    + \tif (opts->branch_msg && !opts->branch)\n    + \t\tBUG(\"branch reflog message given without a branch\");\n    + \n    ++\tif (update_orig_head && !update_head)\n    ++\t\tBUG(\"cannot update ORIG_HEAD without updating HEAD\" );\n    ++\n    + \tif (!refs_only && !dry_run && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {\n    + \t\tret = -1;\n    + \t\tgoto leave_reset_head;\n     @@ reset.c: int reset_working_tree(struct repository *r,\n      \t\toid = &head_oid;\n      \n 7:  2d70838562 =  7:  9e7af4b77b reset: allow the caller to specify the current HEAD object\n 8:  ad56a2d370 =  8:  90b12199e8 reset: stop assuming that the caller passes in a clean index\n 9:  b47f608e92 =  9:  a7e0e345be builtin/history: split handling of ref updates into two phases\n10:  f39ec74b0f = 10:  93bc3d46a4 builtin/history: implement \"drop\" subcommand\n\n---\nbase-commit: 1666c1265231b0bc5f613fbbf3f0a9896cdef76e\nchange-id: 20260601-b4-pks-history-drop-28f6c6399e7b\n\n"},{"id":"545271","messageId":"20260611-b4-pks-history-drop-v5-1-34d35725559c@pks.im","threadId":"65726","inReplyTo":"20260611-b4-pks-history-drop-v5-0-34d35725559c@pks.im","subject":"[PATCH v5 01/10] read-cache: split out function to drop unmerged entries to stage 0","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-11T13:27:04Z","receivedAt":"2026-06-11T13:27:17Z","isPatch":true,"body":"In `repo_read_index_unmerged()` we read the index and then drop any\nunmerged entries to stage 0. In a subsequent commit we'll want to\nperform this operation on arbitrary indexes, not only the one of the\ngiven repository.\n\nPrepare for this by splitting out the functionality into a new function\nthat can act on an arbitrary index.\n\nWhile at it, fix a signedness mismatch when iterating through the index\ncache entries.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n read-cache-ll.h |  1 +\n read-cache.c    | 12 +++++++-----\n 2 files changed, 8 insertions(+), 5 deletions(-)\n\ndiff --git a/read-cache-ll.h b/read-cache-ll.h\nindex 2c8b4b21b1..71b87615eb 100644\n--- a/read-cache-ll.h\n+++ b/read-cache-ll.h\n@@ -309,6 +309,7 @@ int write_locked_index(struct index_state *, struct lock_file *lock, unsigned fl\n void discard_index(struct index_state *);\n void move_index_extensions(struct index_state *dst, struct index_state *src);\n int unmerged_index(const struct index_state *);\n+int index_state_unmerged_to_stage0(struct index_state *istate);\n \n /**\n  * Returns 1 if istate differs from tree, 0 otherwise.  If tree is NULL,\ndiff --git a/read-cache.c b/read-cache.c\nindex 21829102ae..799a5bc719 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -3403,13 +3403,15 @@ int write_locked_index(struct index_state *istate, struct lock_file *lock,\n  */\n int repo_read_index_unmerged(struct repository *repo)\n {\n-\tstruct index_state *istate;\n-\tint i;\n+\trepo_read_index(repo);\n+\treturn index_state_unmerged_to_stage0(repo->index);\n+}\n+\n+int index_state_unmerged_to_stage0(struct index_state *istate)\n+{\n \tint unmerged = 0;\n \n-\trepo_read_index(repo);\n-\tistate = repo->index;\n-\tfor (i = 0; i < istate->cache_nr; i++) {\n+\tfor (unsigned int i = 0; i < istate->cache_nr; i++) {\n \t\tstruct cache_entry *ce = istate->cache[i];\n \t\tstruct cache_entry *new_ce;\n \t\tint len;\n\n-- \n2.54.0.1189.g8c84645362.dirty\n\n"},{"id":"545272","messageId":"20260611-b4-pks-history-drop-v5-2-34d35725559c@pks.im","threadId":"65726","inReplyTo":"20260611-b4-pks-history-drop-v5-0-34d35725559c@pks.im","subject":"[PATCH v5 02/10] reset: drop `USE_THE_REPOSITORY_VARIABLE`","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-11T13:27:05Z","receivedAt":"2026-06-11T13:27:19Z","isPatch":true,"body":"In \"reset.c\" we still have references to `the_repository`, even though\nthe only entry point into the file already receives a repository as\nparameter.\n\nUpdate all uses of `the_repository` to instead use the passed-in repo\nand drop `USE_THE_REPOSITORY_VARIABLE`.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reset.c | 27 +++++++++++++--------------\n 1 file changed, 13 insertions(+), 14 deletions(-)\n\ndiff --git a/reset.c b/reset.c\nindex 46e30e6394..3b3cb74dab 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -1,5 +1,3 @@\n-#define USE_THE_REPOSITORY_VARIABLE\n-\n #include \"git-compat-util.h\"\n #include \"cache-tree.h\"\n #include \"gettext.h\"\n@@ -13,7 +11,8 @@\n #include \"unpack-trees.h\"\n #include \"hook.h\"\n \n-static int update_refs(const struct reset_head_opts *opts,\n+static int update_refs(struct repository *repo,\n+\t\t       const struct reset_head_opts *opts,\n \t\t       const struct object_id *oid,\n \t\t       const struct object_id *head)\n {\n@@ -42,19 +41,19 @@ static int update_refs(const struct reset_head_opts *opts,\n \tprefix_len = msg.len;\n \n \tif (update_orig_head) {\n-\t\tif (!repo_get_oid(the_repository, \"ORIG_HEAD\", &oid_old_orig))\n+\t\tif (!repo_get_oid(repo, \"ORIG_HEAD\", &oid_old_orig))\n \t\t\told_orig = &oid_old_orig;\n \t\tif (head) {\n \t\t\tif (!reflog_orig_head) {\n \t\t\t\tstrbuf_addstr(&msg, \"updating ORIG_HEAD\");\n \t\t\t\treflog_orig_head = msg.buf;\n \t\t\t}\n-\t\t\trefs_update_ref(get_main_ref_store(the_repository),\n+\t\t\trefs_update_ref(get_main_ref_store(repo),\n \t\t\t\t\treflog_orig_head, \"ORIG_HEAD\",\n \t\t\t\t\torig_head ? orig_head : head,\n \t\t\t\t\told_orig, 0, UPDATE_REFS_MSG_ON_ERR);\n \t\t} else if (old_orig)\n-\t\t\trefs_delete_ref(get_main_ref_store(the_repository),\n+\t\t\trefs_delete_ref(get_main_ref_store(repo),\n \t\t\t\t\tNULL, \"ORIG_HEAD\", old_orig, 0);\n \t}\n \n@@ -64,23 +63,23 @@ static int update_refs(const struct reset_head_opts *opts,\n \t\treflog_head = msg.buf;\n \t}\n \tif (!switch_to_branch)\n-\t\tret = refs_update_ref(get_main_ref_store(the_repository),\n+\t\tret = refs_update_ref(get_main_ref_store(repo),\n \t\t\t\t      reflog_head, \"HEAD\", oid, head,\n \t\t\t\t      detach_head ? REF_NO_DEREF : 0,\n \t\t\t\t      UPDATE_REFS_MSG_ON_ERR);\n \telse {\n-\t\tret = refs_update_ref(get_main_ref_store(the_repository),\n+\t\tret = refs_update_ref(get_main_ref_store(repo),\n \t\t\t\t      reflog_branch ? reflog_branch : reflog_head,\n \t\t\t\t      switch_to_branch, oid, NULL, 0,\n \t\t\t\t      UPDATE_REFS_MSG_ON_ERR);\n \t\tif (!ret)\n-\t\t\tret = refs_update_symref(get_main_ref_store(the_repository),\n+\t\t\tret = refs_update_symref(get_main_ref_store(repo),\n \t\t\t\t\t\t \"HEAD\", switch_to_branch,\n \t\t\t\t\t\t reflog_head);\n \t}\n \tif (!ret && run_hook)\n-\t\trun_hooks_l(the_repository, \"post-checkout\",\n-\t\t\t    oid_to_hex(head ? head : null_oid(the_hash_algo)),\n+\t\trun_hooks_l(repo, \"post-checkout\",\n+\t\t\t    oid_to_hex(head ? head : null_oid(repo->hash_algo)),\n \t\t\t    oid_to_hex(oid), \"1\", NULL);\n \tstrbuf_release(&msg);\n \treturn ret;\n@@ -126,7 +125,7 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \t\toid = &head_oid;\n \n \tif (refs_only)\n-\t\treturn update_refs(opts, oid, head);\n+\t\treturn update_refs(r, opts, oid, head);\n \n \taction = reset_hard ? \"reset\" : \"checkout\";\n \tsetup_unpack_trees_porcelain(&unpack_tree_opts, action);\n@@ -163,7 +162,7 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \t\tgoto leave_reset_head;\n \t}\n \n-\ttree = repo_parse_tree_indirect(the_repository, oid);\n+\ttree = repo_parse_tree_indirect(r, oid);\n \tif (!tree) {\n \t\tret = error(_(\"unable to read tree (%s)\"), oid_to_hex(oid));\n \t\tgoto leave_reset_head;\n@@ -177,7 +176,7 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \t}\n \n \tif (oid != &head_oid || update_orig_head || switch_to_branch)\n-\t\tret = update_refs(opts, oid, head);\n+\t\tret = update_refs(r, opts, oid, head);\n \n leave_reset_head:\n \trollback_lock_file(&lock);\n\n-- \n2.54.0.1189.g8c84645362.dirty\n\n"},{"id":"545273","messageId":"20260611-b4-pks-history-drop-v5-3-34d35725559c@pks.im","threadId":"65726","inReplyTo":"20260611-b4-pks-history-drop-v5-0-34d35725559c@pks.im","subject":"[PATCH v5 03/10] reset: rename `reset_head()`","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-11T13:27:06Z","receivedAt":"2026-06-11T13:27:22Z","isPatch":true,"body":"In a subsequent commit we're about to adapt `reset_head()` so that the\nreference update to HEAD is optional, only. At this point the function\nstarts to feel misnamed, as it doesn't necessarily have anything to do\nwith the HEAD reference anymore. The gist of the function then is that\nwe reset the working tree to a specific new commit, updating both the\nindex and the checked-out files.\n\nRename it to `reset_working_tree()` to better reflect that.\n\nNote that we don't adjust the flags yet. This will happen in a\nsubsequent commit.\n\nSuggested-by: Phillip Wood <phillip.wood123@gmail.com>\n---\n builtin/rebase.c | 20 ++++++++++----------\n reset.c          |  5 +++--\n reset.h          |  4 ++--\n sequencer.c      |  8 ++++----\n 4 files changed, 19 insertions(+), 18 deletions(-)\n\ndiff --git a/builtin/rebase.c b/builtin/rebase.c\nindex fa4f5d9306..22fbba3c62 100644\n--- a/builtin/rebase.c\n+++ b/builtin/rebase.c\n@@ -592,7 +592,7 @@ static int finish_rebase(struct rebase_options *opts)\n static int move_to_original_branch(struct rebase_options *opts)\n {\n \tstruct strbuf branch_reflog = STRBUF_INIT, head_reflog = STRBUF_INIT;\n-\tstruct reset_head_opts ropts = { 0 };\n+\tstruct reset_working_tree_options ropts = { 0 };\n \tint ret;\n \n \tif (!opts->head_name)\n@@ -610,7 +610,7 @@ static int move_to_original_branch(struct rebase_options *opts)\n \tropts.flags = RESET_HEAD_REFS_ONLY;\n \tropts.branch_msg = branch_reflog.buf;\n \tropts.head_msg = head_reflog.buf;\n-\tret = reset_head(the_repository, &ropts);\n+\tret = reset_working_tree(the_repository, &ropts);\n \n \tstrbuf_release(&branch_reflog);\n \tstrbuf_release(&head_reflog);\n@@ -685,7 +685,7 @@ static int run_am(struct rebase_options *opts)\n \n \tstatus = run_command(&format_patch);\n \tif (status) {\n-\t\tstruct reset_head_opts ropts = { 0 };\n+\t\tstruct reset_working_tree_options ropts = { 0 };\n \t\tunlink(rebased_patches);\n \t\tfree(rebased_patches);\n \t\tchild_process_clear(&am);\n@@ -693,7 +693,7 @@ static int run_am(struct rebase_options *opts)\n \t\tropts.oid = &opts->orig_head->object.oid;\n \t\tropts.branch = opts->head_name;\n \t\tropts.default_reflog_action = opts->reflog_action;\n-\t\treset_head(the_repository, &ropts);\n+\t\treset_working_tree(the_repository, &ropts);\n \t\terror(_(\"\\ngit encountered an error while preparing the \"\n \t\t\t\"patches to replay\\n\"\n \t\t\t\"these revisions:\\n\"\n@@ -855,7 +855,7 @@ static int rebase_config(const char *var, const char *value,\n static int checkout_up_to_date(struct rebase_options *options)\n {\n \tstruct strbuf buf = STRBUF_INIT;\n-\tstruct reset_head_opts ropts = { 0 };\n+\tstruct reset_working_tree_options ropts = { 0 };\n \tint ret = 0;\n \n \tstrbuf_addf(&buf, \"%s: checkout %s\",\n@@ -866,7 +866,7 @@ static int checkout_up_to_date(struct rebase_options *options)\n \tif (!ropts.branch)\n \t\tropts.flags |=  RESET_HEAD_DETACH;\n \tropts.head_msg = buf.buf;\n-\tif (reset_head(the_repository, &ropts) < 0)\n+\tif (reset_working_tree(the_repository, &ropts) < 0)\n \t\tret = error(_(\"could not switch to %s\"), options->switch_to);\n \tstrbuf_release(&buf);\n \n@@ -1116,7 +1116,7 @@ int cmd_rebase(int argc,\n \tint reschedule_failed_exec = -1;\n \tint allow_preemptive_ff = 1;\n \tint preserve_merges_selected = 0;\n-\tstruct reset_head_opts ropts = { 0 };\n+\tstruct reset_working_tree_options ropts = { 0 };\n \tstruct option builtin_rebase_options[] = {\n \t\tOPT_STRING(0, \"onto\", &options.onto_name,\n \t\t\t   N_(\"revision\"),\n@@ -1385,7 +1385,7 @@ int cmd_rebase(int argc,\n \t\trerere_clear(the_repository, &merge_rr);\n \t\tstring_list_clear(&merge_rr, 1);\n \t\tropts.flags = RESET_HEAD_HARD;\n-\t\tif (reset_head(the_repository, &ropts) < 0)\n+\t\tif (reset_working_tree(the_repository, &ropts) < 0)\n \t\t\tdie(_(\"could not discard worktree changes\"));\n \t\tremove_branch_state(the_repository, 0);\n \t\tif (read_basic_state(&options))\n@@ -1410,7 +1410,7 @@ int cmd_rebase(int argc,\n \t\tropts.head_msg = head_msg.buf;\n \t\tropts.branch = options.head_name;\n \t\tropts.flags = RESET_HEAD_HARD;\n-\t\tif (reset_head(the_repository, &ropts) < 0)\n+\t\tif (reset_working_tree(the_repository, &ropts) < 0)\n \t\t\tdie(_(\"could not move back to %s\"),\n \t\t\t    oid_to_hex(&options.orig_head->object.oid));\n \t\tstrbuf_release(&head_msg);\n@@ -1880,7 +1880,7 @@ int cmd_rebase(int argc,\n \t\t\tRESET_HEAD_RUN_POST_CHECKOUT_HOOK;\n \tropts.head_msg = msg.buf;\n \tropts.default_reflog_action = options.reflog_action;\n-\tif (reset_head(the_repository, &ropts)) {\n+\tif (reset_working_tree(the_repository, &ropts)) {\n \t\tret = error(_(\"Could not detach HEAD\"));\n \t\tgoto cleanup_autostash;\n \t}\ndiff --git a/reset.c b/reset.c\nindex 3b3cb74dab..799596398b 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -12,7 +12,7 @@\n #include \"hook.h\"\n \n static int update_refs(struct repository *repo,\n-\t\t       const struct reset_head_opts *opts,\n+\t\t       const struct reset_working_tree_options *opts,\n \t\t       const struct object_id *oid,\n \t\t       const struct object_id *head)\n {\n@@ -85,7 +85,8 @@ static int update_refs(struct repository *repo,\n \treturn ret;\n }\n \n-int reset_head(struct repository *r, const struct reset_head_opts *opts)\n+int reset_working_tree(struct repository *r,\n+\t\t       const struct reset_working_tree_options *opts)\n {\n \tconst struct object_id *oid = opts->oid;\n \tconst char *switch_to_branch = opts->branch;\ndiff --git a/reset.h b/reset.h\nindex a28f81829d..f130152014 100644\n--- a/reset.h\n+++ b/reset.h\n@@ -17,7 +17,7 @@\n /* Update ORIG_HEAD as well as HEAD */\n #define RESET_ORIG_HEAD (1<<4)\n \n-struct reset_head_opts {\n+struct reset_working_tree_options {\n \t/*\n \t * The commit to checkout/reset to. Defaults to HEAD.\n \t */\n@@ -55,6 +55,6 @@ struct reset_head_opts {\n \tconst char *default_reflog_action;\n };\n \n-int reset_head(struct repository *r, const struct reset_head_opts *opts);\n+int reset_working_tree(struct repository *r, const struct reset_working_tree_options *opts);\n \n #endif\ndiff --git a/sequencer.c b/sequencer.c\nindex 1ee4b2875b..d73ecf0384 100644\n--- a/sequencer.c\n+++ b/sequencer.c\n@@ -4677,7 +4677,7 @@ static void create_autostash_internal(struct repository *r,\n \tif (has_unstaged_changes(r, 1) ||\n \t    has_uncommitted_changes(r, 1)) {\n \t\tstruct child_process stash = CHILD_PROCESS_INIT;\n-\t\tstruct reset_head_opts ropts = { .flags = RESET_HEAD_HARD };\n+\t\tstruct reset_working_tree_options ropts = { .flags = RESET_HEAD_HARD };\n \t\tstruct object_id oid;\n \n \t\tstrvec_pushl(&stash.args,\n@@ -4707,7 +4707,7 @@ static void create_autostash_internal(struct repository *r,\n \n \t\tif (!silent)\n \t\t\tprintf(_(\"Created autostash: %s\\n\"), buf.buf);\n-\t\tif (reset_head(r, &ropts) < 0)\n+\t\tif (reset_working_tree(r, &ropts) < 0)\n \t\t\tdie(_(\"could not reset --hard\"));\n \t\tdiscard_index(r->index);\n \t\tif (repo_read_index(r) < 0)\n@@ -4867,7 +4867,7 @@ static int checkout_onto(struct repository *r, struct replay_opts *opts,\n \t\t\t const char *onto_name, const struct object_id *onto,\n \t\t\t const struct object_id *orig_head)\n {\n-\tstruct reset_head_opts ropts = {\n+\tstruct reset_working_tree_options ropts = {\n \t\t.oid = onto,\n \t\t.orig_head = orig_head,\n \t\t.flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD |\n@@ -4876,7 +4876,7 @@ static int checkout_onto(struct repository *r, struct replay_opts *opts,\n \t\t\t\t\t   onto_name),\n \t\t.default_reflog_action = sequencer_reflog_action(opts)\n \t};\n-\tif (reset_head(r, &ropts)) {\n+\tif (reset_working_tree(r, &ropts)) {\n \t\tapply_autostash(rebase_path_autostash());\n \t\tsequencer_remove_state(opts);\n \t\treturn error(_(\"could not detach HEAD\"));\n\n-- \n2.54.0.1189.g8c84645362.dirty\n\n"},{"id":"545274","messageId":"20260611-b4-pks-history-drop-v5-4-34d35725559c@pks.im","threadId":"65726","inReplyTo":"20260611-b4-pks-history-drop-v5-0-34d35725559c@pks.im","subject":"[PATCH v5 04/10] reset: modernize flags passed to `reset_working_tree()`","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-11T13:27:07Z","receivedAt":"2026-06-11T13:27:25Z","isPatch":true,"body":"The flags passed to `reset_working_tree()` are declared as defines. This\nhas fallen a bit out of practice nowadays, where we instead prefer to\nuse enums. Furthermore, the prefix of those flags does not match the\nfunction name anymore after the rename in the preceding commit.\n\nAdapt the code to follow modern best practices and adapt the flag names.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n builtin/rebase.c | 15 ++++++++-------\n reset.c          | 12 ++++++------\n reset.h          | 31 +++++++++++++++++++------------\n sequencer.c      |  9 ++++++---\n 4 files changed, 39 insertions(+), 28 deletions(-)\n\ndiff --git a/builtin/rebase.c b/builtin/rebase.c\nindex 22fbba3c62..06dcbaf5e8 100644\n--- a/builtin/rebase.c\n+++ b/builtin/rebase.c\n@@ -607,7 +607,7 @@ static int move_to_original_branch(struct rebase_options *opts)\n \tstrbuf_addf(&head_reflog, \"%s (finish): returning to %s\",\n \t\t    opts->reflog_action, opts->head_name);\n \tropts.branch = opts->head_name;\n-\tropts.flags = RESET_HEAD_REFS_ONLY;\n+\tropts.flags = RESET_WORKING_TREE_REFS_ONLY;\n \tropts.branch_msg = branch_reflog.buf;\n \tropts.head_msg = head_reflog.buf;\n \tret = reset_working_tree(the_repository, &ropts);\n@@ -862,9 +862,9 @@ static int checkout_up_to_date(struct rebase_options *options)\n \t\t    options->reflog_action, options->switch_to);\n \tropts.oid = &options->orig_head->object.oid;\n \tropts.branch = options->head_name;\n-\tropts.flags = RESET_HEAD_RUN_POST_CHECKOUT_HOOK;\n+\tropts.flags = RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK;\n \tif (!ropts.branch)\n-\t\tropts.flags |=  RESET_HEAD_DETACH;\n+\t\tropts.flags |=  RESET_WORKING_TREE_DETACH;\n \tropts.head_msg = buf.buf;\n \tif (reset_working_tree(the_repository, &ropts) < 0)\n \t\tret = error(_(\"could not switch to %s\"), options->switch_to);\n@@ -1384,7 +1384,7 @@ int cmd_rebase(int argc,\n \n \t\trerere_clear(the_repository, &merge_rr);\n \t\tstring_list_clear(&merge_rr, 1);\n-\t\tropts.flags = RESET_HEAD_HARD;\n+\t\tropts.flags = RESET_WORKING_TREE_HARD;\n \t\tif (reset_working_tree(the_repository, &ropts) < 0)\n \t\t\tdie(_(\"could not discard worktree changes\"));\n \t\tremove_branch_state(the_repository, 0);\n@@ -1409,7 +1409,7 @@ int cmd_rebase(int argc,\n \t\tropts.oid = &options.orig_head->object.oid;\n \t\tropts.head_msg = head_msg.buf;\n \t\tropts.branch = options.head_name;\n-\t\tropts.flags = RESET_HEAD_HARD;\n+\t\tropts.flags = RESET_WORKING_TREE_HARD;\n \t\tif (reset_working_tree(the_repository, &ropts) < 0)\n \t\t\tdie(_(\"could not move back to %s\"),\n \t\t\t    oid_to_hex(&options.orig_head->object.oid));\n@@ -1876,8 +1876,9 @@ int cmd_rebase(int argc,\n \t\t    options.reflog_action, options.onto_name);\n \tropts.oid = &options.onto->object.oid;\n \tropts.orig_head = &options.orig_head->object.oid;\n-\tropts.flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD |\n-\t\t\tRESET_HEAD_RUN_POST_CHECKOUT_HOOK;\n+\tropts.flags = RESET_WORKING_TREE_DETACH |\n+\t\t      RESET_WORKING_TREE_UPDATE_ORIG_HEAD |\n+\t\t      RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK;\n \tropts.head_msg = msg.buf;\n \tropts.default_reflog_action = options.reflog_action;\n \tif (reset_working_tree(the_repository, &ropts)) {\ndiff --git a/reset.c b/reset.c\nindex 799596398b..4ca7f23a25 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -16,9 +16,9 @@ static int update_refs(struct repository *repo,\n \t\t       const struct object_id *oid,\n \t\t       const struct object_id *head)\n {\n-\tunsigned detach_head = opts->flags & RESET_HEAD_DETACH;\n-\tunsigned run_hook = opts->flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK;\n-\tunsigned update_orig_head = opts->flags & RESET_ORIG_HEAD;\n+\tunsigned detach_head = opts->flags & RESET_WORKING_TREE_DETACH;\n+\tunsigned run_hook = opts->flags & RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK;\n+\tunsigned update_orig_head = opts->flags & RESET_WORKING_TREE_UPDATE_ORIG_HEAD;\n \tconst struct object_id *orig_head = opts->orig_head;\n \tconst char *switch_to_branch = opts->branch;\n \tconst char *reflog_branch = opts->branch_msg;\n@@ -90,9 +90,9 @@ int reset_working_tree(struct repository *r,\n {\n \tconst struct object_id *oid = opts->oid;\n \tconst char *switch_to_branch = opts->branch;\n-\tunsigned reset_hard = opts->flags & RESET_HEAD_HARD;\n-\tunsigned refs_only = opts->flags & RESET_HEAD_REFS_ONLY;\n-\tunsigned update_orig_head = opts->flags & RESET_ORIG_HEAD;\n+\tunsigned reset_hard = opts->flags & RESET_WORKING_TREE_HARD;\n+\tunsigned refs_only = opts->flags & RESET_WORKING_TREE_REFS_ONLY;\n+\tunsigned update_orig_head = opts->flags & RESET_WORKING_TREE_UPDATE_ORIG_HEAD;\n \tstruct object_id *head = NULL, head_oid;\n \tstruct tree_desc desc[2] = { { NULL }, { NULL } };\n \tstruct lock_file lock = LOCK_INIT;\ndiff --git a/reset.h b/reset.h\nindex f130152014..2e5826de99 100644\n--- a/reset.h\n+++ b/reset.h\n@@ -6,16 +6,22 @@\n \n #define GIT_REFLOG_ACTION_ENVIRONMENT \"GIT_REFLOG_ACTION\"\n \n-/* Request a detached checkout */\n-#define RESET_HEAD_DETACH (1<<0)\n-/* Request a reset rather than a checkout */\n-#define RESET_HEAD_HARD (1<<1)\n-/* Run the post-checkout hook */\n-#define RESET_HEAD_RUN_POST_CHECKOUT_HOOK (1<<2)\n-/* Only update refs, do not touch the worktree */\n-#define RESET_HEAD_REFS_ONLY (1<<3)\n-/* Update ORIG_HEAD as well as HEAD */\n-#define RESET_ORIG_HEAD (1<<4)\n+enum reset_working_tree_flags {\n+\t/* Request a detached checkout */\n+\tRESET_WORKING_TREE_DETACH = (1 << 0),\n+\n+\t/* Request a reset rather than a checkout */\n+\tRESET_WORKING_TREE_HARD = (1 << 1),\n+\n+\t/* Run the post-checkout hook */\n+\tRESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK = (1 << 2),\n+\n+\t/* Only update refs, do not touch the worktree */\n+\tRESET_WORKING_TREE_REFS_ONLY = (1 << 3),\n+\n+\t/* Update ORIG_HEAD as well as HEAD */\n+\tRESET_WORKING_TREE_UPDATE_ORIG_HEAD = (1 << 4),\n+};\n \n struct reset_working_tree_options {\n \t/*\n@@ -33,7 +39,7 @@ struct reset_working_tree_options {\n \t/*\n \t * Flags defined above.\n \t */\n-\tunsigned flags;\n+\tenum reset_working_tree_flags flags;\n \t/*\n \t * Optional reflog message for branch, defaults to head_msg.\n \t */\n@@ -45,7 +51,8 @@ struct reset_working_tree_options {\n \tconst char *head_msg;\n \t/*\n \t * Optional reflog message for ORIG_HEAD, if this omitted and flags\n-\t * contains RESET_ORIG_HEAD then default_reflog_action must be given.\n+\t * contains RESET_WORKING_TREE_UPDATE_ORIG_HEAD then\n+\t * default_reflog_action must be given.\n \t */\n \tconst char *orig_head_msg;\n \t/*\ndiff --git a/sequencer.c b/sequencer.c\nindex d73ecf0384..4efe831178 100644\n--- a/sequencer.c\n+++ b/sequencer.c\n@@ -4677,7 +4677,9 @@ static void create_autostash_internal(struct repository *r,\n \tif (has_unstaged_changes(r, 1) ||\n \t    has_uncommitted_changes(r, 1)) {\n \t\tstruct child_process stash = CHILD_PROCESS_INIT;\n-\t\tstruct reset_working_tree_options ropts = { .flags = RESET_HEAD_HARD };\n+\t\tstruct reset_working_tree_options ropts = {\n+\t\t\t.flags = RESET_WORKING_TREE_HARD,\n+\t\t};\n \t\tstruct object_id oid;\n \n \t\tstrvec_pushl(&stash.args,\n@@ -4870,8 +4872,9 @@ static int checkout_onto(struct repository *r, struct replay_opts *opts,\n \tstruct reset_working_tree_options ropts = {\n \t\t.oid = onto,\n \t\t.orig_head = orig_head,\n-\t\t.flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD |\n-\t\t\t\tRESET_HEAD_RUN_POST_CHECKOUT_HOOK,\n+\t\t.flags = RESET_WORKING_TREE_DETACH |\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.default_reflog_action = sequencer_reflog_action(opts)\n\n-- \n2.54.0.1189.g8c84645362.dirty\n\n"},{"id":"545275","messageId":"20260611-b4-pks-history-drop-v5-5-34d35725559c@pks.im","threadId":"65726","inReplyTo":"20260611-b4-pks-history-drop-v5-0-34d35725559c@pks.im","subject":"[PATCH v5 05/10] reset: introduce dry-run mode","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-11T13:27:08Z","receivedAt":"2026-06-11T13:27:27Z","isPatch":true,"body":"In a subsequent commit we'll add another caller to `reset_working_tree()`\nthat wants to perform a dry-run check of whether it would be possible to\nupdate the index and working tree when moving to a new commit. Introduce\na new flag that lets the caller perform this operation.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reset.c | 44 +++++++++++++++++++++++++++++++++-----------\n reset.h |  6 ++++++\n 2 files changed, 39 insertions(+), 11 deletions(-)\n\ndiff --git a/reset.c b/reset.c\nindex 4ca7f23a25..99f2c1b012 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -93,11 +93,14 @@ int reset_working_tree(struct repository *r,\n \tunsigned reset_hard = opts->flags & RESET_WORKING_TREE_HARD;\n \tunsigned refs_only = opts->flags & RESET_WORKING_TREE_REFS_ONLY;\n \tunsigned update_orig_head = opts->flags & RESET_WORKING_TREE_UPDATE_ORIG_HEAD;\n+\tunsigned dry_run = opts->flags & RESET_WORKING_TREE_DRY_RUN;\n \tstruct object_id *head = NULL, head_oid;\n \tstruct tree_desc desc[2] = { { NULL }, { NULL } };\n \tstruct lock_file lock = LOCK_INIT;\n \tstruct unpack_trees_options unpack_tree_opts = { 0 };\n \tstruct tree *tree;\n+\tstruct index_state scratch_index = INDEX_STATE_INIT(r);\n+\tstruct index_state *istate;\n \tconst char *action;\n \tint ret = 0, nr = 0;\n \n@@ -110,7 +113,7 @@ int reset_working_tree(struct repository *r,\n \tif (opts->branch_msg && !opts->branch)\n \t\tBUG(\"branch reflog message given without a branch\");\n \n-\tif (!refs_only && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {\n+\tif (!refs_only && !dry_run && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {\n \t\tret = -1;\n \t\tgoto leave_reset_head;\n \t}\n@@ -125,16 +128,36 @@ int reset_working_tree(struct repository *r,\n \tif (!oid)\n \t\toid = &head_oid;\n \n-\tif (refs_only)\n-\t\treturn update_refs(r, opts, oid, head);\n+\tif (refs_only) {\n+\t\tif (!dry_run)\n+\t\t\treturn update_refs(r, opts, oid, head);\n+\t\treturn 0;\n+\t}\n+\n+\tif (dry_run) {\n+\t\tif (read_index_from(&scratch_index, r->index_file, r->gitdir) < 0 ||\n+\t\t    index_state_unmerged_to_stage0(&scratch_index) < 0) {\n+\t\t\tret = error(_(\"could not read index\"));\n+\t\t\tgoto leave_reset_head;\n+\t\t}\n+\n+\t\tistate = &scratch_index;\n+\t} else {\n+\t\tif (repo_read_index_unmerged(r) < 0) {\n+\t\t\tret = error(_(\"could not read index\"));\n+\t\t\tgoto leave_reset_head;\n+\t\t}\n+\t\tistate = r->index;\n+\t}\n \n \taction = reset_hard ? \"reset\" : \"checkout\";\n \tsetup_unpack_trees_porcelain(&unpack_tree_opts, action);\n \tunpack_tree_opts.head_idx = 1;\n-\tunpack_tree_opts.src_index = r->index;\n-\tunpack_tree_opts.dst_index = r->index;\n+\tunpack_tree_opts.src_index = istate;\n+\tunpack_tree_opts.dst_index = istate;\n \tunpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge;\n-\tunpack_tree_opts.update = 1;\n+\tunpack_tree_opts.update = !dry_run;\n+\tunpack_tree_opts.dry_run = dry_run;\n \tunpack_tree_opts.merge = 1;\n \tunpack_tree_opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */\n \tunpack_tree_opts.skip_cache_tree_update = 1;\n@@ -142,11 +165,6 @@ int reset_working_tree(struct repository *r,\n \tif (reset_hard)\n \t\tunpack_tree_opts.reset = UNPACK_RESET_PROTECT_UNTRACKED;\n \n-\tif (repo_read_index_unmerged(r) < 0) {\n-\t\tret = error(_(\"could not read index\"));\n-\t\tgoto leave_reset_head;\n-\t}\n-\n \tif (!reset_hard && !fill_tree_descriptor(r, &desc[nr++], &head_oid)) {\n \t\tret = error(_(\"failed to find tree of %s\"),\n \t\t\t    oid_to_hex(&head_oid));\n@@ -163,6 +181,9 @@ int reset_working_tree(struct repository *r,\n \t\tgoto leave_reset_head;\n \t}\n \n+\tif (dry_run)\n+\t\tgoto leave_reset_head;\n+\n \ttree = repo_parse_tree_indirect(r, oid);\n \tif (!tree) {\n \t\tret = error(_(\"unable to read tree (%s)\"), oid_to_hex(oid));\n@@ -182,6 +203,7 @@ int reset_working_tree(struct repository *r,\n leave_reset_head:\n \trollback_lock_file(&lock);\n \tclear_unpack_trees_porcelain(&unpack_tree_opts);\n+\trelease_index(&scratch_index);\n \twhile (nr)\n \t\tfree((void *)desc[--nr].buffer);\n \treturn ret;\ndiff --git a/reset.h b/reset.h\nindex 2e5826de99..898e4a1e95 100644\n--- a/reset.h\n+++ b/reset.h\n@@ -21,6 +21,12 @@ enum reset_working_tree_flags {\n \n \t/* Update ORIG_HEAD as well as HEAD */\n \tRESET_WORKING_TREE_UPDATE_ORIG_HEAD = (1 << 4),\n+\n+\t/*\n+\t * Perform a dry-run by performing the operation without updating\n+\t * any user-visible state.\n+\t */\n+\tRESET_WORKING_TREE_DRY_RUN = (1 << 5),\n };\n \n struct reset_working_tree_options {\n\n-- \n2.54.0.1189.g8c84645362.dirty\n\n"},{"id":"545276","messageId":"20260611-b4-pks-history-drop-v5-6-34d35725559c@pks.im","threadId":"65726","inReplyTo":"20260611-b4-pks-history-drop-v5-0-34d35725559c@pks.im","subject":"[PATCH v5 06/10] reset: introduce ability to skip updating HEAD","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-11T13:27:09Z","receivedAt":"2026-06-11T13:27:29Z","isPatch":true,"body":"In a subsequent commit we'll introduce a new caller to\n`reset_working_tree()` that really only wants to update the index and\nworking tree, without updating any references. Introduce a new flag that\nmakes the caller opt in to updating HEAD and adapt all callers to set\nthat flag.\n\nNote that in a previous iteration we instead introduced a flag that made\ncallers opt out of updating any references. This was somewhat awkward\nthough because we already have the `UPDATE_ORIG_HEAD` flag, so the\nresult was somewhat inconsistent.\n\nSuggested-by: Phillip Wood <phillip.wood123@gmail.com>\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n builtin/rebase.c | 14 ++++++++++----\n reset.c          |  9 +++++++--\n reset.h          |  9 ++++++---\n sequencer.c      |  4 +++-\n 4 files changed, 26 insertions(+), 10 deletions(-)\n\ndiff --git a/builtin/rebase.c b/builtin/rebase.c\nindex 06dcbaf5e8..10a306310c 100644\n--- a/builtin/rebase.c\n+++ b/builtin/rebase.c\n@@ -607,7 +607,8 @@ static int move_to_original_branch(struct rebase_options *opts)\n \tstrbuf_addf(&head_reflog, \"%s (finish): returning to %s\",\n \t\t    opts->reflog_action, opts->head_name);\n \tropts.branch = opts->head_name;\n-\tropts.flags = RESET_WORKING_TREE_REFS_ONLY;\n+\tropts.flags = RESET_WORKING_TREE_REFS_ONLY |\n+\t\t      RESET_WORKING_TREE_UPDATE_HEAD;\n \tropts.branch_msg = branch_reflog.buf;\n \tropts.head_msg = head_reflog.buf;\n \tret = reset_working_tree(the_repository, &ropts);\n@@ -693,6 +694,7 @@ static int run_am(struct rebase_options *opts)\n \t\tropts.oid = &opts->orig_head->object.oid;\n \t\tropts.branch = opts->head_name;\n \t\tropts.default_reflog_action = opts->reflog_action;\n+\t\tropts.flags = RESET_WORKING_TREE_UPDATE_HEAD;\n \t\treset_working_tree(the_repository, &ropts);\n \t\terror(_(\"\\ngit encountered an error while preparing the \"\n \t\t\t\"patches to replay\\n\"\n@@ -862,7 +864,8 @@ static int checkout_up_to_date(struct rebase_options *options)\n \t\t    options->reflog_action, options->switch_to);\n \tropts.oid = &options->orig_head->object.oid;\n \tropts.branch = options->head_name;\n-\tropts.flags = RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK;\n+\tropts.flags = RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK |\n+\t\t      RESET_WORKING_TREE_UPDATE_HEAD;\n \tif (!ropts.branch)\n \t\tropts.flags |=  RESET_WORKING_TREE_DETACH;\n \tropts.head_msg = buf.buf;\n@@ -1384,7 +1387,8 @@ int cmd_rebase(int argc,\n \n \t\trerere_clear(the_repository, &merge_rr);\n \t\tstring_list_clear(&merge_rr, 1);\n-\t\tropts.flags = RESET_WORKING_TREE_HARD;\n+\t\tropts.flags = RESET_WORKING_TREE_HARD |\n+\t\t\t      RESET_WORKING_TREE_UPDATE_HEAD;\n \t\tif (reset_working_tree(the_repository, &ropts) < 0)\n \t\t\tdie(_(\"could not discard worktree changes\"));\n \t\tremove_branch_state(the_repository, 0);\n@@ -1409,7 +1413,8 @@ int cmd_rebase(int argc,\n \t\tropts.oid = &options.orig_head->object.oid;\n \t\tropts.head_msg = head_msg.buf;\n \t\tropts.branch = options.head_name;\n-\t\tropts.flags = RESET_WORKING_TREE_HARD;\n+\t\tropts.flags = RESET_WORKING_TREE_HARD |\n+\t\t\t      RESET_WORKING_TREE_UPDATE_HEAD;\n \t\tif (reset_working_tree(the_repository, &ropts) < 0)\n \t\t\tdie(_(\"could not move back to %s\"),\n \t\t\t    oid_to_hex(&options.orig_head->object.oid));\n@@ -1877,6 +1882,7 @@ int cmd_rebase(int argc,\n \tropts.oid = &options.onto->object.oid;\n \tropts.orig_head = &options.orig_head->object.oid;\n \tropts.flags = RESET_WORKING_TREE_DETACH |\n+\t\t      RESET_WORKING_TREE_UPDATE_HEAD |\n \t\t      RESET_WORKING_TREE_UPDATE_ORIG_HEAD |\n \t\t      RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK;\n \tropts.head_msg = msg.buf;\ndiff --git a/reset.c b/reset.c\nindex 99f2c1b012..a744b0f0fc 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -92,6 +92,7 @@ int reset_working_tree(struct repository *r,\n \tconst char *switch_to_branch = opts->branch;\n \tunsigned reset_hard = opts->flags & RESET_WORKING_TREE_HARD;\n \tunsigned refs_only = opts->flags & RESET_WORKING_TREE_REFS_ONLY;\n+\tunsigned update_head = opts->flags & RESET_WORKING_TREE_UPDATE_HEAD;\n \tunsigned update_orig_head = opts->flags & RESET_WORKING_TREE_UPDATE_ORIG_HEAD;\n \tunsigned dry_run = opts->flags & RESET_WORKING_TREE_DRY_RUN;\n \tstruct object_id *head = NULL, head_oid;\n@@ -113,6 +114,9 @@ int reset_working_tree(struct repository *r,\n \tif (opts->branch_msg && !opts->branch)\n \t\tBUG(\"branch reflog message given without a branch\");\n \n+\tif (update_orig_head && !update_head)\n+\t\tBUG(\"cannot update ORIG_HEAD without updating HEAD\" );\n+\n \tif (!refs_only && !dry_run && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {\n \t\tret = -1;\n \t\tgoto leave_reset_head;\n@@ -129,7 +133,7 @@ int reset_working_tree(struct repository *r,\n \t\toid = &head_oid;\n \n \tif (refs_only) {\n-\t\tif (!dry_run)\n+\t\tif (update_head)\n \t\t\treturn update_refs(r, opts, oid, head);\n \t\treturn 0;\n \t}\n@@ -197,7 +201,8 @@ int reset_working_tree(struct repository *r,\n \t\tgoto leave_reset_head;\n \t}\n \n-\tif (oid != &head_oid || update_orig_head || switch_to_branch)\n+\tif (update_head &&\n+\t    (oid != &head_oid || update_orig_head || switch_to_branch))\n \t\tret = update_refs(r, opts, oid, head);\n \n leave_reset_head:\ndiff --git a/reset.h b/reset.h\nindex 898e4a1e95..38b2891b53 100644\n--- a/reset.h\n+++ b/reset.h\n@@ -19,14 +19,17 @@ enum reset_working_tree_flags {\n \t/* Only update refs, do not touch the worktree */\n \tRESET_WORKING_TREE_REFS_ONLY = (1 << 3),\n \n-\t/* Update ORIG_HEAD as well as HEAD */\n-\tRESET_WORKING_TREE_UPDATE_ORIG_HEAD = (1 << 4),\n+\t/* Update HEAD */\n+\tRESET_WORKING_TREE_UPDATE_HEAD = (1 << 4),\n+\n+\t/* Update ORIG_HEAD */\n+\tRESET_WORKING_TREE_UPDATE_ORIG_HEAD = (1 << 5),\n \n \t/*\n \t * Perform a dry-run by performing the operation without updating\n \t * any user-visible state.\n \t */\n-\tRESET_WORKING_TREE_DRY_RUN = (1 << 5),\n+\tRESET_WORKING_TREE_DRY_RUN = (1 << 6),\n };\n \n struct reset_working_tree_options {\ndiff --git a/sequencer.c b/sequencer.c\nindex 4efe831178..e905b1b2d9 100644\n--- a/sequencer.c\n+++ b/sequencer.c\n@@ -4678,7 +4678,8 @@ static void create_autostash_internal(struct repository *r,\n \t    has_uncommitted_changes(r, 1)) {\n \t\tstruct child_process stash = CHILD_PROCESS_INIT;\n \t\tstruct reset_working_tree_options ropts = {\n-\t\t\t.flags = RESET_WORKING_TREE_HARD,\n+\t\t\t.flags = RESET_WORKING_TREE_HARD |\n+\t\t\t\t RESET_WORKING_TREE_UPDATE_HEAD,\n \t\t};\n \t\tstruct object_id oid;\n \n@@ -4873,6 +4874,7 @@ static int checkout_onto(struct repository *r, struct replay_opts *opts,\n \t\t.oid = onto,\n \t\t.orig_head = orig_head,\n \t\t.flags = RESET_WORKING_TREE_DETACH |\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\n-- \n2.54.0.1189.g8c84645362.dirty\n\n"},{"id":"545277","messageId":"20260611-b4-pks-history-drop-v5-7-34d35725559c@pks.im","threadId":"65726","inReplyTo":"20260611-b4-pks-history-drop-v5-0-34d35725559c@pks.im","subject":"[PATCH v5 07/10] reset: allow the caller to specify the current HEAD object","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-11T13:27:10Z","receivedAt":"2026-06-11T13:27:32Z","isPatch":true,"body":"When calling `reset_working_tree()` we automatically derive the commit\nthat the callers wants to move from by reading the HEAD commit. Some\ncallers may already have resolved it, or they may want to move from a\ndifferent commit that doesn't match HEAD.\n\nIntroduce a new `oid_from` option that lets the caller specify the\ncommit.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reset.c | 5 ++++-\n reset.h | 5 +++++\n 2 files changed, 9 insertions(+), 1 deletion(-)\n\ndiff --git a/reset.c b/reset.c\nindex a744b0f0fc..955ea4ed5a 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -122,7 +122,10 @@ int reset_working_tree(struct repository *r,\n \t\tgoto leave_reset_head;\n \t}\n \n-\tif (!repo_get_oid(r, \"HEAD\", &head_oid)) {\n+\tif (opts->oid_from) {\n+\t\toidcpy(&head_oid, opts->oid_from);\n+\t\thead = &head_oid;\n+\t} else if (!repo_get_oid(r, \"HEAD\", &head_oid)) {\n \t\thead = &head_oid;\n \t} else if (!oid || !reset_hard) {\n \t\tret = error(_(\"could not determine HEAD revision\"));\ndiff --git a/reset.h b/reset.h\nindex 38b2891b53..4c992ba671 100644\n--- a/reset.h\n+++ b/reset.h\n@@ -37,6 +37,11 @@ struct reset_working_tree_options {\n \t * The commit to checkout/reset to. Defaults to HEAD.\n \t */\n \tconst struct object_id *oid;\n+\t/*\n+\t * The commit to checkout/reset from when doing a two-way merge. This\n+\t * is used as one of the sides to merge.\n+\t */\n+\tconst struct object_id *oid_from;\n \t/*\n \t * Optional value to set ORIG_HEAD. Defaults to HEAD.\n \t */\n\n-- \n2.54.0.1189.g8c84645362.dirty\n\n"},{"id":"545278","messageId":"20260611-b4-pks-history-drop-v5-8-34d35725559c@pks.im","threadId":"65726","inReplyTo":"20260611-b4-pks-history-drop-v5-0-34d35725559c@pks.im","subject":"[PATCH v5 08/10] reset: stop assuming that the caller passes in a clean index","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-11T13:27:11Z","receivedAt":"2026-06-11T13:27:35Z","isPatch":true,"body":"In 652bd0211d (rebase: use 'skip_cache_tree_update' option, 2022-11-10),\nwe updated `reset_working_tree()` to stop updating the index tree cache.\nThis was done as a performance optimization: the function is only called\nby \"sequencer.c\" and \"rebase.c\", both of which assume a clean index\nbefore they perform their operation, so we know that the end result will\nbe a clean index, too. Consequently, we can skip recomputing the cache\nas we can instead use `prime_cache_tree()` directly.\n\nIn a subsequent commit we're about to add a new caller though where the\nassumption doesn't hold anymore: the index may be dirty before calling\n`reset_working_tree()`, and consequently we cannot prime the cache with\na given tree anymore as the index and tree will mismatch.\n\nAdapt the logic so that we only skip the cache tree update in case we're\ndoing a hard reset. While we could introduce logic that only skips the\nupdate in case the incoming index was dirty already, that doesn't really\nfeel worth it: after all, the mentioned commit says itself that the\nperformance improvement was negligible anyway.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reset.c | 8 +++++---\n 1 file changed, 5 insertions(+), 3 deletions(-)\n\ndiff --git a/reset.c b/reset.c\nindex 955ea4ed5a..5a9ed807d7 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -167,10 +167,11 @@ int reset_working_tree(struct repository *r,\n \tunpack_tree_opts.dry_run = dry_run;\n \tunpack_tree_opts.merge = 1;\n \tunpack_tree_opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */\n-\tunpack_tree_opts.skip_cache_tree_update = 1;\n \tinit_checkout_metadata(&unpack_tree_opts.meta, switch_to_branch, oid, NULL);\n-\tif (reset_hard)\n+\tif (reset_hard) {\n+\t\tunpack_tree_opts.skip_cache_tree_update = 1;\n \t\tunpack_tree_opts.reset = UNPACK_RESET_PROTECT_UNTRACKED;\n+\t}\n \n \tif (!reset_hard && !fill_tree_descriptor(r, &desc[nr++], &head_oid)) {\n \t\tret = error(_(\"failed to find tree of %s\"),\n@@ -197,7 +198,8 @@ int reset_working_tree(struct repository *r,\n \t\tgoto leave_reset_head;\n \t}\n \n-\tprime_cache_tree(r, r->index, tree);\n+\tif (reset_hard)\n+\t\tprime_cache_tree(r, r->index, tree);\n \n \tif (write_locked_index(r->index, &lock, COMMIT_LOCK) < 0) {\n \t\tret = error(_(\"could not write index\"));\n\n-- \n2.54.0.1189.g8c84645362.dirty\n\n"},{"id":"545279","messageId":"20260611-b4-pks-history-drop-v5-9-34d35725559c@pks.im","threadId":"65726","inReplyTo":"20260611-b4-pks-history-drop-v5-0-34d35725559c@pks.im","subject":"[PATCH v5 09/10] builtin/history: split handling of ref updates into two phases","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-11T13:27:12Z","receivedAt":"2026-06-11T13:27:37Z","isPatch":true,"body":"The function `handle_reference_updates()` is used by git-history(1) to\nupdate all references that refer to commits that have been rewritten. As\nsuch, it performs two steps:\n\n  - It gathers the references that need to be updated in the first\n    place.\n\n  - It prepares and commits the reference transaction.\n\nIn a subsequent commit we'll want to handle those two steps separately.\nPrepare for this by splitting up the function into two.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n builtin/history.c | 102 ++++++++++++++++++++++++++++++++++--------------------\n 1 file changed, 64 insertions(+), 38 deletions(-)\n\ndiff --git a/builtin/history.c b/builtin/history.c\nindex 0fc06fb204..4fadf38c32 100644\n--- a/builtin/history.c\n+++ b/builtin/history.c\n@@ -333,21 +333,17 @@ static int handle_ref_update(struct ref_transaction *transaction,\n \t\t\t\t      NULL, NULL, 0, reflog_msg, err);\n }\n \n-static int handle_reference_updates(struct rev_info *revs,\n-\t\t\t\t    enum ref_action action,\n-\t\t\t\t    struct commit *original,\n-\t\t\t\t    struct commit *rewritten,\n-\t\t\t\t    const char *reflog_msg,\n-\t\t\t\t    int dry_run,\n-\t\t\t\t    enum replay_empty_commit_action empty)\n+static int compute_pending_ref_updates(struct rev_info *revs,\n+\t\t\t\t       enum ref_action action,\n+\t\t\t\t       struct commit *original,\n+\t\t\t\t       struct commit *rewritten,\n+\t\t\t\t       enum replay_empty_commit_action empty,\n+\t\t\t\t       struct replay_result *result)\n {\n \tconst struct name_decoration *decoration;\n \tstruct replay_revisions_options opts = {\n \t\t.empty = empty,\n \t};\n-\tstruct replay_result result = { 0 };\n-\tstruct ref_transaction *transaction = NULL;\n-\tstruct strbuf err = STRBUF_INIT;\n \tchar hex[GIT_MAX_HEXSZ + 1];\n \tbool detached_head;\n \tint head_flags = 0;\n@@ -359,34 +355,13 @@ static int handle_reference_updates(struct rev_info *revs,\n \n \topts.onto = oid_to_hex_r(hex, &rewritten->object.oid);\n \n-\tret = replay_revisions(revs, &opts, &result);\n+\tret = replay_revisions(revs, &opts, result);\n \tif (ret)\n-\t\tgoto out;\n+\t\treturn ret;\n \n \tif (action != REF_ACTION_BRANCHES && action != REF_ACTION_HEAD)\n \t\tBUG(\"unsupported ref action %d\", action);\n \n-\tif (!dry_run) {\n-\t\ttransaction = ref_store_transaction_begin(get_main_ref_store(revs->repo), 0, &err);\n-\t\tif (!transaction) {\n-\t\t\tret = error(_(\"failed to begin ref transaction: %s\"), err.buf);\n-\t\t\tgoto out;\n-\t\t}\n-\t}\n-\n-\tfor (size_t i = 0; i < result.updates_nr; i++) {\n-\t\tret = handle_ref_update(transaction,\n-\t\t\t\t\tresult.updates[i].refname,\n-\t\t\t\t\t&result.updates[i].new_oid,\n-\t\t\t\t\t&result.updates[i].old_oid,\n-\t\t\t\t\treflog_msg, &err);\n-\t\tif (ret) {\n-\t\t\tret = error(_(\"failed to update ref '%s': %s\"),\n-\t\t\t\t    result.updates[i].refname, err.buf);\n-\t\t\tgoto out;\n-\t\t}\n-\t}\n-\n \t/*\n \t * `replay_revisions()` only updates references that are\n \t * ancestors of `rewritten`, so we need to manually\n@@ -414,14 +389,43 @@ static int handle_reference_updates(struct rev_info *revs,\n \t\t    !detached_head)\n \t\t\tcontinue;\n \n+\t\tALLOC_GROW(result->updates, result->updates_nr + 1, result->updates_alloc);\n+\t\tresult->updates[result->updates_nr].refname = xstrdup(decoration->name);\n+\t\tresult->updates[result->updates_nr].old_oid = original->object.oid;\n+\t\tresult->updates[result->updates_nr].new_oid = rewritten->object.oid;\n+\t\tresult->updates_nr++;\n+\t}\n+\n+\treturn 0;\n+}\n+\n+static int apply_pending_ref_updates(struct repository *repo,\n+\t\t\t\t     const struct replay_result *result,\n+\t\t\t\t     const char *reflog_msg,\n+\t\t\t\t     int dry_run)\n+{\n+\tstruct ref_transaction *transaction = NULL;\n+\tstruct strbuf err = STRBUF_INIT;\n+\tint ret;\n+\n+\tif (!dry_run) {\n+\t\ttransaction = ref_store_transaction_begin(get_main_ref_store(repo),\n+\t\t\t\t\t\t\t  0, &err);\n+\t\tif (!transaction) {\n+\t\t\tret = error(_(\"failed to begin ref transaction: %s\"), err.buf);\n+\t\t\tgoto out;\n+\t\t}\n+\t}\n+\n+\tfor (size_t i = 0; i < result->updates_nr; i++) {\n \t\tret = handle_ref_update(transaction,\n-\t\t\t\t\tdecoration->name,\n-\t\t\t\t\t&rewritten->object.oid,\n-\t\t\t\t\t&original->object.oid,\n+\t\t\t\t\tresult->updates[i].refname,\n+\t\t\t\t\t&result->updates[i].new_oid,\n+\t\t\t\t\t&result->updates[i].old_oid,\n \t\t\t\t\treflog_msg, &err);\n \t\tif (ret) {\n \t\t\tret = error(_(\"failed to update ref '%s': %s\"),\n-\t\t\t\t    decoration->name, err.buf);\n+\t\t\t\t    result->updates[i].refname, err.buf);\n \t\t\tgoto out;\n \t\t}\n \t}\n@@ -435,11 +439,33 @@ static int handle_reference_updates(struct rev_info *revs,\n \n out:\n \tref_transaction_free(transaction);\n-\treplay_result_release(&result);\n \tstrbuf_release(&err);\n \treturn ret;\n }\n \n+static int handle_reference_updates(struct rev_info *revs,\n+\t\t\t\t    enum ref_action action,\n+\t\t\t\t    struct commit *original,\n+\t\t\t\t    struct commit *rewritten,\n+\t\t\t\t    const char *reflog_msg,\n+\t\t\t\t    int dry_run,\n+\t\t\t\t    enum replay_empty_commit_action empty)\n+{\n+\tstruct replay_result result = { 0 };\n+\tint ret;\n+\n+\tret = compute_pending_ref_updates(revs, action, original, rewritten,\n+\t\t\t\t\t  empty, &result);\n+\tif (ret)\n+\t\tgoto out;\n+\n+\tret = apply_pending_ref_updates(revs->repo, &result, reflog_msg, dry_run);\n+\n+out:\n+\treplay_result_release(&result);\n+\treturn ret;\n+}\n+\n static int commit_became_empty(struct repository *repo,\n \t\t\t       struct commit *original,\n \t\t\t       struct tree *result)\n\n-- \n2.54.0.1189.g8c84645362.dirty\n\n"},{"id":"545280","messageId":"20260611-b4-pks-history-drop-v5-10-34d35725559c@pks.im","threadId":"65726","inReplyTo":"20260611-b4-pks-history-drop-v5-0-34d35725559c@pks.im","subject":"[PATCH v5 10/10] builtin/history: implement \"drop\" subcommand","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-11T13:27:13Z","receivedAt":"2026-06-11T13:27:40Z","isPatch":true,"body":"A common operation when editing the commit history is to drop a specific\ncommit from the history entirely, but this operation is not currently\ncovered by git-history(1).\n\nA couple of noteworthy bits:\n\n  - This is the first git-history(1) command that will ultimately result\n    in changes to both the index and the working tree. We thus have to\n    add logic to merge resulting changes into those.\n\n  - It is still not possible to replay merge commits, so this limitation\n    is inherited for the new \"drop\" command.\n\n  - For now we refuse to drop root commits. While we _can_ indeed drop\n    root commits in the general case, there are edge cases where the\n    resulting history would become completely empty. This is thus left\n    to a subsequent patch series.\n\nOther than that, most of the logic is rather straight-forward as we can\ncontinue to build on the preexisting logic in git-history(1) for most of\nthe part.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n Documentation/git-history.adoc |  38 ++-\n builtin/history.c              | 186 ++++++++++++++\n t/meson.build                  |   1 +\n t/t3454-history-drop.sh        | 537 +++++++++++++++++++++++++++++++++++++++++\n 4 files changed, 761 insertions(+), 1 deletion(-)\n\ndiff --git a/Documentation/git-history.adoc b/Documentation/git-history.adoc\nindex 2ba8121795..28b477cd37 100644\n--- a/Documentation/git-history.adoc\n+++ b/Documentation/git-history.adoc\n@@ -8,6 +8,7 @@ git-history - EXPERIMENTAL: Rewrite history\n SYNOPSIS\n --------\n [synopsis]\n+git history drop <commit> [--dry-run] [--update-refs=(branches|head)] [--empty=(drop|keep|abort)]\n git history fixup <commit> [--dry-run] [--update-refs=(branches|head)] [--reedit-message] [--empty=(drop|keep|abort)]\n git history reword <commit> [--dry-run] [--update-refs=(branches|head)]\n git history split <commit> [--dry-run] [--update-refs=(branches|head)] [--] [<pathspec>...]\n@@ -51,13 +52,28 @@ be stateful operations. The limitation can be lifted once (if) Git learns about\n first-class conflicts.\n \n When using `fixup` with `--empty=drop`, dropping the root commit is not yet\n-supported.\n+supported. Likewise, `drop` cannot remove the root commit or a merge commit.\n \n COMMANDS\n --------\n \n The following commands are available to rewrite history in different ways:\n \n+`drop <commit>`::\n+\tRemove the specified commit from the history. All descendants of the\n+\tcommit are replayed directly onto its parent.\n++\n+The root commit cannot be dropped as that may lead to edge cases where refs\n+end up with no commits anymore. Merge commits cannot be dropped either; see\n+LIMITATIONS.\n++\n+If `HEAD` points at a commit that is to be rewritten, the index and working\n+tree are updated to match the new `HEAD`. The command aborts before any\n+references are updated in case local modifications would be overwritten.\n++\n+If replaying any descendant would result in a conflict, the command aborts\n+with an error.\n+\n `fixup <commit>`::\n \tApply the currently staged changes to the specified commit. This is\n \tsimilar in nature to `git commit --fixup=<commit>` followed by `git\n@@ -170,6 +186,26 @@ The staged addition of `unrelated.txt` has been incorporated into the `first`\n commit. All descendant commits have been replayed on top of the rewritten\n history.\n \n+Drop a commit\n+~~~~~~~~~~~~~\n+\n+----------\n+$ git log --oneline\n+abc1234 (HEAD -> main) third\n+def5678 second\n+ghi9012 first\n+\n+$ git history drop 'main^{/second}'\n+\n+$ git log --oneline\n+jkl3456 (HEAD -> main) third\n+ghi9012 first\n+----------\n+\n+The `second` commit has been removed from the history, and `third` has been\n+replayed directly on top of `first`. All branches that pointed at the dropped\n+commit have been moved to its parent.\n+\n Split a commit\n ~~~~~~~~~~~~~~\n \ndiff --git a/builtin/history.c b/builtin/history.c\nindex 4fadf38c32..e59f69233b 100644\n--- a/builtin/history.c\n+++ b/builtin/history.c\n@@ -17,13 +17,17 @@\n #include \"read-cache.h\"\n #include \"refs.h\"\n #include \"replay.h\"\n+#include \"reset.h\"\n #include \"revision.h\"\n #include \"sequencer.h\"\n #include \"strvec.h\"\n #include \"tree.h\"\n+#include \"tree-walk.h\"\n #include \"unpack-trees.h\"\n #include \"wt-status.h\"\n \n+#define GIT_HISTORY_DROP_USAGE \\\n+\tN_(\"git history drop <commit> [--dry-run] [--update-refs=(branches|head)] [--empty=(drop|keep|abort)]\")\n #define GIT_HISTORY_FIXUP_USAGE \\\n \tN_(\"git history fixup <commit> [--dry-run] [--update-refs=(branches|head)] [--reedit-message] [--empty=(drop|keep|abort)]\")\n #define GIT_HISTORY_REWORD_USAGE \\\n@@ -1001,12 +1005,193 @@ static int cmd_history_split(int argc,\n \treturn ret;\n }\n \n+static int update_worktree(struct repository *repo,\n+\t\t\t   const struct commit *old_head,\n+\t\t\t   const struct commit *new_head,\n+\t\t\t   bool dry_run)\n+{\n+\tstruct reset_working_tree_options opts = {\n+\t\t.oid_from = &old_head->object.oid,\n+\t\t.oid = &new_head->object.oid,\n+\t};\n+\tif (dry_run)\n+\t\topts.flags |= RESET_WORKING_TREE_DRY_RUN;\n+\treturn reset_working_tree(repo, &opts);\n+}\n+\n+static int find_head_tree_change(struct repository *repo,\n+\t\t\t\t const struct replay_result *result,\n+\t\t\t\t struct commit **old_head,\n+\t\t\t\t struct commit **new_head,\n+\t\t\t\t bool *changed)\n+{\n+\tconst struct replay_ref_update *head_update = NULL;\n+\tstruct commit *old_head_commit, *new_head_commit;\n+\tstruct tree *old_head_tree, *new_head_tree;\n+\tconst char *head_target;\n+\tint head_flags;\n+\n+\t*changed = false;\n+\n+\thead_target = refs_resolve_ref_unsafe(get_main_ref_store(repo),\n+\t\t\t\t\t      \"HEAD\", RESOLVE_REF_NO_RECURSE,\n+\t\t\t\t\t      NULL, &head_flags);\n+\tif (!head_target)\n+\t\treturn error(_(\"cannot look up HEAD\"));\n+\tif (!(head_flags & REF_ISSYMREF))\n+\t\thead_target = \"HEAD\";\n+\n+\tfor (size_t i = 0; i < result->updates_nr; i++) {\n+\t\tif (!strcmp(result->updates[i].refname, head_target)) {\n+\t\t\thead_update = &result->updates[i];\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\n+\tif (!head_update)\n+\t\treturn 0;\n+\n+\told_head_commit = lookup_commit_reference(repo, &head_update->old_oid);\n+\tnew_head_commit = lookup_commit_reference(repo, &head_update->new_oid);\n+\tif (!old_head_commit || !new_head_commit)\n+\t\treturn error(_(\"cannot resolve HEAD commit\"));\n+\n+\told_head_tree = repo_get_commit_tree(repo, old_head_commit);\n+\tnew_head_tree = repo_get_commit_tree(repo, new_head_commit);\n+\tif (!old_head_tree || !new_head_tree)\n+\t\treturn error(_(\"cannot resolve tree for HEAD\"));\n+\n+\tif (oideq(&old_head_tree->object.oid, &new_head_tree->object.oid))\n+\t\treturn 0;\n+\n+\t*old_head = old_head_commit;\n+\t*new_head = new_head_commit;\n+\t*changed = true;\n+\n+\treturn 0;\n+}\n+\n+static int cmd_history_drop(int argc,\n+\t\t\t    const char **argv,\n+\t\t\t    const char *prefix,\n+\t\t\t    struct repository *repo)\n+{\n+\tconst char * const usage[] = {\n+\t\tGIT_HISTORY_DROP_USAGE,\n+\t\tNULL,\n+\t};\n+\tenum replay_empty_commit_action empty = REPLAY_EMPTY_COMMIT_DROP;\n+\tenum ref_action action = REF_ACTION_DEFAULT;\n+\tint dry_run = 0;\n+\tstruct option options[] = {\n+\t\tOPT_CALLBACK_F(0, \"update-refs\", &action, \"(branches|head)\",\n+\t\t\t       N_(\"control which refs should be updated\"),\n+\t\t\t       PARSE_OPT_NONEG, parse_ref_action),\n+\t\tOPT_BOOL('n', \"dry-run\", &dry_run,\n+\t\t\t N_(\"perform a dry-run without updating any refs\")),\n+\t\tOPT_CALLBACK_F(0, \"empty\", &empty, \"(drop|keep|abort)\",\n+\t\t\t       N_(\"how to handle descendants that become empty\"),\n+\t\t\t       PARSE_OPT_NONEG, parse_opt_empty),\n+\t\tOPT_END(),\n+\t};\n+\tstruct strbuf reflog_msg = STRBUF_INIT;\n+\tstruct commit *original, *rewritten;\n+\tstruct rev_info revs = { 0 };\n+\tstruct replay_result result = { 0 };\n+\tstruct commit *old_head, *new_head;\n+\tbool head_moves = false;\n+\tint ret;\n+\n+\targc = parse_options(argc, argv, prefix, options, usage, 0);\n+\tif (argc != 1) {\n+\t\tret = error(_(\"command expects a single revision\"));\n+\t\tgoto out;\n+\t}\n+\trepo_config(repo, git_default_config, NULL);\n+\n+\tif (action == REF_ACTION_DEFAULT)\n+\t\taction = REF_ACTION_BRANCHES;\n+\n+\toriginal = lookup_commit_reference_by_name(argv[0]);\n+\tif (!original) {\n+\t\tret = error(_(\"commit cannot be found: %s\"), argv[0]);\n+\t\tgoto out;\n+\t}\n+\n+\tif (!original->parents) {\n+\t\tret = error(_(\"cannot drop root commit %s: \"\n+\t\t\t      \"it has no parent to replay onto\"),\n+\t\t\t    argv[0]);\n+\t\tgoto out;\n+\t} else if (original->parents->next) {\n+\t\tret = error(_(\"cannot drop merge commit: %s\"), argv[0]);\n+\t\tgoto out;\n+\t}\n+\n+\tret = setup_revwalk(repo, action, original, &revs);\n+\tif (ret)\n+\t\tgoto out;\n+\n+\trewritten = original->parents->item;\n+\n+\tret = compute_pending_ref_updates(&revs, action, original, rewritten,\n+\t\t\t\t\t  empty, &result);\n+\tif (ret) {\n+\t\tret = error(_(\"failed replaying descendants\"));\n+\t\tgoto out;\n+\t}\n+\n+\t/*\n+\t * If HEAD will move as a result of the rewrite then we'll have to\n+\t * merge in the changes into the worktree and index. This merge can of\n+\t * course conflict, which will cause the whole operation to abort.\n+\t *\n+\t * If we had already updated the refs at that point then we'd have an\n+\t * inconsistent repository state. So we first perform a dry-run merge\n+\t * here before updating refs.\n+\t */\n+\tif (!is_bare_repository()) {\n+\t\tret = find_head_tree_change(repo, &result, &old_head,\n+\t\t\t\t\t    &new_head, &head_moves);\n+\t\tif (ret < 0)\n+\t\t\tgoto out;\n+\n+\t\tif (head_moves && update_worktree(repo, old_head, new_head, true) < 0) {\n+\t\t\tret = error(_(\"dropping this commit would \"\n+\t\t\t\t      \"overwrite local changes; aborting\"));\n+\t\t\tgoto out;\n+\t\t}\n+\t}\n+\n+\tstrbuf_addf(&reflog_msg, \"drop: dropping %s\", argv[0]);\n+\tret = apply_pending_ref_updates(repo, &result, reflog_msg.buf, dry_run);\n+\tif (ret < 0) {\n+\t\tret = error(_(\"failed to update references\"));\n+\t\tgoto out;\n+\t}\n+\n+\tif (!dry_run && head_moves && update_worktree(repo, old_head, new_head, false) < 0) {\n+\t\tret = error(_(\"could not update working tree to new commit %s\"),\n+\t\t\t    oid_to_hex(&new_head->object.oid));\n+\t\tgoto out;\n+\t}\n+\n+\tret = 0;\n+\n+out:\n+\treplay_result_release(&result);\n+\tstrbuf_release(&reflog_msg);\n+\trelease_revisions(&revs);\n+\treturn ret;\n+}\n+\n int cmd_history(int argc,\n \t\tconst char **argv,\n \t\tconst char *prefix,\n \t\tstruct repository *repo)\n {\n \tconst char * const usage[] = {\n+\t\tGIT_HISTORY_DROP_USAGE,\n \t\tGIT_HISTORY_FIXUP_USAGE,\n \t\tGIT_HISTORY_REWORD_USAGE,\n \t\tGIT_HISTORY_SPLIT_USAGE,\n@@ -1014,6 +1199,7 @@ int cmd_history(int argc,\n \t};\n \tparse_opt_subcommand_fn *fn = NULL;\n \tstruct option options[] = {\n+\t\tOPT_SUBCOMMAND(\"drop\", &fn, cmd_history_drop),\n \t\tOPT_SUBCOMMAND(\"fixup\", &fn, cmd_history_fixup),\n \t\tOPT_SUBCOMMAND(\"reword\", &fn, cmd_history_reword),\n \t\tOPT_SUBCOMMAND(\"split\", &fn, cmd_history_split),\ndiff --git a/t/meson.build b/t/meson.build\nindex 2af8d01279..d5e71056b2 100644\n--- a/t/meson.build\n+++ b/t/meson.build\n@@ -399,6 +399,7 @@ integration_tests = [\n   't3451-history-reword.sh',\n   't3452-history-split.sh',\n   't3453-history-fixup.sh',\n+  't3454-history-drop.sh',\n   't3500-cherry.sh',\n   't3501-revert-cherry-pick.sh',\n   't3502-cherry-pick-merge.sh',\ndiff --git a/t/t3454-history-drop.sh b/t/t3454-history-drop.sh\nnew file mode 100755\nindex 0000000000..0f33247212\n--- /dev/null\n+++ b/t/t3454-history-drop.sh\n@@ -0,0 +1,537 @@\n+#!/bin/sh\n+\n+test_description='tests for git-history drop subcommand'\n+\n+. ./test-lib.sh\n+. \"$TEST_DIRECTORY/lib-log-graph.sh\"\n+\n+expect_graph () {\n+\tcat >expect &&\n+\tlib_test_cmp_graph --format=%s \"$@\"\n+}\n+\n+expect_log () {\n+\tgit log --format=\"%s\" \"$@\" >actual &&\n+\tcat >expect &&\n+\ttest_cmp expect actual\n+}\n+\n+test_expect_success 'errors on missing commit argument' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit initial &&\n+\t\ttest_must_fail git history drop 2>err &&\n+\t\ttest_grep \"command expects a single revision\" err\n+\t)\n+'\n+\n+test_expect_success 'errors on too many arguments' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit initial &&\n+\t\ttest_must_fail git history drop HEAD HEAD 2>err &&\n+\t\ttest_grep \"command expects a single revision\" err\n+\t)\n+'\n+\n+test_expect_success 'errors on unknown revision' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit initial &&\n+\t\ttest_must_fail git history drop does-not-exist 2>err &&\n+\t\ttest_grep \"commit cannot be found: does-not-exist\" err\n+\t)\n+'\n+\n+test_expect_success 'errors with invalid --empty= value' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit initial &&\n+\t\ttest_commit second &&\n+\t\ttest_must_fail git history drop --empty=bogus HEAD 2>err &&\n+\t\ttest_grep \"unrecognized.*--empty.*bogus\" err\n+\t)\n+'\n+\n+test_expect_success 'drops a commit in the middle and replays descendants' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\n+\t\tgit symbolic-ref HEAD >expect &&\n+\t\tgit history drop HEAD~ &&\n+\t\tgit symbolic-ref HEAD >actual &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\texpect_log <<-\\EOF &&\n+\t\tthird\n+\t\tfirst\n+\t\tEOF\n+\n+\t\ttest_must_fail git show HEAD:second.t &&\n+\t\ttest_path_is_missing second.t &&\n+\n+\t\tgit reflog >reflog &&\n+\t\ttest_grep \"drop: dropping HEAD~\" reflog\n+\t)\n+'\n+\n+test_expect_success 'drops the HEAD commit' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\n+\t\tgit history drop HEAD &&\n+\n+\t\texpect_log <<-\\EOF\n+\t\tfirst\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'drops a commit on detached HEAD' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\t\tgit checkout --detach HEAD &&\n+\n+\t\tgit history drop HEAD~ &&\n+\n+\t\texpect_log <<-\\EOF\n+\t\tthird\n+\t\tfirst\n+\t\tEOF\n+\t)\n+'\n+\n+# Note: in this case it would actually be fine to drop the root commit, as we\n+# do have a descendant commit, and no reference points to the root commit\n+# directly. So this is something that we may relax eventually.\n+test_expect_success 'refuses to drop the root commit' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\n+\t\ttest_must_fail git history drop HEAD~ 2>err &&\n+\t\ttest_grep \"cannot drop root commit\" err\n+\t)\n+'\n+\n+# In contrast to the above case, we actually don't want to drop the root commit\n+# here as that would cause us to end up with an empty commit graph.\n+test_expect_success 'refuses to drop the root commit when branch becomes empty' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\n+\t\ttest_must_fail git history drop HEAD 2>err &&\n+\t\ttest_grep \"cannot drop root commit\" err\n+\t)\n+'\n+\n+test_expect_success 'refuses to drop a merge commit' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\tgit branch branch &&\n+\t\ttest_commit ours &&\n+\t\tgit switch branch &&\n+\t\ttest_commit theirs &&\n+\t\tgit switch - &&\n+\t\tgit merge theirs &&\n+\n+\t\ttest_must_fail git history drop HEAD 2>err &&\n+\t\ttest_grep \"cannot drop merge commit\" err\n+\t)\n+'\n+\n+test_expect_success 'refuses when descendants contain a merge commit' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\ttest_commit middle &&\n+\t\tgit branch branch &&\n+\t\ttest_commit ours &&\n+\t\tgit switch branch &&\n+\t\ttest_commit theirs &&\n+\t\tgit switch - &&\n+\t\tgit merge theirs &&\n+\n+\t\ttest_must_fail git history drop middle 2>err &&\n+\t\ttest_grep \"replaying merge commits is not supported yet\" err\n+\t)\n+'\n+\n+test_expect_success 'works in a bare repository' '\n+\ttest_when_finished \"rm -rf repo repo.git\" &&\n+\n+\tgit init repo &&\n+\ttest_commit -C repo first &&\n+\ttest_commit -C repo second &&\n+\ttest_commit -C repo third &&\n+\n+\tgit clone --bare repo repo.git &&\n+\t(\n+\t\tcd repo.git &&\n+\n+\t\tgit history drop HEAD~ &&\n+\t\texpect_log <<-\\EOF\n+\t\tthird\n+\t\tfirst\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'updates branches on other lines of descent' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\ttest_commit target &&\n+\t\tgit branch theirs &&\n+\t\ttest_commit ours &&\n+\t\tgit switch theirs &&\n+\t\ttest_commit theirs &&\n+\n+\t\texpect_graph --branches <<-\\EOF &&\n+\t\t* theirs\n+\t\t| * ours\n+\t\t|/\n+\t\t* target\n+\t\t* base\n+\t\tEOF\n+\n+\t\tgit history drop target &&\n+\n+\t\texpect_graph --branches <<-\\EOF\n+\t\t* ours\n+\t\t| * theirs\n+\t\t|/\n+\t\t* base\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'moves branch pointing at dropped commit to its parent' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo --initial-branch=main &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\tgit branch points-at-second &&\n+\t\ttest_commit third &&\n+\n+\t\tgit rev-parse first >expect &&\n+\t\tgit history drop second &&\n+\t\tgit rev-parse points-at-second >actual &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\texpect_log --format=\"%s %D\" --branches <<-\\EOF\n+\t\tthird HEAD -> main\n+\t\tfirst tag: first, points-at-second\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--dry-run prints ref updates without modifying repo' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo --initial-branch=main &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\tgit branch branch &&\n+\t\ttest_commit middle &&\n+\t\ttest_commit ours &&\n+\t\tgit switch branch &&\n+\t\ttest_commit theirs &&\n+\n+\t\tgit refs list >refs-expect &&\n+\t\tgit history drop --dry-run main~ >updates &&\n+\t\tgit refs list >refs-actual &&\n+\t\ttest_cmp refs-expect refs-actual &&\n+\t\ttest_grep \"update refs/heads/main\" updates &&\n+\n+\t\tgit update-ref --stdin <updates &&\n+\t\texpect_log main <<-\\EOF\n+\t\tours\n+\t\tbase\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--dry-run detects conflicts with modified working tree' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo --initial-branch=main &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second modify-me &&\n+\t\techo modified >modify-me &&\n+\n+\t\tgit refs list >refs-expect &&\n+\t\tgit diff >diff-expect &&\n+\t\ttest_must_fail git history drop --dry-run HEAD 2>err &&\n+\t\ttest_grep \"dropping this commit would overwrite local changes\" err &&\n+\t\tgit diff >diff-actual &&\n+\t\tgit refs list >refs-actual &&\n+\n+\t\ttest_cmp diff-expect diff-actual &&\n+\t\ttest_cmp refs-expect refs-actual\n+\t)\n+'\n+\n+test_expect_success '--update-refs=head updates only HEAD' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo --initial-branch=main &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\ttest_commit target &&\n+\t\tgit branch theirs &&\n+\t\ttest_commit ours &&\n+\t\tgit switch theirs &&\n+\t\ttest_commit theirs &&\n+\n+\t\t# When told to update HEAD only, the command refuses to\n+\t\t# rewrite commits that are not an ancestor of HEAD.\n+\t\ttest_must_fail git history drop --update-refs=head main 2>err &&\n+\t\ttest_grep \"rewritten commit must be an ancestor of HEAD\" err &&\n+\n+\t\texpect_graph --branches <<-\\EOF &&\n+\t\t* theirs\n+\t\t| * ours\n+\t\t|/\n+\t\t* target\n+\t\t* base\n+\t\tEOF\n+\n+\t\tgit switch main &&\n+\t\tgit history drop --update-refs=head target &&\n+\n+\t\texpect_graph --branches <<-\\EOF\n+\t\t* ours\n+\t\t| * theirs\n+\t\t| * target\n+\t\t|/\n+\t\t* base\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'conflict with replayed commit aborts cleanly' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\ttest_commit conflict-a file &&\n+\t\ttest_commit conflict-b file &&\n+\n+\t\tgit refs list >refs-expect &&\n+\t\ttest_must_fail git history drop HEAD~ 2>err &&\n+\t\ttest_grep \"failed replaying descendants\" err &&\n+\t\tgit refs list >refs-actual &&\n+\t\ttest_cmp refs-expect refs-actual\n+\t)\n+'\n+\n+# Build a history where a descendant of the drop target reverts the change\n+# introduced by the drop target. After dropping, the descendant's diff applies\n+# against a tree that already lacks the change, so it becomes empty.\n+setup_empty_descendant_repo () {\n+\tgit init \"$1\" &&\n+\t(\n+\t\tcd \"$1\" &&\n+\t\techo C1 >file &&\n+\t\tgit add file &&\n+\t\tgit commit -m \"base\" &&\n+\t\tgit tag base &&\n+\t\techo C2 >file &&\n+\t\tgit add file &&\n+\t\tgit commit -m \"drop-me\" &&\n+\t\tgit tag drop-me &&\n+\t\ttest_commit middle &&\n+\t\techo C1 >file &&\n+\t\tgit add file &&\n+\t\tgit commit -m \"revert-drop-me\" &&\n+\t\tgit tag revert-drop-me\n+\t)\n+}\n+\n+test_expect_success '--empty=drop drops descendants that become empty' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tsetup_empty_descendant_repo repo &&\n+\t(\n+\t\tcd repo &&\n+\n+\t\tgit history drop --empty=drop drop-me &&\n+\n+\t\texpect_log <<-\\EOF\n+\t\tmiddle\n+\t\tbase\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--empty=keep keeps descendants that become empty' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tsetup_empty_descendant_repo repo &&\n+\t(\n+\t\tcd repo &&\n+\n+\t\tgit history drop --empty=keep drop-me &&\n+\n+\t\texpect_log <<-\\EOF &&\n+\t\trevert-drop-me\n+\t\tmiddle\n+\t\tbase\n+\t\tEOF\n+\t\tgit diff HEAD~ HEAD >diff &&\n+\t\ttest_must_be_empty diff\n+\t)\n+'\n+\n+test_expect_success '--empty=abort errors out when a descendant becomes empty' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tsetup_empty_descendant_repo repo &&\n+\t(\n+\t\tcd repo &&\n+\n+\t\ttest_must_fail git history drop --empty=abort drop-me 2>err &&\n+\t\ttest_grep \"became empty after replay\" err\n+\t)\n+'\n+\n+test_expect_success 'updates index and worktree when HEAD moves' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\n+\t\tgit history drop second &&\n+\n+\t\t# Worktree should no longer contain second.t.\n+\t\ttest_path_is_missing second.t &&\n+\t\ttest_path_is_file first.t &&\n+\t\ttest_path_is_file third.t &&\n+\n+\t\t# Index and worktree should both match the new HEAD.\n+\t\tgit status --porcelain --untracked-files=no >status &&\n+\t\ttest_must_be_empty status\n+\t)\n+'\n+\n+test_expect_success 'updates worktree when dropping HEAD itself' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\n+\t\tgit history drop HEAD &&\n+\n+\t\ttest_path_is_missing second.t &&\n+\t\ttest_path_is_file first.t &&\n+\n+\t\tgit status --porcelain --untracked-files=no >status &&\n+\t\ttest_must_be_empty status\n+\t)\n+'\n+\n+test_expect_success 'preserves unrelated unstaged modifications' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\techo first-content >unrelated.txt &&\n+\t\tgit add unrelated.txt &&\n+\t\tgit commit -m \"add unrelated\" &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\n+\t\techo locally-modified >unrelated.txt &&\n+\n+\t\tgit diff >diff-expect &&\n+\t\tgit history drop second &&\n+\t\tgit diff >diff-actual &&\n+\t\ttest_cmp diff-expect diff-actual &&\n+\t\ttest_path_is_missing second.t\n+\t)\n+'\n+\n+test_expect_success 'preserves unrelated staged changes' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\techo first-content >unrelated.txt &&\n+\t\tgit add unrelated.txt &&\n+\t\tgit commit -m \"add unrelated\" &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\n+\t\techo staged-change >unrelated.txt &&\n+\t\tgit add unrelated.txt &&\n+\n+\t\tgit diff --cached >diff-expect &&\n+\t\tgit history drop second &&\n+\t\tgit diff --cached >diff-actual &&\n+\t\ttest_cmp diff-expect diff-actual &&\n+\t\ttest_path_is_missing second.t\n+\t)\n+'\n+\n+test_expect_success 'aborts when local modifications would be overwritten' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\ttest_commit conflict &&\n+\n+\t\techo local-edit >conflict.t &&\n+\t\tgit diff >diff-expect &&\n+\t\ttest_must_fail git history drop HEAD 2>err &&\n+\t\ttest_grep \"would overwrite local changes\" err &&\n+\t\tgit diff >diff-actual &&\n+\t\ttest_cmp diff-expect diff-actual\n+\t)\n+'\n+\n+test_done\n\n-- \n2.54.0.1189.g8c84645362.dirty\n\n"},{"id":"545309","messageId":"xmqq33ytneiu.fsf@gitster.g","threadId":"65726","inReplyTo":"20260611-b4-pks-history-drop-v5-6-34d35725559c@pks.im","subject":"Re: [PATCH v5 06/10] reset: introduce ability to skip updating HEAD","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-06-11T18:00:25Z","receivedAt":"2026-06-11T18:00:28Z","isPatch":true,"body":"Patrick Steinhardt <ps@pks.im> writes:\n\n> Note that in a previous iteration we instead introduced a flag that made\n> callers opt out of updating any references. This was somewhat awkward\n> though because we already have the `UPDATE_ORIG_HEAD` flag, so the\n> result was somewhat inconsistent.\n>\n> Suggested-by: Phillip Wood <phillip.wood123@gmail.com>\n> Signed-off-by: Patrick Steinhardt <ps@pks.im>\n> ---\n>  builtin/rebase.c | 14 ++++++++++----\n>  reset.c          |  9 +++++++--\n>  reset.h          |  9 ++++++---\n>  sequencer.c      |  4 +++-\n>  4 files changed, 26 insertions(+), 10 deletions(-)\n>\n> diff --git a/reset.c b/reset.c\n> ...\n> @@ -129,7 +133,7 @@ int reset_working_tree(struct repository *r,\n>  \t\toid = &head_oid;\n>  \n>  \tif (refs_only) {\n> -\t\tif (!dry_run)\n> +\t\tif (update_head)\n>  \t\t\treturn update_refs(r, opts, oid, head);\n>  \t\treturn 0;\n>  \t}\n\nSo when refs_only and update-head are in effect, we will call\nupdate_refs(), even if dry_run is given.  update_refs() does not\nseem to pay attention to (opts->flags & RESET_WORKING_TREE_DRY_RUN)\nat all, so wouldn't this mean that we would update even in a dry-run\nsession?\n\n\n"},{"id":"545554","messageId":"ai_z_vloLEwxxmKg@pks.im","threadId":"65726","inReplyTo":"xmqq33ytneiu.fsf@gitster.g","subject":"Re: [PATCH v5 06/10] reset: introduce ability to skip updating HEAD","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-15T12:45:50Z","receivedAt":"2026-06-15T12:45:56Z","isPatch":true,"body":"On Thu, Jun 11, 2026 at 11:00:25AM -0700, Junio C Hamano wrote:\n> Patrick Steinhardt <ps@pks.im> writes:\n> \n> > Note that in a previous iteration we instead introduced a flag that made\n> > callers opt out of updating any references. This was somewhat awkward\n> > though because we already have the `UPDATE_ORIG_HEAD` flag, so the\n> > result was somewhat inconsistent.\n> >\n> > Suggested-by: Phillip Wood <phillip.wood123@gmail.com>\n> > Signed-off-by: Patrick Steinhardt <ps@pks.im>\n> > ---\n> >  builtin/rebase.c | 14 ++++++++++----\n> >  reset.c          |  9 +++++++--\n> >  reset.h          |  9 ++++++---\n> >  sequencer.c      |  4 +++-\n> >  4 files changed, 26 insertions(+), 10 deletions(-)\n> >\n> > diff --git a/reset.c b/reset.c\n> > ...\n> > @@ -129,7 +133,7 @@ int reset_working_tree(struct repository *r,\n> >  \t\toid = &head_oid;\n> >  \n> >  \tif (refs_only) {\n> > -\t\tif (!dry_run)\n> > +\t\tif (update_head)\n> >  \t\t\treturn update_refs(r, opts, oid, head);\n> >  \t\treturn 0;\n> >  \t}\n> \n> So when refs_only and update-head are in effect, we will call\n> update_refs(), even if dry_run is given.  update_refs() does not\n> seem to pay attention to (opts->flags & RESET_WORKING_TREE_DRY_RUN)\n> at all, so wouldn't this mean that we would update even in a dry-run\n> session?\n\nUgh, good catch, this is obviously wrong. Will fix.\n\nPatrick\n"},{"id":"545563","messageId":"20260615-b4-pks-history-drop-v6-0-2e329e536d78@pks.im","threadId":"65726","inReplyTo":"20260601-b4-pks-history-drop-v1-0-643e32340d55@pks.im","subject":"[PATCH v6 00/10] builtin/history: introduce \"drop\" subcommand","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-15T13:54:50Z","receivedAt":"2026-06-15T13:55:00Z","isPatch":true,"body":"Hi,\n\nthis small patch series introduces the new \"drop\" subcommand for\ngit-history(1). As a reader might guess, the command does exactly that:\ngiven a commit, it will drop that commit from the commit history and\nreplay descendant branches on top of it.\n\nChanges in v6:\n  - Fix bad interactions of DRY_RUN with UPDATE_HEAD\n  - Link to v5: https://patch.msgid.link/20260611-b4-pks-history-drop-v5-0-34d35725559c@pks.im\n\nChanges in v5:\n  - Reject UPDATE_ORIG_HEAD without UPDATE_HEAD.\n  - Link to v4: https://patch.msgid.link/20260610-b4-pks-history-drop-v4-0-70d5f0ae8c25@pks.im\n\nChanges in v4:\n  - Remove the `SKIP_REF_UPDATES` flag in favor of a new `UPDATE_HEAD`\n    flag, as suggested by Phillip.\n  - Rename `reset_head()` to `reset_working_tree()`. This better matches\n    the new scope of the function, and it helps us to catch any\n    in-flight patches that would now have to set the `UPDATE_HEAD` flag.\n  - Link to v3: https://patch.msgid.link/20260608-b4-pks-history-drop-v3-0-84ca8e43e937@pks.im\n\nChanges in v3:\n  - Fix commit message typos.\n  - Make `update_orig_head` and `skip_ref_updates` mutually exclusive.\n  - Use fancy revisions to specify the commit to drop in the example\n    section.\n  - Detect conflicting changes in the index/working tree in dry-run\n    mode.\n  - Consistently use a subshell.\n  - Rename `RESET_HEAD_ORIG_HEAD` to `RESET_HEAD_UPDATE_ORIG_HEAD`.\n  - \n  - Link to v2: https://patch.msgid.link/20260603-b4-pks-history-drop-v2-0-742cb5b5176d@pks.im\n\nChanges in v2:\n  - Reworked `update_worktree()` to use `reset_head()`, which required a\n    bunch of changes to `reset_head()`.\n  - Consistently mention the commit that cannot be dropped as part of\n    error messages.\n  - Adapt error message to not use backticks anymore.\n  - Drop redundant \"--graph\" flag in a test helper.\n  - Link to v1: https://patch.msgid.link/20260601-b4-pks-history-drop-v1-0-643e32340d55@pks.im\n\nThanks!\n\nPatrick\n\n---\nPatrick Steinhardt (10):\n      read-cache: split out function to drop unmerged entries to stage 0\n      reset: drop `USE_THE_REPOSITORY_VARIABLE`\n      reset: rename `reset_head()`\n      reset: modernize flags passed to `reset_working_tree()`\n      reset: introduce dry-run mode\n      reset: introduce ability to skip updating HEAD\n      reset: allow the caller to specify the current HEAD object\n      reset: stop assuming that the caller passes in a clean index\n      builtin/history: split handling of ref updates into two phases\n      builtin/history: implement \"drop\" subcommand\n\n Documentation/git-history.adoc |  38 ++-\n builtin/history.c              | 288 +++++++++++++++++++---\n builtin/rebase.c               |  41 ++--\n read-cache-ll.h                |   1 +\n read-cache.c                   |  12 +-\n reset.c                        | 102 +++++---\n reset.h                        |  51 ++--\n sequencer.c                    |  17 +-\n t/meson.build                  |   1 +\n t/t3454-history-drop.sh        | 537 +++++++++++++++++++++++++++++++++++++++++\n 10 files changed, 971 insertions(+), 117 deletions(-)\n\nRange-diff versus v5:\n\n 1:  e21a324987 =  1:  07dee893d7 read-cache: split out function to drop unmerged entries to stage 0\n 2:  d16b3df944 =  2:  96f33165b8 reset: drop `USE_THE_REPOSITORY_VARIABLE`\n 3:  b73224c4b0 =  3:  35f31792f3 reset: rename `reset_head()`\n 4:  1b5fbaa9c2 =  4:  1837483676 reset: modernize flags passed to `reset_working_tree()`\n 5:  a73fb4b3e8 =  5:  206b73f71e reset: introduce dry-run mode\n 6:  3be3208155 !  6:  48c7b1571f reset: introduce ability to skip updating HEAD\n    @@ reset.c: int reset_working_tree(struct repository *r,\n      \n      \tif (refs_only) {\n     -\t\tif (!dry_run)\n    -+\t\tif (update_head)\n    ++\t\tif (!dry_run && update_head)\n      \t\t\treturn update_refs(r, opts, oid, head);\n      \t\treturn 0;\n      \t}\n 7:  fb3a357d93 =  7:  d6a9a3e524 reset: allow the caller to specify the current HEAD object\n 8:  9b883dbbad =  8:  d4bc3acd87 reset: stop assuming that the caller passes in a clean index\n 9:  88a929e1a5 =  9:  493a3f4422 builtin/history: split handling of ref updates into two phases\n10:  8f19defcb0 = 10:  184849df09 builtin/history: implement \"drop\" subcommand\n\n---\nbase-commit: 1666c1265231b0bc5f613fbbf3f0a9896cdef76e\nchange-id: 20260601-b4-pks-history-drop-28f6c6399e7b\n\n"},{"id":"545564","messageId":"20260615-b4-pks-history-drop-v6-1-2e329e536d78@pks.im","threadId":"65726","inReplyTo":"20260615-b4-pks-history-drop-v6-0-2e329e536d78@pks.im","subject":"[PATCH v6 01/10] read-cache: split out function to drop unmerged entries to stage 0","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-15T13:54:51Z","receivedAt":"2026-06-15T13:55:01Z","isPatch":true,"body":"In `repo_read_index_unmerged()` we read the index and then drop any\nunmerged entries to stage 0. In a subsequent commit we'll want to\nperform this operation on arbitrary indexes, not only the one of the\ngiven repository.\n\nPrepare for this by splitting out the functionality into a new function\nthat can act on an arbitrary index.\n\nWhile at it, fix a signedness mismatch when iterating through the index\ncache entries.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n read-cache-ll.h |  1 +\n read-cache.c    | 12 +++++++-----\n 2 files changed, 8 insertions(+), 5 deletions(-)\n\ndiff --git a/read-cache-ll.h b/read-cache-ll.h\nindex 2c8b4b21b1..71b87615eb 100644\n--- a/read-cache-ll.h\n+++ b/read-cache-ll.h\n@@ -309,6 +309,7 @@ int write_locked_index(struct index_state *, struct lock_file *lock, unsigned fl\n void discard_index(struct index_state *);\n void move_index_extensions(struct index_state *dst, struct index_state *src);\n int unmerged_index(const struct index_state *);\n+int index_state_unmerged_to_stage0(struct index_state *istate);\n \n /**\n  * Returns 1 if istate differs from tree, 0 otherwise.  If tree is NULL,\ndiff --git a/read-cache.c b/read-cache.c\nindex 21829102ae..799a5bc719 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -3403,13 +3403,15 @@ int write_locked_index(struct index_state *istate, struct lock_file *lock,\n  */\n int repo_read_index_unmerged(struct repository *repo)\n {\n-\tstruct index_state *istate;\n-\tint i;\n+\trepo_read_index(repo);\n+\treturn index_state_unmerged_to_stage0(repo->index);\n+}\n+\n+int index_state_unmerged_to_stage0(struct index_state *istate)\n+{\n \tint unmerged = 0;\n \n-\trepo_read_index(repo);\n-\tistate = repo->index;\n-\tfor (i = 0; i < istate->cache_nr; i++) {\n+\tfor (unsigned int i = 0; i < istate->cache_nr; i++) {\n \t\tstruct cache_entry *ce = istate->cache[i];\n \t\tstruct cache_entry *new_ce;\n \t\tint len;\n\n-- \n2.55.0.rc0.738.g0c8ab3ebcc.dirty\n\n"},{"id":"545565","messageId":"20260615-b4-pks-history-drop-v6-2-2e329e536d78@pks.im","threadId":"65726","inReplyTo":"20260615-b4-pks-history-drop-v6-0-2e329e536d78@pks.im","subject":"[PATCH v6 02/10] reset: drop `USE_THE_REPOSITORY_VARIABLE`","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-15T13:54:52Z","receivedAt":"2026-06-15T13:55:03Z","isPatch":true,"body":"In \"reset.c\" we still have references to `the_repository`, even though\nthe only entry point into the file already receives a repository as\nparameter.\n\nUpdate all uses of `the_repository` to instead use the passed-in repo\nand drop `USE_THE_REPOSITORY_VARIABLE`.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reset.c | 27 +++++++++++++--------------\n 1 file changed, 13 insertions(+), 14 deletions(-)\n\ndiff --git a/reset.c b/reset.c\nindex 46e30e6394..3b3cb74dab 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -1,5 +1,3 @@\n-#define USE_THE_REPOSITORY_VARIABLE\n-\n #include \"git-compat-util.h\"\n #include \"cache-tree.h\"\n #include \"gettext.h\"\n@@ -13,7 +11,8 @@\n #include \"unpack-trees.h\"\n #include \"hook.h\"\n \n-static int update_refs(const struct reset_head_opts *opts,\n+static int update_refs(struct repository *repo,\n+\t\t       const struct reset_head_opts *opts,\n \t\t       const struct object_id *oid,\n \t\t       const struct object_id *head)\n {\n@@ -42,19 +41,19 @@ static int update_refs(const struct reset_head_opts *opts,\n \tprefix_len = msg.len;\n \n \tif (update_orig_head) {\n-\t\tif (!repo_get_oid(the_repository, \"ORIG_HEAD\", &oid_old_orig))\n+\t\tif (!repo_get_oid(repo, \"ORIG_HEAD\", &oid_old_orig))\n \t\t\told_orig = &oid_old_orig;\n \t\tif (head) {\n \t\t\tif (!reflog_orig_head) {\n \t\t\t\tstrbuf_addstr(&msg, \"updating ORIG_HEAD\");\n \t\t\t\treflog_orig_head = msg.buf;\n \t\t\t}\n-\t\t\trefs_update_ref(get_main_ref_store(the_repository),\n+\t\t\trefs_update_ref(get_main_ref_store(repo),\n \t\t\t\t\treflog_orig_head, \"ORIG_HEAD\",\n \t\t\t\t\torig_head ? orig_head : head,\n \t\t\t\t\told_orig, 0, UPDATE_REFS_MSG_ON_ERR);\n \t\t} else if (old_orig)\n-\t\t\trefs_delete_ref(get_main_ref_store(the_repository),\n+\t\t\trefs_delete_ref(get_main_ref_store(repo),\n \t\t\t\t\tNULL, \"ORIG_HEAD\", old_orig, 0);\n \t}\n \n@@ -64,23 +63,23 @@ static int update_refs(const struct reset_head_opts *opts,\n \t\treflog_head = msg.buf;\n \t}\n \tif (!switch_to_branch)\n-\t\tret = refs_update_ref(get_main_ref_store(the_repository),\n+\t\tret = refs_update_ref(get_main_ref_store(repo),\n \t\t\t\t      reflog_head, \"HEAD\", oid, head,\n \t\t\t\t      detach_head ? REF_NO_DEREF : 0,\n \t\t\t\t      UPDATE_REFS_MSG_ON_ERR);\n \telse {\n-\t\tret = refs_update_ref(get_main_ref_store(the_repository),\n+\t\tret = refs_update_ref(get_main_ref_store(repo),\n \t\t\t\t      reflog_branch ? reflog_branch : reflog_head,\n \t\t\t\t      switch_to_branch, oid, NULL, 0,\n \t\t\t\t      UPDATE_REFS_MSG_ON_ERR);\n \t\tif (!ret)\n-\t\t\tret = refs_update_symref(get_main_ref_store(the_repository),\n+\t\t\tret = refs_update_symref(get_main_ref_store(repo),\n \t\t\t\t\t\t \"HEAD\", switch_to_branch,\n \t\t\t\t\t\t reflog_head);\n \t}\n \tif (!ret && run_hook)\n-\t\trun_hooks_l(the_repository, \"post-checkout\",\n-\t\t\t    oid_to_hex(head ? head : null_oid(the_hash_algo)),\n+\t\trun_hooks_l(repo, \"post-checkout\",\n+\t\t\t    oid_to_hex(head ? head : null_oid(repo->hash_algo)),\n \t\t\t    oid_to_hex(oid), \"1\", NULL);\n \tstrbuf_release(&msg);\n \treturn ret;\n@@ -126,7 +125,7 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \t\toid = &head_oid;\n \n \tif (refs_only)\n-\t\treturn update_refs(opts, oid, head);\n+\t\treturn update_refs(r, opts, oid, head);\n \n \taction = reset_hard ? \"reset\" : \"checkout\";\n \tsetup_unpack_trees_porcelain(&unpack_tree_opts, action);\n@@ -163,7 +162,7 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \t\tgoto leave_reset_head;\n \t}\n \n-\ttree = repo_parse_tree_indirect(the_repository, oid);\n+\ttree = repo_parse_tree_indirect(r, oid);\n \tif (!tree) {\n \t\tret = error(_(\"unable to read tree (%s)\"), oid_to_hex(oid));\n \t\tgoto leave_reset_head;\n@@ -177,7 +176,7 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \t}\n \n \tif (oid != &head_oid || update_orig_head || switch_to_branch)\n-\t\tret = update_refs(opts, oid, head);\n+\t\tret = update_refs(r, opts, oid, head);\n \n leave_reset_head:\n \trollback_lock_file(&lock);\n\n-- \n2.55.0.rc0.738.g0c8ab3ebcc.dirty\n\n"},{"id":"545566","messageId":"20260615-b4-pks-history-drop-v6-3-2e329e536d78@pks.im","threadId":"65726","inReplyTo":"20260615-b4-pks-history-drop-v6-0-2e329e536d78@pks.im","subject":"[PATCH v6 03/10] reset: rename `reset_head()`","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-15T13:54:53Z","receivedAt":"2026-06-15T13:55:05Z","isPatch":true,"body":"In a subsequent commit we're about to adapt `reset_head()` so that the\nreference update to HEAD is optional, only. At this point the function\nstarts to feel misnamed, as it doesn't necessarily have anything to do\nwith the HEAD reference anymore. The gist of the function then is that\nwe reset the working tree to a specific new commit, updating both the\nindex and the checked-out files.\n\nRename it to `reset_working_tree()` to better reflect that.\n\nNote that we don't adjust the flags yet. This will happen in a\nsubsequent commit.\n\nSuggested-by: Phillip Wood <phillip.wood123@gmail.com>\n---\n builtin/rebase.c | 20 ++++++++++----------\n reset.c          |  5 +++--\n reset.h          |  4 ++--\n sequencer.c      |  8 ++++----\n 4 files changed, 19 insertions(+), 18 deletions(-)\n\ndiff --git a/builtin/rebase.c b/builtin/rebase.c\nindex fa4f5d9306..22fbba3c62 100644\n--- a/builtin/rebase.c\n+++ b/builtin/rebase.c\n@@ -592,7 +592,7 @@ static int finish_rebase(struct rebase_options *opts)\n static int move_to_original_branch(struct rebase_options *opts)\n {\n \tstruct strbuf branch_reflog = STRBUF_INIT, head_reflog = STRBUF_INIT;\n-\tstruct reset_head_opts ropts = { 0 };\n+\tstruct reset_working_tree_options ropts = { 0 };\n \tint ret;\n \n \tif (!opts->head_name)\n@@ -610,7 +610,7 @@ static int move_to_original_branch(struct rebase_options *opts)\n \tropts.flags = RESET_HEAD_REFS_ONLY;\n \tropts.branch_msg = branch_reflog.buf;\n \tropts.head_msg = head_reflog.buf;\n-\tret = reset_head(the_repository, &ropts);\n+\tret = reset_working_tree(the_repository, &ropts);\n \n \tstrbuf_release(&branch_reflog);\n \tstrbuf_release(&head_reflog);\n@@ -685,7 +685,7 @@ static int run_am(struct rebase_options *opts)\n \n \tstatus = run_command(&format_patch);\n \tif (status) {\n-\t\tstruct reset_head_opts ropts = { 0 };\n+\t\tstruct reset_working_tree_options ropts = { 0 };\n \t\tunlink(rebased_patches);\n \t\tfree(rebased_patches);\n \t\tchild_process_clear(&am);\n@@ -693,7 +693,7 @@ static int run_am(struct rebase_options *opts)\n \t\tropts.oid = &opts->orig_head->object.oid;\n \t\tropts.branch = opts->head_name;\n \t\tropts.default_reflog_action = opts->reflog_action;\n-\t\treset_head(the_repository, &ropts);\n+\t\treset_working_tree(the_repository, &ropts);\n \t\terror(_(\"\\ngit encountered an error while preparing the \"\n \t\t\t\"patches to replay\\n\"\n \t\t\t\"these revisions:\\n\"\n@@ -855,7 +855,7 @@ static int rebase_config(const char *var, const char *value,\n static int checkout_up_to_date(struct rebase_options *options)\n {\n \tstruct strbuf buf = STRBUF_INIT;\n-\tstruct reset_head_opts ropts = { 0 };\n+\tstruct reset_working_tree_options ropts = { 0 };\n \tint ret = 0;\n \n \tstrbuf_addf(&buf, \"%s: checkout %s\",\n@@ -866,7 +866,7 @@ static int checkout_up_to_date(struct rebase_options *options)\n \tif (!ropts.branch)\n \t\tropts.flags |=  RESET_HEAD_DETACH;\n \tropts.head_msg = buf.buf;\n-\tif (reset_head(the_repository, &ropts) < 0)\n+\tif (reset_working_tree(the_repository, &ropts) < 0)\n \t\tret = error(_(\"could not switch to %s\"), options->switch_to);\n \tstrbuf_release(&buf);\n \n@@ -1116,7 +1116,7 @@ int cmd_rebase(int argc,\n \tint reschedule_failed_exec = -1;\n \tint allow_preemptive_ff = 1;\n \tint preserve_merges_selected = 0;\n-\tstruct reset_head_opts ropts = { 0 };\n+\tstruct reset_working_tree_options ropts = { 0 };\n \tstruct option builtin_rebase_options[] = {\n \t\tOPT_STRING(0, \"onto\", &options.onto_name,\n \t\t\t   N_(\"revision\"),\n@@ -1385,7 +1385,7 @@ int cmd_rebase(int argc,\n \t\trerere_clear(the_repository, &merge_rr);\n \t\tstring_list_clear(&merge_rr, 1);\n \t\tropts.flags = RESET_HEAD_HARD;\n-\t\tif (reset_head(the_repository, &ropts) < 0)\n+\t\tif (reset_working_tree(the_repository, &ropts) < 0)\n \t\t\tdie(_(\"could not discard worktree changes\"));\n \t\tremove_branch_state(the_repository, 0);\n \t\tif (read_basic_state(&options))\n@@ -1410,7 +1410,7 @@ int cmd_rebase(int argc,\n \t\tropts.head_msg = head_msg.buf;\n \t\tropts.branch = options.head_name;\n \t\tropts.flags = RESET_HEAD_HARD;\n-\t\tif (reset_head(the_repository, &ropts) < 0)\n+\t\tif (reset_working_tree(the_repository, &ropts) < 0)\n \t\t\tdie(_(\"could not move back to %s\"),\n \t\t\t    oid_to_hex(&options.orig_head->object.oid));\n \t\tstrbuf_release(&head_msg);\n@@ -1880,7 +1880,7 @@ int cmd_rebase(int argc,\n \t\t\tRESET_HEAD_RUN_POST_CHECKOUT_HOOK;\n \tropts.head_msg = msg.buf;\n \tropts.default_reflog_action = options.reflog_action;\n-\tif (reset_head(the_repository, &ropts)) {\n+\tif (reset_working_tree(the_repository, &ropts)) {\n \t\tret = error(_(\"Could not detach HEAD\"));\n \t\tgoto cleanup_autostash;\n \t}\ndiff --git a/reset.c b/reset.c\nindex 3b3cb74dab..799596398b 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -12,7 +12,7 @@\n #include \"hook.h\"\n \n static int update_refs(struct repository *repo,\n-\t\t       const struct reset_head_opts *opts,\n+\t\t       const struct reset_working_tree_options *opts,\n \t\t       const struct object_id *oid,\n \t\t       const struct object_id *head)\n {\n@@ -85,7 +85,8 @@ static int update_refs(struct repository *repo,\n \treturn ret;\n }\n \n-int reset_head(struct repository *r, const struct reset_head_opts *opts)\n+int reset_working_tree(struct repository *r,\n+\t\t       const struct reset_working_tree_options *opts)\n {\n \tconst struct object_id *oid = opts->oid;\n \tconst char *switch_to_branch = opts->branch;\ndiff --git a/reset.h b/reset.h\nindex a28f81829d..f130152014 100644\n--- a/reset.h\n+++ b/reset.h\n@@ -17,7 +17,7 @@\n /* Update ORIG_HEAD as well as HEAD */\n #define RESET_ORIG_HEAD (1<<4)\n \n-struct reset_head_opts {\n+struct reset_working_tree_options {\n \t/*\n \t * The commit to checkout/reset to. Defaults to HEAD.\n \t */\n@@ -55,6 +55,6 @@ struct reset_head_opts {\n \tconst char *default_reflog_action;\n };\n \n-int reset_head(struct repository *r, const struct reset_head_opts *opts);\n+int reset_working_tree(struct repository *r, const struct reset_working_tree_options *opts);\n \n #endif\ndiff --git a/sequencer.c b/sequencer.c\nindex 1ee4b2875b..d73ecf0384 100644\n--- a/sequencer.c\n+++ b/sequencer.c\n@@ -4677,7 +4677,7 @@ static void create_autostash_internal(struct repository *r,\n \tif (has_unstaged_changes(r, 1) ||\n \t    has_uncommitted_changes(r, 1)) {\n \t\tstruct child_process stash = CHILD_PROCESS_INIT;\n-\t\tstruct reset_head_opts ropts = { .flags = RESET_HEAD_HARD };\n+\t\tstruct reset_working_tree_options ropts = { .flags = RESET_HEAD_HARD };\n \t\tstruct object_id oid;\n \n \t\tstrvec_pushl(&stash.args,\n@@ -4707,7 +4707,7 @@ static void create_autostash_internal(struct repository *r,\n \n \t\tif (!silent)\n \t\t\tprintf(_(\"Created autostash: %s\\n\"), buf.buf);\n-\t\tif (reset_head(r, &ropts) < 0)\n+\t\tif (reset_working_tree(r, &ropts) < 0)\n \t\t\tdie(_(\"could not reset --hard\"));\n \t\tdiscard_index(r->index);\n \t\tif (repo_read_index(r) < 0)\n@@ -4867,7 +4867,7 @@ static int checkout_onto(struct repository *r, struct replay_opts *opts,\n \t\t\t const char *onto_name, const struct object_id *onto,\n \t\t\t const struct object_id *orig_head)\n {\n-\tstruct reset_head_opts ropts = {\n+\tstruct reset_working_tree_options ropts = {\n \t\t.oid = onto,\n \t\t.orig_head = orig_head,\n \t\t.flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD |\n@@ -4876,7 +4876,7 @@ static int checkout_onto(struct repository *r, struct replay_opts *opts,\n \t\t\t\t\t   onto_name),\n \t\t.default_reflog_action = sequencer_reflog_action(opts)\n \t};\n-\tif (reset_head(r, &ropts)) {\n+\tif (reset_working_tree(r, &ropts)) {\n \t\tapply_autostash(rebase_path_autostash());\n \t\tsequencer_remove_state(opts);\n \t\treturn error(_(\"could not detach HEAD\"));\n\n-- \n2.55.0.rc0.738.g0c8ab3ebcc.dirty\n\n"},{"id":"545567","messageId":"20260615-b4-pks-history-drop-v6-4-2e329e536d78@pks.im","threadId":"65726","inReplyTo":"20260615-b4-pks-history-drop-v6-0-2e329e536d78@pks.im","subject":"[PATCH v6 04/10] reset: modernize flags passed to `reset_working_tree()`","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-15T13:54:54Z","receivedAt":"2026-06-15T13:55:07Z","isPatch":true,"body":"The flags passed to `reset_working_tree()` are declared as defines. This\nhas fallen a bit out of practice nowadays, where we instead prefer to\nuse enums. Furthermore, the prefix of those flags does not match the\nfunction name anymore after the rename in the preceding commit.\n\nAdapt the code to follow modern best practices and adapt the flag names.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n builtin/rebase.c | 15 ++++++++-------\n reset.c          | 12 ++++++------\n reset.h          | 31 +++++++++++++++++++------------\n sequencer.c      |  9 ++++++---\n 4 files changed, 39 insertions(+), 28 deletions(-)\n\ndiff --git a/builtin/rebase.c b/builtin/rebase.c\nindex 22fbba3c62..06dcbaf5e8 100644\n--- a/builtin/rebase.c\n+++ b/builtin/rebase.c\n@@ -607,7 +607,7 @@ static int move_to_original_branch(struct rebase_options *opts)\n \tstrbuf_addf(&head_reflog, \"%s (finish): returning to %s\",\n \t\t    opts->reflog_action, opts->head_name);\n \tropts.branch = opts->head_name;\n-\tropts.flags = RESET_HEAD_REFS_ONLY;\n+\tropts.flags = RESET_WORKING_TREE_REFS_ONLY;\n \tropts.branch_msg = branch_reflog.buf;\n \tropts.head_msg = head_reflog.buf;\n \tret = reset_working_tree(the_repository, &ropts);\n@@ -862,9 +862,9 @@ static int checkout_up_to_date(struct rebase_options *options)\n \t\t    options->reflog_action, options->switch_to);\n \tropts.oid = &options->orig_head->object.oid;\n \tropts.branch = options->head_name;\n-\tropts.flags = RESET_HEAD_RUN_POST_CHECKOUT_HOOK;\n+\tropts.flags = RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK;\n \tif (!ropts.branch)\n-\t\tropts.flags |=  RESET_HEAD_DETACH;\n+\t\tropts.flags |=  RESET_WORKING_TREE_DETACH;\n \tropts.head_msg = buf.buf;\n \tif (reset_working_tree(the_repository, &ropts) < 0)\n \t\tret = error(_(\"could not switch to %s\"), options->switch_to);\n@@ -1384,7 +1384,7 @@ int cmd_rebase(int argc,\n \n \t\trerere_clear(the_repository, &merge_rr);\n \t\tstring_list_clear(&merge_rr, 1);\n-\t\tropts.flags = RESET_HEAD_HARD;\n+\t\tropts.flags = RESET_WORKING_TREE_HARD;\n \t\tif (reset_working_tree(the_repository, &ropts) < 0)\n \t\t\tdie(_(\"could not discard worktree changes\"));\n \t\tremove_branch_state(the_repository, 0);\n@@ -1409,7 +1409,7 @@ int cmd_rebase(int argc,\n \t\tropts.oid = &options.orig_head->object.oid;\n \t\tropts.head_msg = head_msg.buf;\n \t\tropts.branch = options.head_name;\n-\t\tropts.flags = RESET_HEAD_HARD;\n+\t\tropts.flags = RESET_WORKING_TREE_HARD;\n \t\tif (reset_working_tree(the_repository, &ropts) < 0)\n \t\t\tdie(_(\"could not move back to %s\"),\n \t\t\t    oid_to_hex(&options.orig_head->object.oid));\n@@ -1876,8 +1876,9 @@ int cmd_rebase(int argc,\n \t\t    options.reflog_action, options.onto_name);\n \tropts.oid = &options.onto->object.oid;\n \tropts.orig_head = &options.orig_head->object.oid;\n-\tropts.flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD |\n-\t\t\tRESET_HEAD_RUN_POST_CHECKOUT_HOOK;\n+\tropts.flags = RESET_WORKING_TREE_DETACH |\n+\t\t      RESET_WORKING_TREE_UPDATE_ORIG_HEAD |\n+\t\t      RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK;\n \tropts.head_msg = msg.buf;\n \tropts.default_reflog_action = options.reflog_action;\n \tif (reset_working_tree(the_repository, &ropts)) {\ndiff --git a/reset.c b/reset.c\nindex 799596398b..4ca7f23a25 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -16,9 +16,9 @@ static int update_refs(struct repository *repo,\n \t\t       const struct object_id *oid,\n \t\t       const struct object_id *head)\n {\n-\tunsigned detach_head = opts->flags & RESET_HEAD_DETACH;\n-\tunsigned run_hook = opts->flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK;\n-\tunsigned update_orig_head = opts->flags & RESET_ORIG_HEAD;\n+\tunsigned detach_head = opts->flags & RESET_WORKING_TREE_DETACH;\n+\tunsigned run_hook = opts->flags & RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK;\n+\tunsigned update_orig_head = opts->flags & RESET_WORKING_TREE_UPDATE_ORIG_HEAD;\n \tconst struct object_id *orig_head = opts->orig_head;\n \tconst char *switch_to_branch = opts->branch;\n \tconst char *reflog_branch = opts->branch_msg;\n@@ -90,9 +90,9 @@ int reset_working_tree(struct repository *r,\n {\n \tconst struct object_id *oid = opts->oid;\n \tconst char *switch_to_branch = opts->branch;\n-\tunsigned reset_hard = opts->flags & RESET_HEAD_HARD;\n-\tunsigned refs_only = opts->flags & RESET_HEAD_REFS_ONLY;\n-\tunsigned update_orig_head = opts->flags & RESET_ORIG_HEAD;\n+\tunsigned reset_hard = opts->flags & RESET_WORKING_TREE_HARD;\n+\tunsigned refs_only = opts->flags & RESET_WORKING_TREE_REFS_ONLY;\n+\tunsigned update_orig_head = opts->flags & RESET_WORKING_TREE_UPDATE_ORIG_HEAD;\n \tstruct object_id *head = NULL, head_oid;\n \tstruct tree_desc desc[2] = { { NULL }, { NULL } };\n \tstruct lock_file lock = LOCK_INIT;\ndiff --git a/reset.h b/reset.h\nindex f130152014..2e5826de99 100644\n--- a/reset.h\n+++ b/reset.h\n@@ -6,16 +6,22 @@\n \n #define GIT_REFLOG_ACTION_ENVIRONMENT \"GIT_REFLOG_ACTION\"\n \n-/* Request a detached checkout */\n-#define RESET_HEAD_DETACH (1<<0)\n-/* Request a reset rather than a checkout */\n-#define RESET_HEAD_HARD (1<<1)\n-/* Run the post-checkout hook */\n-#define RESET_HEAD_RUN_POST_CHECKOUT_HOOK (1<<2)\n-/* Only update refs, do not touch the worktree */\n-#define RESET_HEAD_REFS_ONLY (1<<3)\n-/* Update ORIG_HEAD as well as HEAD */\n-#define RESET_ORIG_HEAD (1<<4)\n+enum reset_working_tree_flags {\n+\t/* Request a detached checkout */\n+\tRESET_WORKING_TREE_DETACH = (1 << 0),\n+\n+\t/* Request a reset rather than a checkout */\n+\tRESET_WORKING_TREE_HARD = (1 << 1),\n+\n+\t/* Run the post-checkout hook */\n+\tRESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK = (1 << 2),\n+\n+\t/* Only update refs, do not touch the worktree */\n+\tRESET_WORKING_TREE_REFS_ONLY = (1 << 3),\n+\n+\t/* Update ORIG_HEAD as well as HEAD */\n+\tRESET_WORKING_TREE_UPDATE_ORIG_HEAD = (1 << 4),\n+};\n \n struct reset_working_tree_options {\n \t/*\n@@ -33,7 +39,7 @@ struct reset_working_tree_options {\n \t/*\n \t * Flags defined above.\n \t */\n-\tunsigned flags;\n+\tenum reset_working_tree_flags flags;\n \t/*\n \t * Optional reflog message for branch, defaults to head_msg.\n \t */\n@@ -45,7 +51,8 @@ struct reset_working_tree_options {\n \tconst char *head_msg;\n \t/*\n \t * Optional reflog message for ORIG_HEAD, if this omitted and flags\n-\t * contains RESET_ORIG_HEAD then default_reflog_action must be given.\n+\t * contains RESET_WORKING_TREE_UPDATE_ORIG_HEAD then\n+\t * default_reflog_action must be given.\n \t */\n \tconst char *orig_head_msg;\n \t/*\ndiff --git a/sequencer.c b/sequencer.c\nindex d73ecf0384..4efe831178 100644\n--- a/sequencer.c\n+++ b/sequencer.c\n@@ -4677,7 +4677,9 @@ static void create_autostash_internal(struct repository *r,\n \tif (has_unstaged_changes(r, 1) ||\n \t    has_uncommitted_changes(r, 1)) {\n \t\tstruct child_process stash = CHILD_PROCESS_INIT;\n-\t\tstruct reset_working_tree_options ropts = { .flags = RESET_HEAD_HARD };\n+\t\tstruct reset_working_tree_options ropts = {\n+\t\t\t.flags = RESET_WORKING_TREE_HARD,\n+\t\t};\n \t\tstruct object_id oid;\n \n \t\tstrvec_pushl(&stash.args,\n@@ -4870,8 +4872,9 @@ static int checkout_onto(struct repository *r, struct replay_opts *opts,\n \tstruct reset_working_tree_options ropts = {\n \t\t.oid = onto,\n \t\t.orig_head = orig_head,\n-\t\t.flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD |\n-\t\t\t\tRESET_HEAD_RUN_POST_CHECKOUT_HOOK,\n+\t\t.flags = RESET_WORKING_TREE_DETACH |\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.default_reflog_action = sequencer_reflog_action(opts)\n\n-- \n2.55.0.rc0.738.g0c8ab3ebcc.dirty\n\n"},{"id":"545568","messageId":"20260615-b4-pks-history-drop-v6-5-2e329e536d78@pks.im","threadId":"65726","inReplyTo":"20260615-b4-pks-history-drop-v6-0-2e329e536d78@pks.im","subject":"[PATCH v6 05/10] reset: introduce dry-run mode","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-15T13:54:55Z","receivedAt":"2026-06-15T13:55:09Z","isPatch":true,"body":"In a subsequent commit we'll add another caller to `reset_working_tree()`\nthat wants to perform a dry-run check of whether it would be possible to\nupdate the index and working tree when moving to a new commit. Introduce\na new flag that lets the caller perform this operation.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reset.c | 44 +++++++++++++++++++++++++++++++++-----------\n reset.h |  6 ++++++\n 2 files changed, 39 insertions(+), 11 deletions(-)\n\ndiff --git a/reset.c b/reset.c\nindex 4ca7f23a25..99f2c1b012 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -93,11 +93,14 @@ int reset_working_tree(struct repository *r,\n \tunsigned reset_hard = opts->flags & RESET_WORKING_TREE_HARD;\n \tunsigned refs_only = opts->flags & RESET_WORKING_TREE_REFS_ONLY;\n \tunsigned update_orig_head = opts->flags & RESET_WORKING_TREE_UPDATE_ORIG_HEAD;\n+\tunsigned dry_run = opts->flags & RESET_WORKING_TREE_DRY_RUN;\n \tstruct object_id *head = NULL, head_oid;\n \tstruct tree_desc desc[2] = { { NULL }, { NULL } };\n \tstruct lock_file lock = LOCK_INIT;\n \tstruct unpack_trees_options unpack_tree_opts = { 0 };\n \tstruct tree *tree;\n+\tstruct index_state scratch_index = INDEX_STATE_INIT(r);\n+\tstruct index_state *istate;\n \tconst char *action;\n \tint ret = 0, nr = 0;\n \n@@ -110,7 +113,7 @@ int reset_working_tree(struct repository *r,\n \tif (opts->branch_msg && !opts->branch)\n \t\tBUG(\"branch reflog message given without a branch\");\n \n-\tif (!refs_only && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {\n+\tif (!refs_only && !dry_run && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {\n \t\tret = -1;\n \t\tgoto leave_reset_head;\n \t}\n@@ -125,16 +128,36 @@ int reset_working_tree(struct repository *r,\n \tif (!oid)\n \t\toid = &head_oid;\n \n-\tif (refs_only)\n-\t\treturn update_refs(r, opts, oid, head);\n+\tif (refs_only) {\n+\t\tif (!dry_run)\n+\t\t\treturn update_refs(r, opts, oid, head);\n+\t\treturn 0;\n+\t}\n+\n+\tif (dry_run) {\n+\t\tif (read_index_from(&scratch_index, r->index_file, r->gitdir) < 0 ||\n+\t\t    index_state_unmerged_to_stage0(&scratch_index) < 0) {\n+\t\t\tret = error(_(\"could not read index\"));\n+\t\t\tgoto leave_reset_head;\n+\t\t}\n+\n+\t\tistate = &scratch_index;\n+\t} else {\n+\t\tif (repo_read_index_unmerged(r) < 0) {\n+\t\t\tret = error(_(\"could not read index\"));\n+\t\t\tgoto leave_reset_head;\n+\t\t}\n+\t\tistate = r->index;\n+\t}\n \n \taction = reset_hard ? \"reset\" : \"checkout\";\n \tsetup_unpack_trees_porcelain(&unpack_tree_opts, action);\n \tunpack_tree_opts.head_idx = 1;\n-\tunpack_tree_opts.src_index = r->index;\n-\tunpack_tree_opts.dst_index = r->index;\n+\tunpack_tree_opts.src_index = istate;\n+\tunpack_tree_opts.dst_index = istate;\n \tunpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge;\n-\tunpack_tree_opts.update = 1;\n+\tunpack_tree_opts.update = !dry_run;\n+\tunpack_tree_opts.dry_run = dry_run;\n \tunpack_tree_opts.merge = 1;\n \tunpack_tree_opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */\n \tunpack_tree_opts.skip_cache_tree_update = 1;\n@@ -142,11 +165,6 @@ int reset_working_tree(struct repository *r,\n \tif (reset_hard)\n \t\tunpack_tree_opts.reset = UNPACK_RESET_PROTECT_UNTRACKED;\n \n-\tif (repo_read_index_unmerged(r) < 0) {\n-\t\tret = error(_(\"could not read index\"));\n-\t\tgoto leave_reset_head;\n-\t}\n-\n \tif (!reset_hard && !fill_tree_descriptor(r, &desc[nr++], &head_oid)) {\n \t\tret = error(_(\"failed to find tree of %s\"),\n \t\t\t    oid_to_hex(&head_oid));\n@@ -163,6 +181,9 @@ int reset_working_tree(struct repository *r,\n \t\tgoto leave_reset_head;\n \t}\n \n+\tif (dry_run)\n+\t\tgoto leave_reset_head;\n+\n \ttree = repo_parse_tree_indirect(r, oid);\n \tif (!tree) {\n \t\tret = error(_(\"unable to read tree (%s)\"), oid_to_hex(oid));\n@@ -182,6 +203,7 @@ int reset_working_tree(struct repository *r,\n leave_reset_head:\n \trollback_lock_file(&lock);\n \tclear_unpack_trees_porcelain(&unpack_tree_opts);\n+\trelease_index(&scratch_index);\n \twhile (nr)\n \t\tfree((void *)desc[--nr].buffer);\n \treturn ret;\ndiff --git a/reset.h b/reset.h\nindex 2e5826de99..898e4a1e95 100644\n--- a/reset.h\n+++ b/reset.h\n@@ -21,6 +21,12 @@ enum reset_working_tree_flags {\n \n \t/* Update ORIG_HEAD as well as HEAD */\n \tRESET_WORKING_TREE_UPDATE_ORIG_HEAD = (1 << 4),\n+\n+\t/*\n+\t * Perform a dry-run by performing the operation without updating\n+\t * any user-visible state.\n+\t */\n+\tRESET_WORKING_TREE_DRY_RUN = (1 << 5),\n };\n \n struct reset_working_tree_options {\n\n-- \n2.55.0.rc0.738.g0c8ab3ebcc.dirty\n\n"},{"id":"545569","messageId":"20260615-b4-pks-history-drop-v6-6-2e329e536d78@pks.im","threadId":"65726","inReplyTo":"20260615-b4-pks-history-drop-v6-0-2e329e536d78@pks.im","subject":"[PATCH v6 06/10] reset: introduce ability to skip updating HEAD","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-15T13:54:56Z","receivedAt":"2026-06-15T13:55:13Z","isPatch":true,"body":"In a subsequent commit we'll introduce a new caller to\n`reset_working_tree()` that really only wants to update the index and\nworking tree, without updating any references. Introduce a new flag that\nmakes the caller opt in to updating HEAD and adapt all callers to set\nthat flag.\n\nNote that in a previous iteration we instead introduced a flag that made\ncallers opt out of updating any references. This was somewhat awkward\nthough because we already have the `UPDATE_ORIG_HEAD` flag, so the\nresult was somewhat inconsistent.\n\nSuggested-by: Phillip Wood <phillip.wood123@gmail.com>\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n builtin/rebase.c | 14 ++++++++++----\n reset.c          |  9 +++++++--\n reset.h          |  9 ++++++---\n sequencer.c      |  4 +++-\n 4 files changed, 26 insertions(+), 10 deletions(-)\n\ndiff --git a/builtin/rebase.c b/builtin/rebase.c\nindex 06dcbaf5e8..10a306310c 100644\n--- a/builtin/rebase.c\n+++ b/builtin/rebase.c\n@@ -607,7 +607,8 @@ static int move_to_original_branch(struct rebase_options *opts)\n \tstrbuf_addf(&head_reflog, \"%s (finish): returning to %s\",\n \t\t    opts->reflog_action, opts->head_name);\n \tropts.branch = opts->head_name;\n-\tropts.flags = RESET_WORKING_TREE_REFS_ONLY;\n+\tropts.flags = RESET_WORKING_TREE_REFS_ONLY |\n+\t\t      RESET_WORKING_TREE_UPDATE_HEAD;\n \tropts.branch_msg = branch_reflog.buf;\n \tropts.head_msg = head_reflog.buf;\n \tret = reset_working_tree(the_repository, &ropts);\n@@ -693,6 +694,7 @@ static int run_am(struct rebase_options *opts)\n \t\tropts.oid = &opts->orig_head->object.oid;\n \t\tropts.branch = opts->head_name;\n \t\tropts.default_reflog_action = opts->reflog_action;\n+\t\tropts.flags = RESET_WORKING_TREE_UPDATE_HEAD;\n \t\treset_working_tree(the_repository, &ropts);\n \t\terror(_(\"\\ngit encountered an error while preparing the \"\n \t\t\t\"patches to replay\\n\"\n@@ -862,7 +864,8 @@ static int checkout_up_to_date(struct rebase_options *options)\n \t\t    options->reflog_action, options->switch_to);\n \tropts.oid = &options->orig_head->object.oid;\n \tropts.branch = options->head_name;\n-\tropts.flags = RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK;\n+\tropts.flags = RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK |\n+\t\t      RESET_WORKING_TREE_UPDATE_HEAD;\n \tif (!ropts.branch)\n \t\tropts.flags |=  RESET_WORKING_TREE_DETACH;\n \tropts.head_msg = buf.buf;\n@@ -1384,7 +1387,8 @@ int cmd_rebase(int argc,\n \n \t\trerere_clear(the_repository, &merge_rr);\n \t\tstring_list_clear(&merge_rr, 1);\n-\t\tropts.flags = RESET_WORKING_TREE_HARD;\n+\t\tropts.flags = RESET_WORKING_TREE_HARD |\n+\t\t\t      RESET_WORKING_TREE_UPDATE_HEAD;\n \t\tif (reset_working_tree(the_repository, &ropts) < 0)\n \t\t\tdie(_(\"could not discard worktree changes\"));\n \t\tremove_branch_state(the_repository, 0);\n@@ -1409,7 +1413,8 @@ int cmd_rebase(int argc,\n \t\tropts.oid = &options.orig_head->object.oid;\n \t\tropts.head_msg = head_msg.buf;\n \t\tropts.branch = options.head_name;\n-\t\tropts.flags = RESET_WORKING_TREE_HARD;\n+\t\tropts.flags = RESET_WORKING_TREE_HARD |\n+\t\t\t      RESET_WORKING_TREE_UPDATE_HEAD;\n \t\tif (reset_working_tree(the_repository, &ropts) < 0)\n \t\t\tdie(_(\"could not move back to %s\"),\n \t\t\t    oid_to_hex(&options.orig_head->object.oid));\n@@ -1877,6 +1882,7 @@ int cmd_rebase(int argc,\n \tropts.oid = &options.onto->object.oid;\n \tropts.orig_head = &options.orig_head->object.oid;\n \tropts.flags = RESET_WORKING_TREE_DETACH |\n+\t\t      RESET_WORKING_TREE_UPDATE_HEAD |\n \t\t      RESET_WORKING_TREE_UPDATE_ORIG_HEAD |\n \t\t      RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK;\n \tropts.head_msg = msg.buf;\ndiff --git a/reset.c b/reset.c\nindex 99f2c1b012..4bde5d8dc6 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -92,6 +92,7 @@ int reset_working_tree(struct repository *r,\n \tconst char *switch_to_branch = opts->branch;\n \tunsigned reset_hard = opts->flags & RESET_WORKING_TREE_HARD;\n \tunsigned refs_only = opts->flags & RESET_WORKING_TREE_REFS_ONLY;\n+\tunsigned update_head = opts->flags & RESET_WORKING_TREE_UPDATE_HEAD;\n \tunsigned update_orig_head = opts->flags & RESET_WORKING_TREE_UPDATE_ORIG_HEAD;\n \tunsigned dry_run = opts->flags & RESET_WORKING_TREE_DRY_RUN;\n \tstruct object_id *head = NULL, head_oid;\n@@ -113,6 +114,9 @@ int reset_working_tree(struct repository *r,\n \tif (opts->branch_msg && !opts->branch)\n \t\tBUG(\"branch reflog message given without a branch\");\n \n+\tif (update_orig_head && !update_head)\n+\t\tBUG(\"cannot update ORIG_HEAD without updating HEAD\" );\n+\n \tif (!refs_only && !dry_run && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {\n \t\tret = -1;\n \t\tgoto leave_reset_head;\n@@ -129,7 +133,7 @@ int reset_working_tree(struct repository *r,\n \t\toid = &head_oid;\n \n \tif (refs_only) {\n-\t\tif (!dry_run)\n+\t\tif (!dry_run && update_head)\n \t\t\treturn update_refs(r, opts, oid, head);\n \t\treturn 0;\n \t}\n@@ -197,7 +201,8 @@ int reset_working_tree(struct repository *r,\n \t\tgoto leave_reset_head;\n \t}\n \n-\tif (oid != &head_oid || update_orig_head || switch_to_branch)\n+\tif (update_head &&\n+\t    (oid != &head_oid || update_orig_head || switch_to_branch))\n \t\tret = update_refs(r, opts, oid, head);\n \n leave_reset_head:\ndiff --git a/reset.h b/reset.h\nindex 898e4a1e95..38b2891b53 100644\n--- a/reset.h\n+++ b/reset.h\n@@ -19,14 +19,17 @@ enum reset_working_tree_flags {\n \t/* Only update refs, do not touch the worktree */\n \tRESET_WORKING_TREE_REFS_ONLY = (1 << 3),\n \n-\t/* Update ORIG_HEAD as well as HEAD */\n-\tRESET_WORKING_TREE_UPDATE_ORIG_HEAD = (1 << 4),\n+\t/* Update HEAD */\n+\tRESET_WORKING_TREE_UPDATE_HEAD = (1 << 4),\n+\n+\t/* Update ORIG_HEAD */\n+\tRESET_WORKING_TREE_UPDATE_ORIG_HEAD = (1 << 5),\n \n \t/*\n \t * Perform a dry-run by performing the operation without updating\n \t * any user-visible state.\n \t */\n-\tRESET_WORKING_TREE_DRY_RUN = (1 << 5),\n+\tRESET_WORKING_TREE_DRY_RUN = (1 << 6),\n };\n \n struct reset_working_tree_options {\ndiff --git a/sequencer.c b/sequencer.c\nindex 4efe831178..e905b1b2d9 100644\n--- a/sequencer.c\n+++ b/sequencer.c\n@@ -4678,7 +4678,8 @@ static void create_autostash_internal(struct repository *r,\n \t    has_uncommitted_changes(r, 1)) {\n \t\tstruct child_process stash = CHILD_PROCESS_INIT;\n \t\tstruct reset_working_tree_options ropts = {\n-\t\t\t.flags = RESET_WORKING_TREE_HARD,\n+\t\t\t.flags = RESET_WORKING_TREE_HARD |\n+\t\t\t\t RESET_WORKING_TREE_UPDATE_HEAD,\n \t\t};\n \t\tstruct object_id oid;\n \n@@ -4873,6 +4874,7 @@ static int checkout_onto(struct repository *r, struct replay_opts *opts,\n \t\t.oid = onto,\n \t\t.orig_head = orig_head,\n \t\t.flags = RESET_WORKING_TREE_DETACH |\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\n-- \n2.55.0.rc0.738.g0c8ab3ebcc.dirty\n\n"},{"id":"545570","messageId":"20260615-b4-pks-history-drop-v6-7-2e329e536d78@pks.im","threadId":"65726","inReplyTo":"20260615-b4-pks-history-drop-v6-0-2e329e536d78@pks.im","subject":"[PATCH v6 07/10] reset: allow the caller to specify the current HEAD object","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-15T13:54:57Z","receivedAt":"2026-06-15T13:55:15Z","isPatch":true,"body":"When calling `reset_working_tree()` we automatically derive the commit\nthat the callers wants to move from by reading the HEAD commit. Some\ncallers may already have resolved it, or they may want to move from a\ndifferent commit that doesn't match HEAD.\n\nIntroduce a new `oid_from` option that lets the caller specify the\ncommit.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reset.c | 5 ++++-\n reset.h | 5 +++++\n 2 files changed, 9 insertions(+), 1 deletion(-)\n\ndiff --git a/reset.c b/reset.c\nindex 4bde5d8dc6..06f375f296 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -122,7 +122,10 @@ int reset_working_tree(struct repository *r,\n \t\tgoto leave_reset_head;\n \t}\n \n-\tif (!repo_get_oid(r, \"HEAD\", &head_oid)) {\n+\tif (opts->oid_from) {\n+\t\toidcpy(&head_oid, opts->oid_from);\n+\t\thead = &head_oid;\n+\t} else if (!repo_get_oid(r, \"HEAD\", &head_oid)) {\n \t\thead = &head_oid;\n \t} else if (!oid || !reset_hard) {\n \t\tret = error(_(\"could not determine HEAD revision\"));\ndiff --git a/reset.h b/reset.h\nindex 38b2891b53..4c992ba671 100644\n--- a/reset.h\n+++ b/reset.h\n@@ -37,6 +37,11 @@ struct reset_working_tree_options {\n \t * The commit to checkout/reset to. Defaults to HEAD.\n \t */\n \tconst struct object_id *oid;\n+\t/*\n+\t * The commit to checkout/reset from when doing a two-way merge. This\n+\t * is used as one of the sides to merge.\n+\t */\n+\tconst struct object_id *oid_from;\n \t/*\n \t * Optional value to set ORIG_HEAD. Defaults to HEAD.\n \t */\n\n-- \n2.55.0.rc0.738.g0c8ab3ebcc.dirty\n\n"},{"id":"545571","messageId":"20260615-b4-pks-history-drop-v6-8-2e329e536d78@pks.im","threadId":"65726","inReplyTo":"20260615-b4-pks-history-drop-v6-0-2e329e536d78@pks.im","subject":"[PATCH v6 08/10] reset: stop assuming that the caller passes in a clean index","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-15T13:54:58Z","receivedAt":"2026-06-15T13:55:18Z","isPatch":true,"body":"In 652bd0211d (rebase: use 'skip_cache_tree_update' option, 2022-11-10),\nwe updated `reset_working_tree()` to stop updating the index tree cache.\nThis was done as a performance optimization: the function is only called\nby \"sequencer.c\" and \"rebase.c\", both of which assume a clean index\nbefore they perform their operation, so we know that the end result will\nbe a clean index, too. Consequently, we can skip recomputing the cache\nas we can instead use `prime_cache_tree()` directly.\n\nIn a subsequent commit we're about to add a new caller though where the\nassumption doesn't hold anymore: the index may be dirty before calling\n`reset_working_tree()`, and consequently we cannot prime the cache with\na given tree anymore as the index and tree will mismatch.\n\nAdapt the logic so that we only skip the cache tree update in case we're\ndoing a hard reset. While we could introduce logic that only skips the\nupdate in case the incoming index was dirty already, that doesn't really\nfeel worth it: after all, the mentioned commit says itself that the\nperformance improvement was negligible anyway.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reset.c | 8 +++++---\n 1 file changed, 5 insertions(+), 3 deletions(-)\n\ndiff --git a/reset.c b/reset.c\nindex 06f375f296..ff87e3e357 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -167,10 +167,11 @@ int reset_working_tree(struct repository *r,\n \tunpack_tree_opts.dry_run = dry_run;\n \tunpack_tree_opts.merge = 1;\n \tunpack_tree_opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */\n-\tunpack_tree_opts.skip_cache_tree_update = 1;\n \tinit_checkout_metadata(&unpack_tree_opts.meta, switch_to_branch, oid, NULL);\n-\tif (reset_hard)\n+\tif (reset_hard) {\n+\t\tunpack_tree_opts.skip_cache_tree_update = 1;\n \t\tunpack_tree_opts.reset = UNPACK_RESET_PROTECT_UNTRACKED;\n+\t}\n \n \tif (!reset_hard && !fill_tree_descriptor(r, &desc[nr++], &head_oid)) {\n \t\tret = error(_(\"failed to find tree of %s\"),\n@@ -197,7 +198,8 @@ int reset_working_tree(struct repository *r,\n \t\tgoto leave_reset_head;\n \t}\n \n-\tprime_cache_tree(r, r->index, tree);\n+\tif (reset_hard)\n+\t\tprime_cache_tree(r, r->index, tree);\n \n \tif (write_locked_index(r->index, &lock, COMMIT_LOCK) < 0) {\n \t\tret = error(_(\"could not write index\"));\n\n-- \n2.55.0.rc0.738.g0c8ab3ebcc.dirty\n\n"},{"id":"545572","messageId":"20260615-b4-pks-history-drop-v6-9-2e329e536d78@pks.im","threadId":"65726","inReplyTo":"20260615-b4-pks-history-drop-v6-0-2e329e536d78@pks.im","subject":"[PATCH v6 09/10] builtin/history: split handling of ref updates into two phases","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-15T13:54:59Z","receivedAt":"2026-06-15T13:55:20Z","isPatch":true,"body":"The function `handle_reference_updates()` is used by git-history(1) to\nupdate all references that refer to commits that have been rewritten. As\nsuch, it performs two steps:\n\n  - It gathers the references that need to be updated in the first\n    place.\n\n  - It prepares and commits the reference transaction.\n\nIn a subsequent commit we'll want to handle those two steps separately.\nPrepare for this by splitting up the function into two.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n builtin/history.c | 102 ++++++++++++++++++++++++++++++++++--------------------\n 1 file changed, 64 insertions(+), 38 deletions(-)\n\ndiff --git a/builtin/history.c b/builtin/history.c\nindex 0fc06fb204..4fadf38c32 100644\n--- a/builtin/history.c\n+++ b/builtin/history.c\n@@ -333,21 +333,17 @@ static int handle_ref_update(struct ref_transaction *transaction,\n \t\t\t\t      NULL, NULL, 0, reflog_msg, err);\n }\n \n-static int handle_reference_updates(struct rev_info *revs,\n-\t\t\t\t    enum ref_action action,\n-\t\t\t\t    struct commit *original,\n-\t\t\t\t    struct commit *rewritten,\n-\t\t\t\t    const char *reflog_msg,\n-\t\t\t\t    int dry_run,\n-\t\t\t\t    enum replay_empty_commit_action empty)\n+static int compute_pending_ref_updates(struct rev_info *revs,\n+\t\t\t\t       enum ref_action action,\n+\t\t\t\t       struct commit *original,\n+\t\t\t\t       struct commit *rewritten,\n+\t\t\t\t       enum replay_empty_commit_action empty,\n+\t\t\t\t       struct replay_result *result)\n {\n \tconst struct name_decoration *decoration;\n \tstruct replay_revisions_options opts = {\n \t\t.empty = empty,\n \t};\n-\tstruct replay_result result = { 0 };\n-\tstruct ref_transaction *transaction = NULL;\n-\tstruct strbuf err = STRBUF_INIT;\n \tchar hex[GIT_MAX_HEXSZ + 1];\n \tbool detached_head;\n \tint head_flags = 0;\n@@ -359,34 +355,13 @@ static int handle_reference_updates(struct rev_info *revs,\n \n \topts.onto = oid_to_hex_r(hex, &rewritten->object.oid);\n \n-\tret = replay_revisions(revs, &opts, &result);\n+\tret = replay_revisions(revs, &opts, result);\n \tif (ret)\n-\t\tgoto out;\n+\t\treturn ret;\n \n \tif (action != REF_ACTION_BRANCHES && action != REF_ACTION_HEAD)\n \t\tBUG(\"unsupported ref action %d\", action);\n \n-\tif (!dry_run) {\n-\t\ttransaction = ref_store_transaction_begin(get_main_ref_store(revs->repo), 0, &err);\n-\t\tif (!transaction) {\n-\t\t\tret = error(_(\"failed to begin ref transaction: %s\"), err.buf);\n-\t\t\tgoto out;\n-\t\t}\n-\t}\n-\n-\tfor (size_t i = 0; i < result.updates_nr; i++) {\n-\t\tret = handle_ref_update(transaction,\n-\t\t\t\t\tresult.updates[i].refname,\n-\t\t\t\t\t&result.updates[i].new_oid,\n-\t\t\t\t\t&result.updates[i].old_oid,\n-\t\t\t\t\treflog_msg, &err);\n-\t\tif (ret) {\n-\t\t\tret = error(_(\"failed to update ref '%s': %s\"),\n-\t\t\t\t    result.updates[i].refname, err.buf);\n-\t\t\tgoto out;\n-\t\t}\n-\t}\n-\n \t/*\n \t * `replay_revisions()` only updates references that are\n \t * ancestors of `rewritten`, so we need to manually\n@@ -414,14 +389,43 @@ static int handle_reference_updates(struct rev_info *revs,\n \t\t    !detached_head)\n \t\t\tcontinue;\n \n+\t\tALLOC_GROW(result->updates, result->updates_nr + 1, result->updates_alloc);\n+\t\tresult->updates[result->updates_nr].refname = xstrdup(decoration->name);\n+\t\tresult->updates[result->updates_nr].old_oid = original->object.oid;\n+\t\tresult->updates[result->updates_nr].new_oid = rewritten->object.oid;\n+\t\tresult->updates_nr++;\n+\t}\n+\n+\treturn 0;\n+}\n+\n+static int apply_pending_ref_updates(struct repository *repo,\n+\t\t\t\t     const struct replay_result *result,\n+\t\t\t\t     const char *reflog_msg,\n+\t\t\t\t     int dry_run)\n+{\n+\tstruct ref_transaction *transaction = NULL;\n+\tstruct strbuf err = STRBUF_INIT;\n+\tint ret;\n+\n+\tif (!dry_run) {\n+\t\ttransaction = ref_store_transaction_begin(get_main_ref_store(repo),\n+\t\t\t\t\t\t\t  0, &err);\n+\t\tif (!transaction) {\n+\t\t\tret = error(_(\"failed to begin ref transaction: %s\"), err.buf);\n+\t\t\tgoto out;\n+\t\t}\n+\t}\n+\n+\tfor (size_t i = 0; i < result->updates_nr; i++) {\n \t\tret = handle_ref_update(transaction,\n-\t\t\t\t\tdecoration->name,\n-\t\t\t\t\t&rewritten->object.oid,\n-\t\t\t\t\t&original->object.oid,\n+\t\t\t\t\tresult->updates[i].refname,\n+\t\t\t\t\t&result->updates[i].new_oid,\n+\t\t\t\t\t&result->updates[i].old_oid,\n \t\t\t\t\treflog_msg, &err);\n \t\tif (ret) {\n \t\t\tret = error(_(\"failed to update ref '%s': %s\"),\n-\t\t\t\t    decoration->name, err.buf);\n+\t\t\t\t    result->updates[i].refname, err.buf);\n \t\t\tgoto out;\n \t\t}\n \t}\n@@ -435,11 +439,33 @@ static int handle_reference_updates(struct rev_info *revs,\n \n out:\n \tref_transaction_free(transaction);\n-\treplay_result_release(&result);\n \tstrbuf_release(&err);\n \treturn ret;\n }\n \n+static int handle_reference_updates(struct rev_info *revs,\n+\t\t\t\t    enum ref_action action,\n+\t\t\t\t    struct commit *original,\n+\t\t\t\t    struct commit *rewritten,\n+\t\t\t\t    const char *reflog_msg,\n+\t\t\t\t    int dry_run,\n+\t\t\t\t    enum replay_empty_commit_action empty)\n+{\n+\tstruct replay_result result = { 0 };\n+\tint ret;\n+\n+\tret = compute_pending_ref_updates(revs, action, original, rewritten,\n+\t\t\t\t\t  empty, &result);\n+\tif (ret)\n+\t\tgoto out;\n+\n+\tret = apply_pending_ref_updates(revs->repo, &result, reflog_msg, dry_run);\n+\n+out:\n+\treplay_result_release(&result);\n+\treturn ret;\n+}\n+\n static int commit_became_empty(struct repository *repo,\n \t\t\t       struct commit *original,\n \t\t\t       struct tree *result)\n\n-- \n2.55.0.rc0.738.g0c8ab3ebcc.dirty\n\n"},{"id":"545573","messageId":"20260615-b4-pks-history-drop-v6-10-2e329e536d78@pks.im","threadId":"65726","inReplyTo":"20260615-b4-pks-history-drop-v6-0-2e329e536d78@pks.im","subject":"[PATCH v6 10/10] builtin/history: implement \"drop\" subcommand","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-15T13:55:00Z","receivedAt":"2026-06-15T13:55:23Z","isPatch":true,"body":"A common operation when editing the commit history is to drop a specific\ncommit from the history entirely, but this operation is not currently\ncovered by git-history(1).\n\nA couple of noteworthy bits:\n\n  - This is the first git-history(1) command that will ultimately result\n    in changes to both the index and the working tree. We thus have to\n    add logic to merge resulting changes into those.\n\n  - It is still not possible to replay merge commits, so this limitation\n    is inherited for the new \"drop\" command.\n\n  - For now we refuse to drop root commits. While we _can_ indeed drop\n    root commits in the general case, there are edge cases where the\n    resulting history would become completely empty. This is thus left\n    to a subsequent patch series.\n\nOther than that, most of the logic is rather straight-forward as we can\ncontinue to build on the preexisting logic in git-history(1) for most of\nthe part.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n Documentation/git-history.adoc |  38 ++-\n builtin/history.c              | 186 ++++++++++++++\n t/meson.build                  |   1 +\n t/t3454-history-drop.sh        | 537 +++++++++++++++++++++++++++++++++++++++++\n 4 files changed, 761 insertions(+), 1 deletion(-)\n\ndiff --git a/Documentation/git-history.adoc b/Documentation/git-history.adoc\nindex 2ba8121795..28b477cd37 100644\n--- a/Documentation/git-history.adoc\n+++ b/Documentation/git-history.adoc\n@@ -8,6 +8,7 @@ git-history - EXPERIMENTAL: Rewrite history\n SYNOPSIS\n --------\n [synopsis]\n+git history drop <commit> [--dry-run] [--update-refs=(branches|head)] [--empty=(drop|keep|abort)]\n git history fixup <commit> [--dry-run] [--update-refs=(branches|head)] [--reedit-message] [--empty=(drop|keep|abort)]\n git history reword <commit> [--dry-run] [--update-refs=(branches|head)]\n git history split <commit> [--dry-run] [--update-refs=(branches|head)] [--] [<pathspec>...]\n@@ -51,13 +52,28 @@ be stateful operations. The limitation can be lifted once (if) Git learns about\n first-class conflicts.\n \n When using `fixup` with `--empty=drop`, dropping the root commit is not yet\n-supported.\n+supported. Likewise, `drop` cannot remove the root commit or a merge commit.\n \n COMMANDS\n --------\n \n The following commands are available to rewrite history in different ways:\n \n+`drop <commit>`::\n+\tRemove the specified commit from the history. All descendants of the\n+\tcommit are replayed directly onto its parent.\n++\n+The root commit cannot be dropped as that may lead to edge cases where refs\n+end up with no commits anymore. Merge commits cannot be dropped either; see\n+LIMITATIONS.\n++\n+If `HEAD` points at a commit that is to be rewritten, the index and working\n+tree are updated to match the new `HEAD`. The command aborts before any\n+references are updated in case local modifications would be overwritten.\n++\n+If replaying any descendant would result in a conflict, the command aborts\n+with an error.\n+\n `fixup <commit>`::\n \tApply the currently staged changes to the specified commit. This is\n \tsimilar in nature to `git commit --fixup=<commit>` followed by `git\n@@ -170,6 +186,26 @@ The staged addition of `unrelated.txt` has been incorporated into the `first`\n commit. All descendant commits have been replayed on top of the rewritten\n history.\n \n+Drop a commit\n+~~~~~~~~~~~~~\n+\n+----------\n+$ git log --oneline\n+abc1234 (HEAD -> main) third\n+def5678 second\n+ghi9012 first\n+\n+$ git history drop 'main^{/second}'\n+\n+$ git log --oneline\n+jkl3456 (HEAD -> main) third\n+ghi9012 first\n+----------\n+\n+The `second` commit has been removed from the history, and `third` has been\n+replayed directly on top of `first`. All branches that pointed at the dropped\n+commit have been moved to its parent.\n+\n Split a commit\n ~~~~~~~~~~~~~~\n \ndiff --git a/builtin/history.c b/builtin/history.c\nindex 4fadf38c32..e59f69233b 100644\n--- a/builtin/history.c\n+++ b/builtin/history.c\n@@ -17,13 +17,17 @@\n #include \"read-cache.h\"\n #include \"refs.h\"\n #include \"replay.h\"\n+#include \"reset.h\"\n #include \"revision.h\"\n #include \"sequencer.h\"\n #include \"strvec.h\"\n #include \"tree.h\"\n+#include \"tree-walk.h\"\n #include \"unpack-trees.h\"\n #include \"wt-status.h\"\n \n+#define GIT_HISTORY_DROP_USAGE \\\n+\tN_(\"git history drop <commit> [--dry-run] [--update-refs=(branches|head)] [--empty=(drop|keep|abort)]\")\n #define GIT_HISTORY_FIXUP_USAGE \\\n \tN_(\"git history fixup <commit> [--dry-run] [--update-refs=(branches|head)] [--reedit-message] [--empty=(drop|keep|abort)]\")\n #define GIT_HISTORY_REWORD_USAGE \\\n@@ -1001,12 +1005,193 @@ static int cmd_history_split(int argc,\n \treturn ret;\n }\n \n+static int update_worktree(struct repository *repo,\n+\t\t\t   const struct commit *old_head,\n+\t\t\t   const struct commit *new_head,\n+\t\t\t   bool dry_run)\n+{\n+\tstruct reset_working_tree_options opts = {\n+\t\t.oid_from = &old_head->object.oid,\n+\t\t.oid = &new_head->object.oid,\n+\t};\n+\tif (dry_run)\n+\t\topts.flags |= RESET_WORKING_TREE_DRY_RUN;\n+\treturn reset_working_tree(repo, &opts);\n+}\n+\n+static int find_head_tree_change(struct repository *repo,\n+\t\t\t\t const struct replay_result *result,\n+\t\t\t\t struct commit **old_head,\n+\t\t\t\t struct commit **new_head,\n+\t\t\t\t bool *changed)\n+{\n+\tconst struct replay_ref_update *head_update = NULL;\n+\tstruct commit *old_head_commit, *new_head_commit;\n+\tstruct tree *old_head_tree, *new_head_tree;\n+\tconst char *head_target;\n+\tint head_flags;\n+\n+\t*changed = false;\n+\n+\thead_target = refs_resolve_ref_unsafe(get_main_ref_store(repo),\n+\t\t\t\t\t      \"HEAD\", RESOLVE_REF_NO_RECURSE,\n+\t\t\t\t\t      NULL, &head_flags);\n+\tif (!head_target)\n+\t\treturn error(_(\"cannot look up HEAD\"));\n+\tif (!(head_flags & REF_ISSYMREF))\n+\t\thead_target = \"HEAD\";\n+\n+\tfor (size_t i = 0; i < result->updates_nr; i++) {\n+\t\tif (!strcmp(result->updates[i].refname, head_target)) {\n+\t\t\thead_update = &result->updates[i];\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\n+\tif (!head_update)\n+\t\treturn 0;\n+\n+\told_head_commit = lookup_commit_reference(repo, &head_update->old_oid);\n+\tnew_head_commit = lookup_commit_reference(repo, &head_update->new_oid);\n+\tif (!old_head_commit || !new_head_commit)\n+\t\treturn error(_(\"cannot resolve HEAD commit\"));\n+\n+\told_head_tree = repo_get_commit_tree(repo, old_head_commit);\n+\tnew_head_tree = repo_get_commit_tree(repo, new_head_commit);\n+\tif (!old_head_tree || !new_head_tree)\n+\t\treturn error(_(\"cannot resolve tree for HEAD\"));\n+\n+\tif (oideq(&old_head_tree->object.oid, &new_head_tree->object.oid))\n+\t\treturn 0;\n+\n+\t*old_head = old_head_commit;\n+\t*new_head = new_head_commit;\n+\t*changed = true;\n+\n+\treturn 0;\n+}\n+\n+static int cmd_history_drop(int argc,\n+\t\t\t    const char **argv,\n+\t\t\t    const char *prefix,\n+\t\t\t    struct repository *repo)\n+{\n+\tconst char * const usage[] = {\n+\t\tGIT_HISTORY_DROP_USAGE,\n+\t\tNULL,\n+\t};\n+\tenum replay_empty_commit_action empty = REPLAY_EMPTY_COMMIT_DROP;\n+\tenum ref_action action = REF_ACTION_DEFAULT;\n+\tint dry_run = 0;\n+\tstruct option options[] = {\n+\t\tOPT_CALLBACK_F(0, \"update-refs\", &action, \"(branches|head)\",\n+\t\t\t       N_(\"control which refs should be updated\"),\n+\t\t\t       PARSE_OPT_NONEG, parse_ref_action),\n+\t\tOPT_BOOL('n', \"dry-run\", &dry_run,\n+\t\t\t N_(\"perform a dry-run without updating any refs\")),\n+\t\tOPT_CALLBACK_F(0, \"empty\", &empty, \"(drop|keep|abort)\",\n+\t\t\t       N_(\"how to handle descendants that become empty\"),\n+\t\t\t       PARSE_OPT_NONEG, parse_opt_empty),\n+\t\tOPT_END(),\n+\t};\n+\tstruct strbuf reflog_msg = STRBUF_INIT;\n+\tstruct commit *original, *rewritten;\n+\tstruct rev_info revs = { 0 };\n+\tstruct replay_result result = { 0 };\n+\tstruct commit *old_head, *new_head;\n+\tbool head_moves = false;\n+\tint ret;\n+\n+\targc = parse_options(argc, argv, prefix, options, usage, 0);\n+\tif (argc != 1) {\n+\t\tret = error(_(\"command expects a single revision\"));\n+\t\tgoto out;\n+\t}\n+\trepo_config(repo, git_default_config, NULL);\n+\n+\tif (action == REF_ACTION_DEFAULT)\n+\t\taction = REF_ACTION_BRANCHES;\n+\n+\toriginal = lookup_commit_reference_by_name(argv[0]);\n+\tif (!original) {\n+\t\tret = error(_(\"commit cannot be found: %s\"), argv[0]);\n+\t\tgoto out;\n+\t}\n+\n+\tif (!original->parents) {\n+\t\tret = error(_(\"cannot drop root commit %s: \"\n+\t\t\t      \"it has no parent to replay onto\"),\n+\t\t\t    argv[0]);\n+\t\tgoto out;\n+\t} else if (original->parents->next) {\n+\t\tret = error(_(\"cannot drop merge commit: %s\"), argv[0]);\n+\t\tgoto out;\n+\t}\n+\n+\tret = setup_revwalk(repo, action, original, &revs);\n+\tif (ret)\n+\t\tgoto out;\n+\n+\trewritten = original->parents->item;\n+\n+\tret = compute_pending_ref_updates(&revs, action, original, rewritten,\n+\t\t\t\t\t  empty, &result);\n+\tif (ret) {\n+\t\tret = error(_(\"failed replaying descendants\"));\n+\t\tgoto out;\n+\t}\n+\n+\t/*\n+\t * If HEAD will move as a result of the rewrite then we'll have to\n+\t * merge in the changes into the worktree and index. This merge can of\n+\t * course conflict, which will cause the whole operation to abort.\n+\t *\n+\t * If we had already updated the refs at that point then we'd have an\n+\t * inconsistent repository state. So we first perform a dry-run merge\n+\t * here before updating refs.\n+\t */\n+\tif (!is_bare_repository()) {\n+\t\tret = find_head_tree_change(repo, &result, &old_head,\n+\t\t\t\t\t    &new_head, &head_moves);\n+\t\tif (ret < 0)\n+\t\t\tgoto out;\n+\n+\t\tif (head_moves && update_worktree(repo, old_head, new_head, true) < 0) {\n+\t\t\tret = error(_(\"dropping this commit would \"\n+\t\t\t\t      \"overwrite local changes; aborting\"));\n+\t\t\tgoto out;\n+\t\t}\n+\t}\n+\n+\tstrbuf_addf(&reflog_msg, \"drop: dropping %s\", argv[0]);\n+\tret = apply_pending_ref_updates(repo, &result, reflog_msg.buf, dry_run);\n+\tif (ret < 0) {\n+\t\tret = error(_(\"failed to update references\"));\n+\t\tgoto out;\n+\t}\n+\n+\tif (!dry_run && head_moves && update_worktree(repo, old_head, new_head, false) < 0) {\n+\t\tret = error(_(\"could not update working tree to new commit %s\"),\n+\t\t\t    oid_to_hex(&new_head->object.oid));\n+\t\tgoto out;\n+\t}\n+\n+\tret = 0;\n+\n+out:\n+\treplay_result_release(&result);\n+\tstrbuf_release(&reflog_msg);\n+\trelease_revisions(&revs);\n+\treturn ret;\n+}\n+\n int cmd_history(int argc,\n \t\tconst char **argv,\n \t\tconst char *prefix,\n \t\tstruct repository *repo)\n {\n \tconst char * const usage[] = {\n+\t\tGIT_HISTORY_DROP_USAGE,\n \t\tGIT_HISTORY_FIXUP_USAGE,\n \t\tGIT_HISTORY_REWORD_USAGE,\n \t\tGIT_HISTORY_SPLIT_USAGE,\n@@ -1014,6 +1199,7 @@ int cmd_history(int argc,\n \t};\n \tparse_opt_subcommand_fn *fn = NULL;\n \tstruct option options[] = {\n+\t\tOPT_SUBCOMMAND(\"drop\", &fn, cmd_history_drop),\n \t\tOPT_SUBCOMMAND(\"fixup\", &fn, cmd_history_fixup),\n \t\tOPT_SUBCOMMAND(\"reword\", &fn, cmd_history_reword),\n \t\tOPT_SUBCOMMAND(\"split\", &fn, cmd_history_split),\ndiff --git a/t/meson.build b/t/meson.build\nindex 2af8d01279..d5e71056b2 100644\n--- a/t/meson.build\n+++ b/t/meson.build\n@@ -399,6 +399,7 @@ integration_tests = [\n   't3451-history-reword.sh',\n   't3452-history-split.sh',\n   't3453-history-fixup.sh',\n+  't3454-history-drop.sh',\n   't3500-cherry.sh',\n   't3501-revert-cherry-pick.sh',\n   't3502-cherry-pick-merge.sh',\ndiff --git a/t/t3454-history-drop.sh b/t/t3454-history-drop.sh\nnew file mode 100755\nindex 0000000000..0f33247212\n--- /dev/null\n+++ b/t/t3454-history-drop.sh\n@@ -0,0 +1,537 @@\n+#!/bin/sh\n+\n+test_description='tests for git-history drop subcommand'\n+\n+. ./test-lib.sh\n+. \"$TEST_DIRECTORY/lib-log-graph.sh\"\n+\n+expect_graph () {\n+\tcat >expect &&\n+\tlib_test_cmp_graph --format=%s \"$@\"\n+}\n+\n+expect_log () {\n+\tgit log --format=\"%s\" \"$@\" >actual &&\n+\tcat >expect &&\n+\ttest_cmp expect actual\n+}\n+\n+test_expect_success 'errors on missing commit argument' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit initial &&\n+\t\ttest_must_fail git history drop 2>err &&\n+\t\ttest_grep \"command expects a single revision\" err\n+\t)\n+'\n+\n+test_expect_success 'errors on too many arguments' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit initial &&\n+\t\ttest_must_fail git history drop HEAD HEAD 2>err &&\n+\t\ttest_grep \"command expects a single revision\" err\n+\t)\n+'\n+\n+test_expect_success 'errors on unknown revision' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit initial &&\n+\t\ttest_must_fail git history drop does-not-exist 2>err &&\n+\t\ttest_grep \"commit cannot be found: does-not-exist\" err\n+\t)\n+'\n+\n+test_expect_success 'errors with invalid --empty= value' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit initial &&\n+\t\ttest_commit second &&\n+\t\ttest_must_fail git history drop --empty=bogus HEAD 2>err &&\n+\t\ttest_grep \"unrecognized.*--empty.*bogus\" err\n+\t)\n+'\n+\n+test_expect_success 'drops a commit in the middle and replays descendants' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\n+\t\tgit symbolic-ref HEAD >expect &&\n+\t\tgit history drop HEAD~ &&\n+\t\tgit symbolic-ref HEAD >actual &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\texpect_log <<-\\EOF &&\n+\t\tthird\n+\t\tfirst\n+\t\tEOF\n+\n+\t\ttest_must_fail git show HEAD:second.t &&\n+\t\ttest_path_is_missing second.t &&\n+\n+\t\tgit reflog >reflog &&\n+\t\ttest_grep \"drop: dropping HEAD~\" reflog\n+\t)\n+'\n+\n+test_expect_success 'drops the HEAD commit' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\n+\t\tgit history drop HEAD &&\n+\n+\t\texpect_log <<-\\EOF\n+\t\tfirst\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'drops a commit on detached HEAD' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\t\tgit checkout --detach HEAD &&\n+\n+\t\tgit history drop HEAD~ &&\n+\n+\t\texpect_log <<-\\EOF\n+\t\tthird\n+\t\tfirst\n+\t\tEOF\n+\t)\n+'\n+\n+# Note: in this case it would actually be fine to drop the root commit, as we\n+# do have a descendant commit, and no reference points to the root commit\n+# directly. So this is something that we may relax eventually.\n+test_expect_success 'refuses to drop the root commit' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\n+\t\ttest_must_fail git history drop HEAD~ 2>err &&\n+\t\ttest_grep \"cannot drop root commit\" err\n+\t)\n+'\n+\n+# In contrast to the above case, we actually don't want to drop the root commit\n+# here as that would cause us to end up with an empty commit graph.\n+test_expect_success 'refuses to drop the root commit when branch becomes empty' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\n+\t\ttest_must_fail git history drop HEAD 2>err &&\n+\t\ttest_grep \"cannot drop root commit\" err\n+\t)\n+'\n+\n+test_expect_success 'refuses to drop a merge commit' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\tgit branch branch &&\n+\t\ttest_commit ours &&\n+\t\tgit switch branch &&\n+\t\ttest_commit theirs &&\n+\t\tgit switch - &&\n+\t\tgit merge theirs &&\n+\n+\t\ttest_must_fail git history drop HEAD 2>err &&\n+\t\ttest_grep \"cannot drop merge commit\" err\n+\t)\n+'\n+\n+test_expect_success 'refuses when descendants contain a merge commit' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\ttest_commit middle &&\n+\t\tgit branch branch &&\n+\t\ttest_commit ours &&\n+\t\tgit switch branch &&\n+\t\ttest_commit theirs &&\n+\t\tgit switch - &&\n+\t\tgit merge theirs &&\n+\n+\t\ttest_must_fail git history drop middle 2>err &&\n+\t\ttest_grep \"replaying merge commits is not supported yet\" err\n+\t)\n+'\n+\n+test_expect_success 'works in a bare repository' '\n+\ttest_when_finished \"rm -rf repo repo.git\" &&\n+\n+\tgit init repo &&\n+\ttest_commit -C repo first &&\n+\ttest_commit -C repo second &&\n+\ttest_commit -C repo third &&\n+\n+\tgit clone --bare repo repo.git &&\n+\t(\n+\t\tcd repo.git &&\n+\n+\t\tgit history drop HEAD~ &&\n+\t\texpect_log <<-\\EOF\n+\t\tthird\n+\t\tfirst\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'updates branches on other lines of descent' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\ttest_commit target &&\n+\t\tgit branch theirs &&\n+\t\ttest_commit ours &&\n+\t\tgit switch theirs &&\n+\t\ttest_commit theirs &&\n+\n+\t\texpect_graph --branches <<-\\EOF &&\n+\t\t* theirs\n+\t\t| * ours\n+\t\t|/\n+\t\t* target\n+\t\t* base\n+\t\tEOF\n+\n+\t\tgit history drop target &&\n+\n+\t\texpect_graph --branches <<-\\EOF\n+\t\t* ours\n+\t\t| * theirs\n+\t\t|/\n+\t\t* base\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'moves branch pointing at dropped commit to its parent' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo --initial-branch=main &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\tgit branch points-at-second &&\n+\t\ttest_commit third &&\n+\n+\t\tgit rev-parse first >expect &&\n+\t\tgit history drop second &&\n+\t\tgit rev-parse points-at-second >actual &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\texpect_log --format=\"%s %D\" --branches <<-\\EOF\n+\t\tthird HEAD -> main\n+\t\tfirst tag: first, points-at-second\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--dry-run prints ref updates without modifying repo' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo --initial-branch=main &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\tgit branch branch &&\n+\t\ttest_commit middle &&\n+\t\ttest_commit ours &&\n+\t\tgit switch branch &&\n+\t\ttest_commit theirs &&\n+\n+\t\tgit refs list >refs-expect &&\n+\t\tgit history drop --dry-run main~ >updates &&\n+\t\tgit refs list >refs-actual &&\n+\t\ttest_cmp refs-expect refs-actual &&\n+\t\ttest_grep \"update refs/heads/main\" updates &&\n+\n+\t\tgit update-ref --stdin <updates &&\n+\t\texpect_log main <<-\\EOF\n+\t\tours\n+\t\tbase\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--dry-run detects conflicts with modified working tree' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo --initial-branch=main &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second modify-me &&\n+\t\techo modified >modify-me &&\n+\n+\t\tgit refs list >refs-expect &&\n+\t\tgit diff >diff-expect &&\n+\t\ttest_must_fail git history drop --dry-run HEAD 2>err &&\n+\t\ttest_grep \"dropping this commit would overwrite local changes\" err &&\n+\t\tgit diff >diff-actual &&\n+\t\tgit refs list >refs-actual &&\n+\n+\t\ttest_cmp diff-expect diff-actual &&\n+\t\ttest_cmp refs-expect refs-actual\n+\t)\n+'\n+\n+test_expect_success '--update-refs=head updates only HEAD' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo --initial-branch=main &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\ttest_commit target &&\n+\t\tgit branch theirs &&\n+\t\ttest_commit ours &&\n+\t\tgit switch theirs &&\n+\t\ttest_commit theirs &&\n+\n+\t\t# When told to update HEAD only, the command refuses to\n+\t\t# rewrite commits that are not an ancestor of HEAD.\n+\t\ttest_must_fail git history drop --update-refs=head main 2>err &&\n+\t\ttest_grep \"rewritten commit must be an ancestor of HEAD\" err &&\n+\n+\t\texpect_graph --branches <<-\\EOF &&\n+\t\t* theirs\n+\t\t| * ours\n+\t\t|/\n+\t\t* target\n+\t\t* base\n+\t\tEOF\n+\n+\t\tgit switch main &&\n+\t\tgit history drop --update-refs=head target &&\n+\n+\t\texpect_graph --branches <<-\\EOF\n+\t\t* ours\n+\t\t| * theirs\n+\t\t| * target\n+\t\t|/\n+\t\t* base\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'conflict with replayed commit aborts cleanly' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\ttest_commit conflict-a file &&\n+\t\ttest_commit conflict-b file &&\n+\n+\t\tgit refs list >refs-expect &&\n+\t\ttest_must_fail git history drop HEAD~ 2>err &&\n+\t\ttest_grep \"failed replaying descendants\" err &&\n+\t\tgit refs list >refs-actual &&\n+\t\ttest_cmp refs-expect refs-actual\n+\t)\n+'\n+\n+# Build a history where a descendant of the drop target reverts the change\n+# introduced by the drop target. After dropping, the descendant's diff applies\n+# against a tree that already lacks the change, so it becomes empty.\n+setup_empty_descendant_repo () {\n+\tgit init \"$1\" &&\n+\t(\n+\t\tcd \"$1\" &&\n+\t\techo C1 >file &&\n+\t\tgit add file &&\n+\t\tgit commit -m \"base\" &&\n+\t\tgit tag base &&\n+\t\techo C2 >file &&\n+\t\tgit add file &&\n+\t\tgit commit -m \"drop-me\" &&\n+\t\tgit tag drop-me &&\n+\t\ttest_commit middle &&\n+\t\techo C1 >file &&\n+\t\tgit add file &&\n+\t\tgit commit -m \"revert-drop-me\" &&\n+\t\tgit tag revert-drop-me\n+\t)\n+}\n+\n+test_expect_success '--empty=drop drops descendants that become empty' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tsetup_empty_descendant_repo repo &&\n+\t(\n+\t\tcd repo &&\n+\n+\t\tgit history drop --empty=drop drop-me &&\n+\n+\t\texpect_log <<-\\EOF\n+\t\tmiddle\n+\t\tbase\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--empty=keep keeps descendants that become empty' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tsetup_empty_descendant_repo repo &&\n+\t(\n+\t\tcd repo &&\n+\n+\t\tgit history drop --empty=keep drop-me &&\n+\n+\t\texpect_log <<-\\EOF &&\n+\t\trevert-drop-me\n+\t\tmiddle\n+\t\tbase\n+\t\tEOF\n+\t\tgit diff HEAD~ HEAD >diff &&\n+\t\ttest_must_be_empty diff\n+\t)\n+'\n+\n+test_expect_success '--empty=abort errors out when a descendant becomes empty' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tsetup_empty_descendant_repo repo &&\n+\t(\n+\t\tcd repo &&\n+\n+\t\ttest_must_fail git history drop --empty=abort drop-me 2>err &&\n+\t\ttest_grep \"became empty after replay\" err\n+\t)\n+'\n+\n+test_expect_success 'updates index and worktree when HEAD moves' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\n+\t\tgit history drop second &&\n+\n+\t\t# Worktree should no longer contain second.t.\n+\t\ttest_path_is_missing second.t &&\n+\t\ttest_path_is_file first.t &&\n+\t\ttest_path_is_file third.t &&\n+\n+\t\t# Index and worktree should both match the new HEAD.\n+\t\tgit status --porcelain --untracked-files=no >status &&\n+\t\ttest_must_be_empty status\n+\t)\n+'\n+\n+test_expect_success 'updates worktree when dropping HEAD itself' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\n+\t\tgit history drop HEAD &&\n+\n+\t\ttest_path_is_missing second.t &&\n+\t\ttest_path_is_file first.t &&\n+\n+\t\tgit status --porcelain --untracked-files=no >status &&\n+\t\ttest_must_be_empty status\n+\t)\n+'\n+\n+test_expect_success 'preserves unrelated unstaged modifications' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\techo first-content >unrelated.txt &&\n+\t\tgit add unrelated.txt &&\n+\t\tgit commit -m \"add unrelated\" &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\n+\t\techo locally-modified >unrelated.txt &&\n+\n+\t\tgit diff >diff-expect &&\n+\t\tgit history drop second &&\n+\t\tgit diff >diff-actual &&\n+\t\ttest_cmp diff-expect diff-actual &&\n+\t\ttest_path_is_missing second.t\n+\t)\n+'\n+\n+test_expect_success 'preserves unrelated staged changes' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\techo first-content >unrelated.txt &&\n+\t\tgit add unrelated.txt &&\n+\t\tgit commit -m \"add unrelated\" &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\n+\t\techo staged-change >unrelated.txt &&\n+\t\tgit add unrelated.txt &&\n+\n+\t\tgit diff --cached >diff-expect &&\n+\t\tgit history drop second &&\n+\t\tgit diff --cached >diff-actual &&\n+\t\ttest_cmp diff-expect diff-actual &&\n+\t\ttest_path_is_missing second.t\n+\t)\n+'\n+\n+test_expect_success 'aborts when local modifications would be overwritten' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\ttest_commit conflict &&\n+\n+\t\techo local-edit >conflict.t &&\n+\t\tgit diff >diff-expect &&\n+\t\ttest_must_fail git history drop HEAD 2>err &&\n+\t\ttest_grep \"would overwrite local changes\" err &&\n+\t\tgit diff >diff-actual &&\n+\t\ttest_cmp diff-expect diff-actual\n+\t)\n+'\n+\n+test_done\n\n-- \n2.55.0.rc0.738.g0c8ab3ebcc.dirty\n\n"},{"id":"546417","messageId":"CAP8UFD1evTZqj1ymW9g5g2RmMkYMaE0rPa0Hzt+irH94M6LD6A@mail.gmail.com","threadId":"65726","inReplyTo":"20260615-b4-pks-history-drop-v6-9-2e329e536d78@pks.im","subject":"Re: [PATCH v6 09/10] builtin/history: split handling of ref updates into two phases","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2026-06-25T13:37:42Z","receivedAt":"2026-06-25T13:37:55Z","isPatch":true,"body":"On Mon, Jun 15, 2026 at 3:56 PM Patrick Steinhardt <ps@pks.im> wrote:\n\n> @@ -414,14 +389,43 @@ static int handle_reference_updates(struct rev_info *revs,\n>                     !detached_head)\n>                         continue;\n>\n> +               ALLOC_GROW(result->updates, result->updates_nr + 1, result->updates_alloc);\n> +               result->updates[result->updates_nr].refname = xstrdup(decoration->name);\n> +               result->updates[result->updates_nr].old_oid = original->object.oid;\n> +               result->updates[result->updates_nr].new_oid = rewritten->object.oid;\n> +               result->updates_nr++;\n\nIt looks like this duplicates what replay_result_queue_update() from\nreplay.c does.\n\n> +       }\n"},{"id":"546418","messageId":"CAP8UFD3jsepRaiHDen_CzWcse-atvBfCdzAQovk+1csaQeDxmQ@mail.gmail.com","threadId":"65726","inReplyTo":"20260615-b4-pks-history-drop-v6-10-2e329e536d78@pks.im","subject":"Re: [PATCH v6 10/10] builtin/history: implement \"drop\" subcommand","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2026-06-25T13:51:56Z","receivedAt":"2026-06-25T13:52:08Z","isPatch":true,"body":"On Mon, Jun 15, 2026 at 3:55 PM Patrick Steinhardt <ps@pks.im> wrote:\n\n> +       /*\n> +        * If HEAD will move as a result of the rewrite then we'll have to\n> +        * merge in the changes into the worktree and index. This merge can of\n> +        * course conflict, which will cause the whole operation to abort.\n> +        *\n> +        * If we had already updated the refs at that point then we'd have an\n> +        * inconsistent repository state. So we first perform a dry-run merge\n> +        * here before updating refs.\n> +        */\n> +       if (!is_bare_repository()) {\n\nWhen your ps/setup-drop-global-state series is merged, this will look like:\n\n      if (!is_bare_repository(repo)) {\n\nwhich is nicer.\n\nSo except for perhaps the replay_result_queue_update() duplication,\nthe series looks great to me.\n\nThanks.\n"},{"id":"546434","messageId":"xmqqo6gye44d.fsf@gitster.g","threadId":"65726","inReplyTo":"CAP8UFD3jsepRaiHDen_CzWcse-atvBfCdzAQovk+1csaQeDxmQ@mail.gmail.com","subject":"Re: [PATCH v6 10/10] builtin/history: implement \"drop\" subcommand","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-06-25T20:50:42Z","receivedAt":"2026-06-25T20:50:45Z","isPatch":true,"body":"Christian Couder <christian.couder@gmail.com> writes:\n\n> On Mon, Jun 15, 2026 at 3:55 PM Patrick Steinhardt <ps@pks.im> wrote:\n>\n>> +       /*\n>> +        * If HEAD will move as a result of the rewrite then we'll have to\n>> +        * merge in the changes into the worktree and index. This merge can of\n>> +        * course conflict, which will cause the whole operation to abort.\n>> +        *\n>> +        * If we had already updated the refs at that point then we'd have an\n>> +        * inconsistent repository state. So we first perform a dry-run merge\n>> +        * here before updating refs.\n>> +        */\n>> +       if (!is_bare_repository()) {\n>\n> When your ps/setup-drop-global-state series is merged, this will look like:\n>\n>       if (!is_bare_repository(repo)) {\n>\n> which is nicer.\n>\n> So except for perhaps the replay_result_queue_update() duplication,\n> the series looks great to me.\n\nThanks.  Let me mark it as \"Waiting for response(s) to review\ncomment(s).\" then.\n"},{"id":"546633","messageId":"akIf1N2HuAGJEAnx@pks.im","threadId":"65726","inReplyTo":"CAP8UFD1evTZqj1ymW9g5g2RmMkYMaE0rPa0Hzt+irH94M6LD6A@mail.gmail.com","subject":"Re: [PATCH v6 09/10] builtin/history: split handling of ref updates into two phases","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-29T07:33:40Z","receivedAt":"2026-06-29T07:33:46Z","isPatch":true,"body":"On Thu, Jun 25, 2026 at 03:37:42PM +0200, Christian Couder wrote:\n> On Mon, Jun 15, 2026 at 3:56 PM Patrick Steinhardt <ps@pks.im> wrote:\n> \n> > @@ -414,14 +389,43 @@ static int handle_reference_updates(struct rev_info *revs,\n> >                     !detached_head)\n> >                         continue;\n> >\n> > +               ALLOC_GROW(result->updates, result->updates_nr + 1, result->updates_alloc);\n> > +               result->updates[result->updates_nr].refname = xstrdup(decoration->name);\n> > +               result->updates[result->updates_nr].old_oid = original->object.oid;\n> > +               result->updates[result->updates_nr].new_oid = rewritten->object.oid;\n> > +               result->updates_nr++;\n> \n> It looks like this duplicates what replay_result_queue_update() from\n> replay.c does.\n\nIt indeed is. That function is internal to \"replay.c\" though. We could\nexpose it, but I wonder whether that's worth it. Goes looking... you\nknow, let me just do it.\n\nThanks!\n\nPatrick\n"},{"id":"546634","messageId":"20260629-b4-pks-history-drop-v7-0-6e9392a957d8@pks.im","threadId":"65726","inReplyTo":"20260601-b4-pks-history-drop-v1-0-643e32340d55@pks.im","subject":"[PATCH v7 00/11] builtin/history: introduce \"drop\" subcommand","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-29T07:34:26Z","receivedAt":"2026-06-29T07:34:35Z","isPatch":true,"body":"Hi,\n\nthis small patch series introduces the new \"drop\" subcommand for\ngit-history(1). As a reader might guess, the command does exactly that:\ngiven a commit, it will drop that commit from the commit history and\nreplay descendant branches on top of it.\n\nChanges in v7:\n  - Expose `replay_result_queue_update()` so that we don't have to\n    duplicate its functionality.\n  - Add missing SOB.\n  - Link to v6: https://patch.msgid.link/20260615-b4-pks-history-drop-v6-0-2e329e536d78@pks.im\n\nChanges in v6:\n  - Fix bad interactions of DRY_RUN with UPDATE_HEAD\n  - Link to v5: https://patch.msgid.link/20260611-b4-pks-history-drop-v5-0-34d35725559c@pks.im\n\nChanges in v5:\n  - Reject UPDATE_ORIG_HEAD without UPDATE_HEAD.\n  - Link to v4: https://patch.msgid.link/20260610-b4-pks-history-drop-v4-0-70d5f0ae8c25@pks.im\n\nChanges in v4:\n  - Remove the `SKIP_REF_UPDATES` flag in favor of a new `UPDATE_HEAD`\n    flag, as suggested by Phillip.\n  - Rename `reset_head()` to `reset_working_tree()`. This better matches\n    the new scope of the function, and it helps us to catch any\n    in-flight patches that would now have to set the `UPDATE_HEAD` flag.\n  - Link to v3: https://patch.msgid.link/20260608-b4-pks-history-drop-v3-0-84ca8e43e937@pks.im\n\nChanges in v3:\n  - Fix commit message typos.\n  - Make `update_orig_head` and `skip_ref_updates` mutually exclusive.\n  - Use fancy revisions to specify the commit to drop in the example\n    section.\n  - Detect conflicting changes in the index/working tree in dry-run\n    mode.\n  - Consistently use a subshell.\n  - Rename `RESET_HEAD_ORIG_HEAD` to `RESET_HEAD_UPDATE_ORIG_HEAD`.\n  - \n  - Link to v2: https://patch.msgid.link/20260603-b4-pks-history-drop-v2-0-742cb5b5176d@pks.im\n\nChanges in v2:\n  - Reworked `update_worktree()` to use `reset_head()`, which required a\n    bunch of changes to `reset_head()`.\n  - Consistently mention the commit that cannot be dropped as part of\n    error messages.\n  - Adapt error message to not use backticks anymore.\n  - Drop redundant \"--graph\" flag in a test helper.\n  - Link to v1: https://patch.msgid.link/20260601-b4-pks-history-drop-v1-0-643e32340d55@pks.im\n\nThanks!\n\nPatrick\n\n---\nPatrick Steinhardt (11):\n      read-cache: split out function to drop unmerged entries to stage 0\n      reset: drop `USE_THE_REPOSITORY_VARIABLE`\n      reset: rename `reset_head()`\n      reset: modernize flags passed to `reset_working_tree()`\n      reset: introduce dry-run mode\n      reset: introduce ability to skip updating HEAD\n      reset: allow the caller to specify the current HEAD object\n      reset: stop assuming that the caller passes in a clean index\n      replay: expose `replay_result_queue_update()`\n      builtin/history: split handling of ref updates into two phases\n      builtin/history: implement \"drop\" subcommand\n\n Documentation/git-history.adoc |  38 ++-\n builtin/history.c              | 286 +++++++++++++++++++---\n builtin/rebase.c               |  41 ++--\n read-cache-ll.h                |   1 +\n read-cache.c                   |  12 +-\n replay.c                       |   8 +-\n replay.h                       |   5 +\n reset.c                        | 102 +++++---\n reset.h                        |  51 ++--\n sequencer.c                    |  17 +-\n t/meson.build                  |   1 +\n t/t3454-history-drop.sh        | 537 +++++++++++++++++++++++++++++++++++++++++\n 12 files changed, 978 insertions(+), 121 deletions(-)\n\nRange-diff versus v6:\n\n 1:  61c6eb1bdc =  1:  640b51b963 read-cache: split out function to drop unmerged entries to stage 0\n 2:  50a61da426 =  2:  485dee2858 reset: drop `USE_THE_REPOSITORY_VARIABLE`\n 3:  96ffa9d2a6 !  3:  fd82a0b592 reset: rename `reset_head()`\n    @@ Commit message\n         subsequent commit.\n     \n         Suggested-by: Phillip Wood <phillip.wood123@gmail.com>\n    +    Signed-off-by: Patrick Steinhardt <ps@pks.im>\n     \n      ## builtin/rebase.c ##\n     @@ builtin/rebase.c: static int finish_rebase(struct rebase_options *opts)\n 4:  874c3ecd59 =  4:  7fe6db0459 reset: modernize flags passed to `reset_working_tree()`\n 5:  bba2845f2d =  5:  b0fba42b75 reset: introduce dry-run mode\n 6:  ada93af1da =  6:  db635dbea4 reset: introduce ability to skip updating HEAD\n 7:  3cf1dcf549 =  7:  5422a99683 reset: allow the caller to specify the current HEAD object\n 8:  ff28ad814c =  8:  a264b72376 reset: stop assuming that the caller passes in a clean index\n -:  ---------- >  9:  8060e462d1 replay: expose `replay_result_queue_update()`\n 9:  7b048d5a16 ! 10:  7233b48732 builtin/history: split handling of ref updates into two phases\n    @@ builtin/history.c: static int handle_reference_updates(struct rev_info *revs,\n      \t\t    !detached_head)\n      \t\t\tcontinue;\n      \n    -+\t\tALLOC_GROW(result->updates, result->updates_nr + 1, result->updates_alloc);\n    -+\t\tresult->updates[result->updates_nr].refname = xstrdup(decoration->name);\n    -+\t\tresult->updates[result->updates_nr].old_oid = original->object.oid;\n    -+\t\tresult->updates[result->updates_nr].new_oid = rewritten->object.oid;\n    -+\t\tresult->updates_nr++;\n    ++\t\treplay_result_queue_update(result, decoration->name,\n    ++\t\t\t\t\t   &original->object.oid,\n    ++\t\t\t\t\t   &rewritten->object.oid);\n     +\t}\n     +\n     +\treturn 0;\n10:  7389e0432a = 11:  61668ea59a builtin/history: implement \"drop\" subcommand\n\n---\nbase-commit: 1666c1265231b0bc5f613fbbf3f0a9896cdef76e\nchange-id: 20260601-b4-pks-history-drop-28f6c6399e7b\n\n"},{"id":"546635","messageId":"20260629-b4-pks-history-drop-v7-1-6e9392a957d8@pks.im","threadId":"65726","inReplyTo":"20260629-b4-pks-history-drop-v7-0-6e9392a957d8@pks.im","subject":"[PATCH v7 01/11] read-cache: split out function to drop unmerged entries to stage 0","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-29T07:34:27Z","receivedAt":"2026-06-29T07:34:37Z","isPatch":true,"body":"In `repo_read_index_unmerged()` we read the index and then drop any\nunmerged entries to stage 0. In a subsequent commit we'll want to\nperform this operation on arbitrary indexes, not only the one of the\ngiven repository.\n\nPrepare for this by splitting out the functionality into a new function\nthat can act on an arbitrary index.\n\nWhile at it, fix a signedness mismatch when iterating through the index\ncache entries.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n read-cache-ll.h |  1 +\n read-cache.c    | 12 +++++++-----\n 2 files changed, 8 insertions(+), 5 deletions(-)\n\ndiff --git a/read-cache-ll.h b/read-cache-ll.h\nindex 2c8b4b21b1..71b87615eb 100644\n--- a/read-cache-ll.h\n+++ b/read-cache-ll.h\n@@ -309,6 +309,7 @@ int write_locked_index(struct index_state *, struct lock_file *lock, unsigned fl\n void discard_index(struct index_state *);\n void move_index_extensions(struct index_state *dst, struct index_state *src);\n int unmerged_index(const struct index_state *);\n+int index_state_unmerged_to_stage0(struct index_state *istate);\n \n /**\n  * Returns 1 if istate differs from tree, 0 otherwise.  If tree is NULL,\ndiff --git a/read-cache.c b/read-cache.c\nindex 21829102ae..799a5bc719 100644\n--- a/read-cache.c\n+++ b/read-cache.c\n@@ -3403,13 +3403,15 @@ int write_locked_index(struct index_state *istate, struct lock_file *lock,\n  */\n int repo_read_index_unmerged(struct repository *repo)\n {\n-\tstruct index_state *istate;\n-\tint i;\n+\trepo_read_index(repo);\n+\treturn index_state_unmerged_to_stage0(repo->index);\n+}\n+\n+int index_state_unmerged_to_stage0(struct index_state *istate)\n+{\n \tint unmerged = 0;\n \n-\trepo_read_index(repo);\n-\tistate = repo->index;\n-\tfor (i = 0; i < istate->cache_nr; i++) {\n+\tfor (unsigned int i = 0; i < istate->cache_nr; i++) {\n \t\tstruct cache_entry *ce = istate->cache[i];\n \t\tstruct cache_entry *new_ce;\n \t\tint len;\n\n-- \n2.55.0.rc2.803.g1fd1e6609c.dirty\n\n"},{"id":"546637","messageId":"20260629-b4-pks-history-drop-v7-2-6e9392a957d8@pks.im","threadId":"65726","inReplyTo":"20260629-b4-pks-history-drop-v7-0-6e9392a957d8@pks.im","subject":"[PATCH v7 02/11] reset: drop `USE_THE_REPOSITORY_VARIABLE`","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-29T07:34:28Z","receivedAt":"2026-06-29T07:34:39Z","isPatch":true,"body":"In \"reset.c\" we still have references to `the_repository`, even though\nthe only entry point into the file already receives a repository as\nparameter.\n\nUpdate all uses of `the_repository` to instead use the passed-in repo\nand drop `USE_THE_REPOSITORY_VARIABLE`.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reset.c | 27 +++++++++++++--------------\n 1 file changed, 13 insertions(+), 14 deletions(-)\n\ndiff --git a/reset.c b/reset.c\nindex 46e30e6394..3b3cb74dab 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -1,5 +1,3 @@\n-#define USE_THE_REPOSITORY_VARIABLE\n-\n #include \"git-compat-util.h\"\n #include \"cache-tree.h\"\n #include \"gettext.h\"\n@@ -13,7 +11,8 @@\n #include \"unpack-trees.h\"\n #include \"hook.h\"\n \n-static int update_refs(const struct reset_head_opts *opts,\n+static int update_refs(struct repository *repo,\n+\t\t       const struct reset_head_opts *opts,\n \t\t       const struct object_id *oid,\n \t\t       const struct object_id *head)\n {\n@@ -42,19 +41,19 @@ static int update_refs(const struct reset_head_opts *opts,\n \tprefix_len = msg.len;\n \n \tif (update_orig_head) {\n-\t\tif (!repo_get_oid(the_repository, \"ORIG_HEAD\", &oid_old_orig))\n+\t\tif (!repo_get_oid(repo, \"ORIG_HEAD\", &oid_old_orig))\n \t\t\told_orig = &oid_old_orig;\n \t\tif (head) {\n \t\t\tif (!reflog_orig_head) {\n \t\t\t\tstrbuf_addstr(&msg, \"updating ORIG_HEAD\");\n \t\t\t\treflog_orig_head = msg.buf;\n \t\t\t}\n-\t\t\trefs_update_ref(get_main_ref_store(the_repository),\n+\t\t\trefs_update_ref(get_main_ref_store(repo),\n \t\t\t\t\treflog_orig_head, \"ORIG_HEAD\",\n \t\t\t\t\torig_head ? orig_head : head,\n \t\t\t\t\told_orig, 0, UPDATE_REFS_MSG_ON_ERR);\n \t\t} else if (old_orig)\n-\t\t\trefs_delete_ref(get_main_ref_store(the_repository),\n+\t\t\trefs_delete_ref(get_main_ref_store(repo),\n \t\t\t\t\tNULL, \"ORIG_HEAD\", old_orig, 0);\n \t}\n \n@@ -64,23 +63,23 @@ static int update_refs(const struct reset_head_opts *opts,\n \t\treflog_head = msg.buf;\n \t}\n \tif (!switch_to_branch)\n-\t\tret = refs_update_ref(get_main_ref_store(the_repository),\n+\t\tret = refs_update_ref(get_main_ref_store(repo),\n \t\t\t\t      reflog_head, \"HEAD\", oid, head,\n \t\t\t\t      detach_head ? REF_NO_DEREF : 0,\n \t\t\t\t      UPDATE_REFS_MSG_ON_ERR);\n \telse {\n-\t\tret = refs_update_ref(get_main_ref_store(the_repository),\n+\t\tret = refs_update_ref(get_main_ref_store(repo),\n \t\t\t\t      reflog_branch ? reflog_branch : reflog_head,\n \t\t\t\t      switch_to_branch, oid, NULL, 0,\n \t\t\t\t      UPDATE_REFS_MSG_ON_ERR);\n \t\tif (!ret)\n-\t\t\tret = refs_update_symref(get_main_ref_store(the_repository),\n+\t\t\tret = refs_update_symref(get_main_ref_store(repo),\n \t\t\t\t\t\t \"HEAD\", switch_to_branch,\n \t\t\t\t\t\t reflog_head);\n \t}\n \tif (!ret && run_hook)\n-\t\trun_hooks_l(the_repository, \"post-checkout\",\n-\t\t\t    oid_to_hex(head ? head : null_oid(the_hash_algo)),\n+\t\trun_hooks_l(repo, \"post-checkout\",\n+\t\t\t    oid_to_hex(head ? head : null_oid(repo->hash_algo)),\n \t\t\t    oid_to_hex(oid), \"1\", NULL);\n \tstrbuf_release(&msg);\n \treturn ret;\n@@ -126,7 +125,7 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \t\toid = &head_oid;\n \n \tif (refs_only)\n-\t\treturn update_refs(opts, oid, head);\n+\t\treturn update_refs(r, opts, oid, head);\n \n \taction = reset_hard ? \"reset\" : \"checkout\";\n \tsetup_unpack_trees_porcelain(&unpack_tree_opts, action);\n@@ -163,7 +162,7 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \t\tgoto leave_reset_head;\n \t}\n \n-\ttree = repo_parse_tree_indirect(the_repository, oid);\n+\ttree = repo_parse_tree_indirect(r, oid);\n \tif (!tree) {\n \t\tret = error(_(\"unable to read tree (%s)\"), oid_to_hex(oid));\n \t\tgoto leave_reset_head;\n@@ -177,7 +176,7 @@ int reset_head(struct repository *r, const struct reset_head_opts *opts)\n \t}\n \n \tif (oid != &head_oid || update_orig_head || switch_to_branch)\n-\t\tret = update_refs(opts, oid, head);\n+\t\tret = update_refs(r, opts, oid, head);\n \n leave_reset_head:\n \trollback_lock_file(&lock);\n\n-- \n2.55.0.rc2.803.g1fd1e6609c.dirty\n\n"},{"id":"546636","messageId":"20260629-b4-pks-history-drop-v7-3-6e9392a957d8@pks.im","threadId":"65726","inReplyTo":"20260629-b4-pks-history-drop-v7-0-6e9392a957d8@pks.im","subject":"[PATCH v7 03/11] reset: rename `reset_head()`","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-29T07:34:29Z","receivedAt":"2026-06-29T07:34:41Z","isPatch":true,"body":"In a subsequent commit we're about to adapt `reset_head()` so that the\nreference update to HEAD is optional, only. At this point the function\nstarts to feel misnamed, as it doesn't necessarily have anything to do\nwith the HEAD reference anymore. The gist of the function then is that\nwe reset the working tree to a specific new commit, updating both the\nindex and the checked-out files.\n\nRename it to `reset_working_tree()` to better reflect that.\n\nNote that we don't adjust the flags yet. This will happen in a\nsubsequent commit.\n\nSuggested-by: Phillip Wood <phillip.wood123@gmail.com>\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n builtin/rebase.c | 20 ++++++++++----------\n reset.c          |  5 +++--\n reset.h          |  4 ++--\n sequencer.c      |  8 ++++----\n 4 files changed, 19 insertions(+), 18 deletions(-)\n\ndiff --git a/builtin/rebase.c b/builtin/rebase.c\nindex fa4f5d9306..22fbba3c62 100644\n--- a/builtin/rebase.c\n+++ b/builtin/rebase.c\n@@ -592,7 +592,7 @@ static int finish_rebase(struct rebase_options *opts)\n static int move_to_original_branch(struct rebase_options *opts)\n {\n \tstruct strbuf branch_reflog = STRBUF_INIT, head_reflog = STRBUF_INIT;\n-\tstruct reset_head_opts ropts = { 0 };\n+\tstruct reset_working_tree_options ropts = { 0 };\n \tint ret;\n \n \tif (!opts->head_name)\n@@ -610,7 +610,7 @@ static int move_to_original_branch(struct rebase_options *opts)\n \tropts.flags = RESET_HEAD_REFS_ONLY;\n \tropts.branch_msg = branch_reflog.buf;\n \tropts.head_msg = head_reflog.buf;\n-\tret = reset_head(the_repository, &ropts);\n+\tret = reset_working_tree(the_repository, &ropts);\n \n \tstrbuf_release(&branch_reflog);\n \tstrbuf_release(&head_reflog);\n@@ -685,7 +685,7 @@ static int run_am(struct rebase_options *opts)\n \n \tstatus = run_command(&format_patch);\n \tif (status) {\n-\t\tstruct reset_head_opts ropts = { 0 };\n+\t\tstruct reset_working_tree_options ropts = { 0 };\n \t\tunlink(rebased_patches);\n \t\tfree(rebased_patches);\n \t\tchild_process_clear(&am);\n@@ -693,7 +693,7 @@ static int run_am(struct rebase_options *opts)\n \t\tropts.oid = &opts->orig_head->object.oid;\n \t\tropts.branch = opts->head_name;\n \t\tropts.default_reflog_action = opts->reflog_action;\n-\t\treset_head(the_repository, &ropts);\n+\t\treset_working_tree(the_repository, &ropts);\n \t\terror(_(\"\\ngit encountered an error while preparing the \"\n \t\t\t\"patches to replay\\n\"\n \t\t\t\"these revisions:\\n\"\n@@ -855,7 +855,7 @@ static int rebase_config(const char *var, const char *value,\n static int checkout_up_to_date(struct rebase_options *options)\n {\n \tstruct strbuf buf = STRBUF_INIT;\n-\tstruct reset_head_opts ropts = { 0 };\n+\tstruct reset_working_tree_options ropts = { 0 };\n \tint ret = 0;\n \n \tstrbuf_addf(&buf, \"%s: checkout %s\",\n@@ -866,7 +866,7 @@ static int checkout_up_to_date(struct rebase_options *options)\n \tif (!ropts.branch)\n \t\tropts.flags |=  RESET_HEAD_DETACH;\n \tropts.head_msg = buf.buf;\n-\tif (reset_head(the_repository, &ropts) < 0)\n+\tif (reset_working_tree(the_repository, &ropts) < 0)\n \t\tret = error(_(\"could not switch to %s\"), options->switch_to);\n \tstrbuf_release(&buf);\n \n@@ -1116,7 +1116,7 @@ int cmd_rebase(int argc,\n \tint reschedule_failed_exec = -1;\n \tint allow_preemptive_ff = 1;\n \tint preserve_merges_selected = 0;\n-\tstruct reset_head_opts ropts = { 0 };\n+\tstruct reset_working_tree_options ropts = { 0 };\n \tstruct option builtin_rebase_options[] = {\n \t\tOPT_STRING(0, \"onto\", &options.onto_name,\n \t\t\t   N_(\"revision\"),\n@@ -1385,7 +1385,7 @@ int cmd_rebase(int argc,\n \t\trerere_clear(the_repository, &merge_rr);\n \t\tstring_list_clear(&merge_rr, 1);\n \t\tropts.flags = RESET_HEAD_HARD;\n-\t\tif (reset_head(the_repository, &ropts) < 0)\n+\t\tif (reset_working_tree(the_repository, &ropts) < 0)\n \t\t\tdie(_(\"could not discard worktree changes\"));\n \t\tremove_branch_state(the_repository, 0);\n \t\tif (read_basic_state(&options))\n@@ -1410,7 +1410,7 @@ int cmd_rebase(int argc,\n \t\tropts.head_msg = head_msg.buf;\n \t\tropts.branch = options.head_name;\n \t\tropts.flags = RESET_HEAD_HARD;\n-\t\tif (reset_head(the_repository, &ropts) < 0)\n+\t\tif (reset_working_tree(the_repository, &ropts) < 0)\n \t\t\tdie(_(\"could not move back to %s\"),\n \t\t\t    oid_to_hex(&options.orig_head->object.oid));\n \t\tstrbuf_release(&head_msg);\n@@ -1880,7 +1880,7 @@ int cmd_rebase(int argc,\n \t\t\tRESET_HEAD_RUN_POST_CHECKOUT_HOOK;\n \tropts.head_msg = msg.buf;\n \tropts.default_reflog_action = options.reflog_action;\n-\tif (reset_head(the_repository, &ropts)) {\n+\tif (reset_working_tree(the_repository, &ropts)) {\n \t\tret = error(_(\"Could not detach HEAD\"));\n \t\tgoto cleanup_autostash;\n \t}\ndiff --git a/reset.c b/reset.c\nindex 3b3cb74dab..799596398b 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -12,7 +12,7 @@\n #include \"hook.h\"\n \n static int update_refs(struct repository *repo,\n-\t\t       const struct reset_head_opts *opts,\n+\t\t       const struct reset_working_tree_options *opts,\n \t\t       const struct object_id *oid,\n \t\t       const struct object_id *head)\n {\n@@ -85,7 +85,8 @@ static int update_refs(struct repository *repo,\n \treturn ret;\n }\n \n-int reset_head(struct repository *r, const struct reset_head_opts *opts)\n+int reset_working_tree(struct repository *r,\n+\t\t       const struct reset_working_tree_options *opts)\n {\n \tconst struct object_id *oid = opts->oid;\n \tconst char *switch_to_branch = opts->branch;\ndiff --git a/reset.h b/reset.h\nindex a28f81829d..f130152014 100644\n--- a/reset.h\n+++ b/reset.h\n@@ -17,7 +17,7 @@\n /* Update ORIG_HEAD as well as HEAD */\n #define RESET_ORIG_HEAD (1<<4)\n \n-struct reset_head_opts {\n+struct reset_working_tree_options {\n \t/*\n \t * The commit to checkout/reset to. Defaults to HEAD.\n \t */\n@@ -55,6 +55,6 @@ struct reset_head_opts {\n \tconst char *default_reflog_action;\n };\n \n-int reset_head(struct repository *r, const struct reset_head_opts *opts);\n+int reset_working_tree(struct repository *r, const struct reset_working_tree_options *opts);\n \n #endif\ndiff --git a/sequencer.c b/sequencer.c\nindex 1ee4b2875b..d73ecf0384 100644\n--- a/sequencer.c\n+++ b/sequencer.c\n@@ -4677,7 +4677,7 @@ static void create_autostash_internal(struct repository *r,\n \tif (has_unstaged_changes(r, 1) ||\n \t    has_uncommitted_changes(r, 1)) {\n \t\tstruct child_process stash = CHILD_PROCESS_INIT;\n-\t\tstruct reset_head_opts ropts = { .flags = RESET_HEAD_HARD };\n+\t\tstruct reset_working_tree_options ropts = { .flags = RESET_HEAD_HARD };\n \t\tstruct object_id oid;\n \n \t\tstrvec_pushl(&stash.args,\n@@ -4707,7 +4707,7 @@ static void create_autostash_internal(struct repository *r,\n \n \t\tif (!silent)\n \t\t\tprintf(_(\"Created autostash: %s\\n\"), buf.buf);\n-\t\tif (reset_head(r, &ropts) < 0)\n+\t\tif (reset_working_tree(r, &ropts) < 0)\n \t\t\tdie(_(\"could not reset --hard\"));\n \t\tdiscard_index(r->index);\n \t\tif (repo_read_index(r) < 0)\n@@ -4867,7 +4867,7 @@ static int checkout_onto(struct repository *r, struct replay_opts *opts,\n \t\t\t const char *onto_name, const struct object_id *onto,\n \t\t\t const struct object_id *orig_head)\n {\n-\tstruct reset_head_opts ropts = {\n+\tstruct reset_working_tree_options ropts = {\n \t\t.oid = onto,\n \t\t.orig_head = orig_head,\n \t\t.flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD |\n@@ -4876,7 +4876,7 @@ static int checkout_onto(struct repository *r, struct replay_opts *opts,\n \t\t\t\t\t   onto_name),\n \t\t.default_reflog_action = sequencer_reflog_action(opts)\n \t};\n-\tif (reset_head(r, &ropts)) {\n+\tif (reset_working_tree(r, &ropts)) {\n \t\tapply_autostash(rebase_path_autostash());\n \t\tsequencer_remove_state(opts);\n \t\treturn error(_(\"could not detach HEAD\"));\n\n-- \n2.55.0.rc2.803.g1fd1e6609c.dirty\n\n"},{"id":"546638","messageId":"20260629-b4-pks-history-drop-v7-4-6e9392a957d8@pks.im","threadId":"65726","inReplyTo":"20260629-b4-pks-history-drop-v7-0-6e9392a957d8@pks.im","subject":"[PATCH v7 04/11] reset: modernize flags passed to `reset_working_tree()`","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-29T07:34:30Z","receivedAt":"2026-06-29T07:34:44Z","isPatch":true,"body":"The flags passed to `reset_working_tree()` are declared as defines. This\nhas fallen a bit out of practice nowadays, where we instead prefer to\nuse enums. Furthermore, the prefix of those flags does not match the\nfunction name anymore after the rename in the preceding commit.\n\nAdapt the code to follow modern best practices and adapt the flag names.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n builtin/rebase.c | 15 ++++++++-------\n reset.c          | 12 ++++++------\n reset.h          | 31 +++++++++++++++++++------------\n sequencer.c      |  9 ++++++---\n 4 files changed, 39 insertions(+), 28 deletions(-)\n\ndiff --git a/builtin/rebase.c b/builtin/rebase.c\nindex 22fbba3c62..06dcbaf5e8 100644\n--- a/builtin/rebase.c\n+++ b/builtin/rebase.c\n@@ -607,7 +607,7 @@ static int move_to_original_branch(struct rebase_options *opts)\n \tstrbuf_addf(&head_reflog, \"%s (finish): returning to %s\",\n \t\t    opts->reflog_action, opts->head_name);\n \tropts.branch = opts->head_name;\n-\tropts.flags = RESET_HEAD_REFS_ONLY;\n+\tropts.flags = RESET_WORKING_TREE_REFS_ONLY;\n \tropts.branch_msg = branch_reflog.buf;\n \tropts.head_msg = head_reflog.buf;\n \tret = reset_working_tree(the_repository, &ropts);\n@@ -862,9 +862,9 @@ static int checkout_up_to_date(struct rebase_options *options)\n \t\t    options->reflog_action, options->switch_to);\n \tropts.oid = &options->orig_head->object.oid;\n \tropts.branch = options->head_name;\n-\tropts.flags = RESET_HEAD_RUN_POST_CHECKOUT_HOOK;\n+\tropts.flags = RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK;\n \tif (!ropts.branch)\n-\t\tropts.flags |=  RESET_HEAD_DETACH;\n+\t\tropts.flags |=  RESET_WORKING_TREE_DETACH;\n \tropts.head_msg = buf.buf;\n \tif (reset_working_tree(the_repository, &ropts) < 0)\n \t\tret = error(_(\"could not switch to %s\"), options->switch_to);\n@@ -1384,7 +1384,7 @@ int cmd_rebase(int argc,\n \n \t\trerere_clear(the_repository, &merge_rr);\n \t\tstring_list_clear(&merge_rr, 1);\n-\t\tropts.flags = RESET_HEAD_HARD;\n+\t\tropts.flags = RESET_WORKING_TREE_HARD;\n \t\tif (reset_working_tree(the_repository, &ropts) < 0)\n \t\t\tdie(_(\"could not discard worktree changes\"));\n \t\tremove_branch_state(the_repository, 0);\n@@ -1409,7 +1409,7 @@ int cmd_rebase(int argc,\n \t\tropts.oid = &options.orig_head->object.oid;\n \t\tropts.head_msg = head_msg.buf;\n \t\tropts.branch = options.head_name;\n-\t\tropts.flags = RESET_HEAD_HARD;\n+\t\tropts.flags = RESET_WORKING_TREE_HARD;\n \t\tif (reset_working_tree(the_repository, &ropts) < 0)\n \t\t\tdie(_(\"could not move back to %s\"),\n \t\t\t    oid_to_hex(&options.orig_head->object.oid));\n@@ -1876,8 +1876,9 @@ int cmd_rebase(int argc,\n \t\t    options.reflog_action, options.onto_name);\n \tropts.oid = &options.onto->object.oid;\n \tropts.orig_head = &options.orig_head->object.oid;\n-\tropts.flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD |\n-\t\t\tRESET_HEAD_RUN_POST_CHECKOUT_HOOK;\n+\tropts.flags = RESET_WORKING_TREE_DETACH |\n+\t\t      RESET_WORKING_TREE_UPDATE_ORIG_HEAD |\n+\t\t      RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK;\n \tropts.head_msg = msg.buf;\n \tropts.default_reflog_action = options.reflog_action;\n \tif (reset_working_tree(the_repository, &ropts)) {\ndiff --git a/reset.c b/reset.c\nindex 799596398b..4ca7f23a25 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -16,9 +16,9 @@ static int update_refs(struct repository *repo,\n \t\t       const struct object_id *oid,\n \t\t       const struct object_id *head)\n {\n-\tunsigned detach_head = opts->flags & RESET_HEAD_DETACH;\n-\tunsigned run_hook = opts->flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK;\n-\tunsigned update_orig_head = opts->flags & RESET_ORIG_HEAD;\n+\tunsigned detach_head = opts->flags & RESET_WORKING_TREE_DETACH;\n+\tunsigned run_hook = opts->flags & RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK;\n+\tunsigned update_orig_head = opts->flags & RESET_WORKING_TREE_UPDATE_ORIG_HEAD;\n \tconst struct object_id *orig_head = opts->orig_head;\n \tconst char *switch_to_branch = opts->branch;\n \tconst char *reflog_branch = opts->branch_msg;\n@@ -90,9 +90,9 @@ int reset_working_tree(struct repository *r,\n {\n \tconst struct object_id *oid = opts->oid;\n \tconst char *switch_to_branch = opts->branch;\n-\tunsigned reset_hard = opts->flags & RESET_HEAD_HARD;\n-\tunsigned refs_only = opts->flags & RESET_HEAD_REFS_ONLY;\n-\tunsigned update_orig_head = opts->flags & RESET_ORIG_HEAD;\n+\tunsigned reset_hard = opts->flags & RESET_WORKING_TREE_HARD;\n+\tunsigned refs_only = opts->flags & RESET_WORKING_TREE_REFS_ONLY;\n+\tunsigned update_orig_head = opts->flags & RESET_WORKING_TREE_UPDATE_ORIG_HEAD;\n \tstruct object_id *head = NULL, head_oid;\n \tstruct tree_desc desc[2] = { { NULL }, { NULL } };\n \tstruct lock_file lock = LOCK_INIT;\ndiff --git a/reset.h b/reset.h\nindex f130152014..2e5826de99 100644\n--- a/reset.h\n+++ b/reset.h\n@@ -6,16 +6,22 @@\n \n #define GIT_REFLOG_ACTION_ENVIRONMENT \"GIT_REFLOG_ACTION\"\n \n-/* Request a detached checkout */\n-#define RESET_HEAD_DETACH (1<<0)\n-/* Request a reset rather than a checkout */\n-#define RESET_HEAD_HARD (1<<1)\n-/* Run the post-checkout hook */\n-#define RESET_HEAD_RUN_POST_CHECKOUT_HOOK (1<<2)\n-/* Only update refs, do not touch the worktree */\n-#define RESET_HEAD_REFS_ONLY (1<<3)\n-/* Update ORIG_HEAD as well as HEAD */\n-#define RESET_ORIG_HEAD (1<<4)\n+enum reset_working_tree_flags {\n+\t/* Request a detached checkout */\n+\tRESET_WORKING_TREE_DETACH = (1 << 0),\n+\n+\t/* Request a reset rather than a checkout */\n+\tRESET_WORKING_TREE_HARD = (1 << 1),\n+\n+\t/* Run the post-checkout hook */\n+\tRESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK = (1 << 2),\n+\n+\t/* Only update refs, do not touch the worktree */\n+\tRESET_WORKING_TREE_REFS_ONLY = (1 << 3),\n+\n+\t/* Update ORIG_HEAD as well as HEAD */\n+\tRESET_WORKING_TREE_UPDATE_ORIG_HEAD = (1 << 4),\n+};\n \n struct reset_working_tree_options {\n \t/*\n@@ -33,7 +39,7 @@ struct reset_working_tree_options {\n \t/*\n \t * Flags defined above.\n \t */\n-\tunsigned flags;\n+\tenum reset_working_tree_flags flags;\n \t/*\n \t * Optional reflog message for branch, defaults to head_msg.\n \t */\n@@ -45,7 +51,8 @@ struct reset_working_tree_options {\n \tconst char *head_msg;\n \t/*\n \t * Optional reflog message for ORIG_HEAD, if this omitted and flags\n-\t * contains RESET_ORIG_HEAD then default_reflog_action must be given.\n+\t * contains RESET_WORKING_TREE_UPDATE_ORIG_HEAD then\n+\t * default_reflog_action must be given.\n \t */\n \tconst char *orig_head_msg;\n \t/*\ndiff --git a/sequencer.c b/sequencer.c\nindex d73ecf0384..4efe831178 100644\n--- a/sequencer.c\n+++ b/sequencer.c\n@@ -4677,7 +4677,9 @@ static void create_autostash_internal(struct repository *r,\n \tif (has_unstaged_changes(r, 1) ||\n \t    has_uncommitted_changes(r, 1)) {\n \t\tstruct child_process stash = CHILD_PROCESS_INIT;\n-\t\tstruct reset_working_tree_options ropts = { .flags = RESET_HEAD_HARD };\n+\t\tstruct reset_working_tree_options ropts = {\n+\t\t\t.flags = RESET_WORKING_TREE_HARD,\n+\t\t};\n \t\tstruct object_id oid;\n \n \t\tstrvec_pushl(&stash.args,\n@@ -4870,8 +4872,9 @@ static int checkout_onto(struct repository *r, struct replay_opts *opts,\n \tstruct reset_working_tree_options ropts = {\n \t\t.oid = onto,\n \t\t.orig_head = orig_head,\n-\t\t.flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD |\n-\t\t\t\tRESET_HEAD_RUN_POST_CHECKOUT_HOOK,\n+\t\t.flags = RESET_WORKING_TREE_DETACH |\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.default_reflog_action = sequencer_reflog_action(opts)\n\n-- \n2.55.0.rc2.803.g1fd1e6609c.dirty\n\n"},{"id":"546639","messageId":"20260629-b4-pks-history-drop-v7-5-6e9392a957d8@pks.im","threadId":"65726","inReplyTo":"20260629-b4-pks-history-drop-v7-0-6e9392a957d8@pks.im","subject":"[PATCH v7 05/11] reset: introduce dry-run mode","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-29T07:34:31Z","receivedAt":"2026-06-29T07:34:47Z","isPatch":true,"body":"In a subsequent commit we'll add another caller to `reset_working_tree()`\nthat wants to perform a dry-run check of whether it would be possible to\nupdate the index and working tree when moving to a new commit. Introduce\na new flag that lets the caller perform this operation.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reset.c | 44 +++++++++++++++++++++++++++++++++-----------\n reset.h |  6 ++++++\n 2 files changed, 39 insertions(+), 11 deletions(-)\n\ndiff --git a/reset.c b/reset.c\nindex 4ca7f23a25..99f2c1b012 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -93,11 +93,14 @@ int reset_working_tree(struct repository *r,\n \tunsigned reset_hard = opts->flags & RESET_WORKING_TREE_HARD;\n \tunsigned refs_only = opts->flags & RESET_WORKING_TREE_REFS_ONLY;\n \tunsigned update_orig_head = opts->flags & RESET_WORKING_TREE_UPDATE_ORIG_HEAD;\n+\tunsigned dry_run = opts->flags & RESET_WORKING_TREE_DRY_RUN;\n \tstruct object_id *head = NULL, head_oid;\n \tstruct tree_desc desc[2] = { { NULL }, { NULL } };\n \tstruct lock_file lock = LOCK_INIT;\n \tstruct unpack_trees_options unpack_tree_opts = { 0 };\n \tstruct tree *tree;\n+\tstruct index_state scratch_index = INDEX_STATE_INIT(r);\n+\tstruct index_state *istate;\n \tconst char *action;\n \tint ret = 0, nr = 0;\n \n@@ -110,7 +113,7 @@ int reset_working_tree(struct repository *r,\n \tif (opts->branch_msg && !opts->branch)\n \t\tBUG(\"branch reflog message given without a branch\");\n \n-\tif (!refs_only && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {\n+\tif (!refs_only && !dry_run && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {\n \t\tret = -1;\n \t\tgoto leave_reset_head;\n \t}\n@@ -125,16 +128,36 @@ int reset_working_tree(struct repository *r,\n \tif (!oid)\n \t\toid = &head_oid;\n \n-\tif (refs_only)\n-\t\treturn update_refs(r, opts, oid, head);\n+\tif (refs_only) {\n+\t\tif (!dry_run)\n+\t\t\treturn update_refs(r, opts, oid, head);\n+\t\treturn 0;\n+\t}\n+\n+\tif (dry_run) {\n+\t\tif (read_index_from(&scratch_index, r->index_file, r->gitdir) < 0 ||\n+\t\t    index_state_unmerged_to_stage0(&scratch_index) < 0) {\n+\t\t\tret = error(_(\"could not read index\"));\n+\t\t\tgoto leave_reset_head;\n+\t\t}\n+\n+\t\tistate = &scratch_index;\n+\t} else {\n+\t\tif (repo_read_index_unmerged(r) < 0) {\n+\t\t\tret = error(_(\"could not read index\"));\n+\t\t\tgoto leave_reset_head;\n+\t\t}\n+\t\tistate = r->index;\n+\t}\n \n \taction = reset_hard ? \"reset\" : \"checkout\";\n \tsetup_unpack_trees_porcelain(&unpack_tree_opts, action);\n \tunpack_tree_opts.head_idx = 1;\n-\tunpack_tree_opts.src_index = r->index;\n-\tunpack_tree_opts.dst_index = r->index;\n+\tunpack_tree_opts.src_index = istate;\n+\tunpack_tree_opts.dst_index = istate;\n \tunpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge;\n-\tunpack_tree_opts.update = 1;\n+\tunpack_tree_opts.update = !dry_run;\n+\tunpack_tree_opts.dry_run = dry_run;\n \tunpack_tree_opts.merge = 1;\n \tunpack_tree_opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */\n \tunpack_tree_opts.skip_cache_tree_update = 1;\n@@ -142,11 +165,6 @@ int reset_working_tree(struct repository *r,\n \tif (reset_hard)\n \t\tunpack_tree_opts.reset = UNPACK_RESET_PROTECT_UNTRACKED;\n \n-\tif (repo_read_index_unmerged(r) < 0) {\n-\t\tret = error(_(\"could not read index\"));\n-\t\tgoto leave_reset_head;\n-\t}\n-\n \tif (!reset_hard && !fill_tree_descriptor(r, &desc[nr++], &head_oid)) {\n \t\tret = error(_(\"failed to find tree of %s\"),\n \t\t\t    oid_to_hex(&head_oid));\n@@ -163,6 +181,9 @@ int reset_working_tree(struct repository *r,\n \t\tgoto leave_reset_head;\n \t}\n \n+\tif (dry_run)\n+\t\tgoto leave_reset_head;\n+\n \ttree = repo_parse_tree_indirect(r, oid);\n \tif (!tree) {\n \t\tret = error(_(\"unable to read tree (%s)\"), oid_to_hex(oid));\n@@ -182,6 +203,7 @@ int reset_working_tree(struct repository *r,\n leave_reset_head:\n \trollback_lock_file(&lock);\n \tclear_unpack_trees_porcelain(&unpack_tree_opts);\n+\trelease_index(&scratch_index);\n \twhile (nr)\n \t\tfree((void *)desc[--nr].buffer);\n \treturn ret;\ndiff --git a/reset.h b/reset.h\nindex 2e5826de99..898e4a1e95 100644\n--- a/reset.h\n+++ b/reset.h\n@@ -21,6 +21,12 @@ enum reset_working_tree_flags {\n \n \t/* Update ORIG_HEAD as well as HEAD */\n \tRESET_WORKING_TREE_UPDATE_ORIG_HEAD = (1 << 4),\n+\n+\t/*\n+\t * Perform a dry-run by performing the operation without updating\n+\t * any user-visible state.\n+\t */\n+\tRESET_WORKING_TREE_DRY_RUN = (1 << 5),\n };\n \n struct reset_working_tree_options {\n\n-- \n2.55.0.rc2.803.g1fd1e6609c.dirty\n\n"},{"id":"546640","messageId":"20260629-b4-pks-history-drop-v7-6-6e9392a957d8@pks.im","threadId":"65726","inReplyTo":"20260629-b4-pks-history-drop-v7-0-6e9392a957d8@pks.im","subject":"[PATCH v7 06/11] reset: introduce ability to skip updating HEAD","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-29T07:34:32Z","receivedAt":"2026-06-29T07:34:50Z","isPatch":true,"body":"In a subsequent commit we'll introduce a new caller to\n`reset_working_tree()` that really only wants to update the index and\nworking tree, without updating any references. Introduce a new flag that\nmakes the caller opt in to updating HEAD and adapt all callers to set\nthat flag.\n\nNote that in a previous iteration we instead introduced a flag that made\ncallers opt out of updating any references. This was somewhat awkward\nthough because we already have the `UPDATE_ORIG_HEAD` flag, so the\nresult was somewhat inconsistent.\n\nSuggested-by: Phillip Wood <phillip.wood123@gmail.com>\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n builtin/rebase.c | 14 ++++++++++----\n reset.c          |  9 +++++++--\n reset.h          |  9 ++++++---\n sequencer.c      |  4 +++-\n 4 files changed, 26 insertions(+), 10 deletions(-)\n\ndiff --git a/builtin/rebase.c b/builtin/rebase.c\nindex 06dcbaf5e8..10a306310c 100644\n--- a/builtin/rebase.c\n+++ b/builtin/rebase.c\n@@ -607,7 +607,8 @@ static int move_to_original_branch(struct rebase_options *opts)\n \tstrbuf_addf(&head_reflog, \"%s (finish): returning to %s\",\n \t\t    opts->reflog_action, opts->head_name);\n \tropts.branch = opts->head_name;\n-\tropts.flags = RESET_WORKING_TREE_REFS_ONLY;\n+\tropts.flags = RESET_WORKING_TREE_REFS_ONLY |\n+\t\t      RESET_WORKING_TREE_UPDATE_HEAD;\n \tropts.branch_msg = branch_reflog.buf;\n \tropts.head_msg = head_reflog.buf;\n \tret = reset_working_tree(the_repository, &ropts);\n@@ -693,6 +694,7 @@ static int run_am(struct rebase_options *opts)\n \t\tropts.oid = &opts->orig_head->object.oid;\n \t\tropts.branch = opts->head_name;\n \t\tropts.default_reflog_action = opts->reflog_action;\n+\t\tropts.flags = RESET_WORKING_TREE_UPDATE_HEAD;\n \t\treset_working_tree(the_repository, &ropts);\n \t\terror(_(\"\\ngit encountered an error while preparing the \"\n \t\t\t\"patches to replay\\n\"\n@@ -862,7 +864,8 @@ static int checkout_up_to_date(struct rebase_options *options)\n \t\t    options->reflog_action, options->switch_to);\n \tropts.oid = &options->orig_head->object.oid;\n \tropts.branch = options->head_name;\n-\tropts.flags = RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK;\n+\tropts.flags = RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK |\n+\t\t      RESET_WORKING_TREE_UPDATE_HEAD;\n \tif (!ropts.branch)\n \t\tropts.flags |=  RESET_WORKING_TREE_DETACH;\n \tropts.head_msg = buf.buf;\n@@ -1384,7 +1387,8 @@ int cmd_rebase(int argc,\n \n \t\trerere_clear(the_repository, &merge_rr);\n \t\tstring_list_clear(&merge_rr, 1);\n-\t\tropts.flags = RESET_WORKING_TREE_HARD;\n+\t\tropts.flags = RESET_WORKING_TREE_HARD |\n+\t\t\t      RESET_WORKING_TREE_UPDATE_HEAD;\n \t\tif (reset_working_tree(the_repository, &ropts) < 0)\n \t\t\tdie(_(\"could not discard worktree changes\"));\n \t\tremove_branch_state(the_repository, 0);\n@@ -1409,7 +1413,8 @@ int cmd_rebase(int argc,\n \t\tropts.oid = &options.orig_head->object.oid;\n \t\tropts.head_msg = head_msg.buf;\n \t\tropts.branch = options.head_name;\n-\t\tropts.flags = RESET_WORKING_TREE_HARD;\n+\t\tropts.flags = RESET_WORKING_TREE_HARD |\n+\t\t\t      RESET_WORKING_TREE_UPDATE_HEAD;\n \t\tif (reset_working_tree(the_repository, &ropts) < 0)\n \t\t\tdie(_(\"could not move back to %s\"),\n \t\t\t    oid_to_hex(&options.orig_head->object.oid));\n@@ -1877,6 +1882,7 @@ int cmd_rebase(int argc,\n \tropts.oid = &options.onto->object.oid;\n \tropts.orig_head = &options.orig_head->object.oid;\n \tropts.flags = RESET_WORKING_TREE_DETACH |\n+\t\t      RESET_WORKING_TREE_UPDATE_HEAD |\n \t\t      RESET_WORKING_TREE_UPDATE_ORIG_HEAD |\n \t\t      RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK;\n \tropts.head_msg = msg.buf;\ndiff --git a/reset.c b/reset.c\nindex 99f2c1b012..4bde5d8dc6 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -92,6 +92,7 @@ int reset_working_tree(struct repository *r,\n \tconst char *switch_to_branch = opts->branch;\n \tunsigned reset_hard = opts->flags & RESET_WORKING_TREE_HARD;\n \tunsigned refs_only = opts->flags & RESET_WORKING_TREE_REFS_ONLY;\n+\tunsigned update_head = opts->flags & RESET_WORKING_TREE_UPDATE_HEAD;\n \tunsigned update_orig_head = opts->flags & RESET_WORKING_TREE_UPDATE_ORIG_HEAD;\n \tunsigned dry_run = opts->flags & RESET_WORKING_TREE_DRY_RUN;\n \tstruct object_id *head = NULL, head_oid;\n@@ -113,6 +114,9 @@ int reset_working_tree(struct repository *r,\n \tif (opts->branch_msg && !opts->branch)\n \t\tBUG(\"branch reflog message given without a branch\");\n \n+\tif (update_orig_head && !update_head)\n+\t\tBUG(\"cannot update ORIG_HEAD without updating HEAD\" );\n+\n \tif (!refs_only && !dry_run && repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {\n \t\tret = -1;\n \t\tgoto leave_reset_head;\n@@ -129,7 +133,7 @@ int reset_working_tree(struct repository *r,\n \t\toid = &head_oid;\n \n \tif (refs_only) {\n-\t\tif (!dry_run)\n+\t\tif (!dry_run && update_head)\n \t\t\treturn update_refs(r, opts, oid, head);\n \t\treturn 0;\n \t}\n@@ -197,7 +201,8 @@ int reset_working_tree(struct repository *r,\n \t\tgoto leave_reset_head;\n \t}\n \n-\tif (oid != &head_oid || update_orig_head || switch_to_branch)\n+\tif (update_head &&\n+\t    (oid != &head_oid || update_orig_head || switch_to_branch))\n \t\tret = update_refs(r, opts, oid, head);\n \n leave_reset_head:\ndiff --git a/reset.h b/reset.h\nindex 898e4a1e95..38b2891b53 100644\n--- a/reset.h\n+++ b/reset.h\n@@ -19,14 +19,17 @@ enum reset_working_tree_flags {\n \t/* Only update refs, do not touch the worktree */\n \tRESET_WORKING_TREE_REFS_ONLY = (1 << 3),\n \n-\t/* Update ORIG_HEAD as well as HEAD */\n-\tRESET_WORKING_TREE_UPDATE_ORIG_HEAD = (1 << 4),\n+\t/* Update HEAD */\n+\tRESET_WORKING_TREE_UPDATE_HEAD = (1 << 4),\n+\n+\t/* Update ORIG_HEAD */\n+\tRESET_WORKING_TREE_UPDATE_ORIG_HEAD = (1 << 5),\n \n \t/*\n \t * Perform a dry-run by performing the operation without updating\n \t * any user-visible state.\n \t */\n-\tRESET_WORKING_TREE_DRY_RUN = (1 << 5),\n+\tRESET_WORKING_TREE_DRY_RUN = (1 << 6),\n };\n \n struct reset_working_tree_options {\ndiff --git a/sequencer.c b/sequencer.c\nindex 4efe831178..e905b1b2d9 100644\n--- a/sequencer.c\n+++ b/sequencer.c\n@@ -4678,7 +4678,8 @@ static void create_autostash_internal(struct repository *r,\n \t    has_uncommitted_changes(r, 1)) {\n \t\tstruct child_process stash = CHILD_PROCESS_INIT;\n \t\tstruct reset_working_tree_options ropts = {\n-\t\t\t.flags = RESET_WORKING_TREE_HARD,\n+\t\t\t.flags = RESET_WORKING_TREE_HARD |\n+\t\t\t\t RESET_WORKING_TREE_UPDATE_HEAD,\n \t\t};\n \t\tstruct object_id oid;\n \n@@ -4873,6 +4874,7 @@ static int checkout_onto(struct repository *r, struct replay_opts *opts,\n \t\t.oid = onto,\n \t\t.orig_head = orig_head,\n \t\t.flags = RESET_WORKING_TREE_DETACH |\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\n-- \n2.55.0.rc2.803.g1fd1e6609c.dirty\n\n"},{"id":"546641","messageId":"20260629-b4-pks-history-drop-v7-7-6e9392a957d8@pks.im","threadId":"65726","inReplyTo":"20260629-b4-pks-history-drop-v7-0-6e9392a957d8@pks.im","subject":"[PATCH v7 07/11] reset: allow the caller to specify the current HEAD object","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-29T07:34:33Z","receivedAt":"2026-06-29T07:34:53Z","isPatch":true,"body":"When calling `reset_working_tree()` we automatically derive the commit\nthat the callers wants to move from by reading the HEAD commit. Some\ncallers may already have resolved it, or they may want to move from a\ndifferent commit that doesn't match HEAD.\n\nIntroduce a new `oid_from` option that lets the caller specify the\ncommit.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reset.c | 5 ++++-\n reset.h | 5 +++++\n 2 files changed, 9 insertions(+), 1 deletion(-)\n\ndiff --git a/reset.c b/reset.c\nindex 4bde5d8dc6..06f375f296 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -122,7 +122,10 @@ int reset_working_tree(struct repository *r,\n \t\tgoto leave_reset_head;\n \t}\n \n-\tif (!repo_get_oid(r, \"HEAD\", &head_oid)) {\n+\tif (opts->oid_from) {\n+\t\toidcpy(&head_oid, opts->oid_from);\n+\t\thead = &head_oid;\n+\t} else if (!repo_get_oid(r, \"HEAD\", &head_oid)) {\n \t\thead = &head_oid;\n \t} else if (!oid || !reset_hard) {\n \t\tret = error(_(\"could not determine HEAD revision\"));\ndiff --git a/reset.h b/reset.h\nindex 38b2891b53..4c992ba671 100644\n--- a/reset.h\n+++ b/reset.h\n@@ -37,6 +37,11 @@ struct reset_working_tree_options {\n \t * The commit to checkout/reset to. Defaults to HEAD.\n \t */\n \tconst struct object_id *oid;\n+\t/*\n+\t * The commit to checkout/reset from when doing a two-way merge. This\n+\t * is used as one of the sides to merge.\n+\t */\n+\tconst struct object_id *oid_from;\n \t/*\n \t * Optional value to set ORIG_HEAD. Defaults to HEAD.\n \t */\n\n-- \n2.55.0.rc2.803.g1fd1e6609c.dirty\n\n"},{"id":"546643","messageId":"20260629-b4-pks-history-drop-v7-8-6e9392a957d8@pks.im","threadId":"65726","inReplyTo":"20260629-b4-pks-history-drop-v7-0-6e9392a957d8@pks.im","subject":"[PATCH v7 08/11] reset: stop assuming that the caller passes in a clean index","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-29T07:34:34Z","receivedAt":"2026-06-29T07:34:54Z","isPatch":true,"body":"In 652bd0211d (rebase: use 'skip_cache_tree_update' option, 2022-11-10),\nwe updated `reset_working_tree()` to stop updating the index tree cache.\nThis was done as a performance optimization: the function is only called\nby \"sequencer.c\" and \"rebase.c\", both of which assume a clean index\nbefore they perform their operation, so we know that the end result will\nbe a clean index, too. Consequently, we can skip recomputing the cache\nas we can instead use `prime_cache_tree()` directly.\n\nIn a subsequent commit we're about to add a new caller though where the\nassumption doesn't hold anymore: the index may be dirty before calling\n`reset_working_tree()`, and consequently we cannot prime the cache with\na given tree anymore as the index and tree will mismatch.\n\nAdapt the logic so that we only skip the cache tree update in case we're\ndoing a hard reset. While we could introduce logic that only skips the\nupdate in case the incoming index was dirty already, that doesn't really\nfeel worth it: after all, the mentioned commit says itself that the\nperformance improvement was negligible anyway.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n reset.c | 8 +++++---\n 1 file changed, 5 insertions(+), 3 deletions(-)\n\ndiff --git a/reset.c b/reset.c\nindex 06f375f296..ff87e3e357 100644\n--- a/reset.c\n+++ b/reset.c\n@@ -167,10 +167,11 @@ int reset_working_tree(struct repository *r,\n \tunpack_tree_opts.dry_run = dry_run;\n \tunpack_tree_opts.merge = 1;\n \tunpack_tree_opts.preserve_ignored = 0; /* FIXME: !overwrite_ignore */\n-\tunpack_tree_opts.skip_cache_tree_update = 1;\n \tinit_checkout_metadata(&unpack_tree_opts.meta, switch_to_branch, oid, NULL);\n-\tif (reset_hard)\n+\tif (reset_hard) {\n+\t\tunpack_tree_opts.skip_cache_tree_update = 1;\n \t\tunpack_tree_opts.reset = UNPACK_RESET_PROTECT_UNTRACKED;\n+\t}\n \n \tif (!reset_hard && !fill_tree_descriptor(r, &desc[nr++], &head_oid)) {\n \t\tret = error(_(\"failed to find tree of %s\"),\n@@ -197,7 +198,8 @@ int reset_working_tree(struct repository *r,\n \t\tgoto leave_reset_head;\n \t}\n \n-\tprime_cache_tree(r, r->index, tree);\n+\tif (reset_hard)\n+\t\tprime_cache_tree(r, r->index, tree);\n \n \tif (write_locked_index(r->index, &lock, COMMIT_LOCK) < 0) {\n \t\tret = error(_(\"could not write index\"));\n\n-- \n2.55.0.rc2.803.g1fd1e6609c.dirty\n\n"},{"id":"546642","messageId":"20260629-b4-pks-history-drop-v7-9-6e9392a957d8@pks.im","threadId":"65726","inReplyTo":"20260629-b4-pks-history-drop-v7-0-6e9392a957d8@pks.im","subject":"[PATCH v7 09/11] replay: expose `replay_result_queue_update()`","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-29T07:34:35Z","receivedAt":"2026-06-29T07:34:57Z","isPatch":true,"body":"Expose `replay_result_queue_update()`, which is used to append another\nreference update to the replay result. This function will be used in a\nsubsequent commit.\n\nSuggested-by: Christian Couder <christian.couder@gmail.com>\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n replay.c | 8 ++++----\n replay.h | 5 +++++\n 2 files changed, 9 insertions(+), 4 deletions(-)\n\ndiff --git a/replay.c b/replay.c\nindex 4ef8abb607..7c8433107b 100644\n--- a/replay.c\n+++ b/replay.c\n@@ -351,10 +351,10 @@ void replay_result_release(struct replay_result *result)\n \tfree(result->updates);\n }\n \n-static void replay_result_queue_update(struct replay_result *result,\n-\t\t\t\t       const char *refname,\n-\t\t\t\t       const struct object_id *old_oid,\n-\t\t\t\t       const struct object_id *new_oid)\n+void replay_result_queue_update(struct replay_result *result,\n+\t\t\t\tconst char *refname,\n+\t\t\t\tconst struct object_id *old_oid,\n+\t\t\t\tconst struct object_id *new_oid)\n {\n \tALLOC_GROW(result->updates, result->updates_nr + 1, result->updates_alloc);\n \tresult->updates[result->updates_nr].refname = xstrdup(refname);\ndiff --git a/replay.h b/replay.h\nindex 1851a07705..da83b65345 100644\n--- a/replay.h\n+++ b/replay.h\n@@ -80,6 +80,11 @@ struct replay_result {\n \n void replay_result_release(struct replay_result *result);\n \n+void replay_result_queue_update(struct replay_result *result,\n+\t\t\t\tconst char *refname,\n+\t\t\t\tconst struct object_id *old_oid,\n+\t\t\t\tconst struct object_id *new_oid);\n+\n /*\n  * Replay a set of commits onto a new location. Leaves both the working tree,\n  * index and references untouched. Reference updates caused by the replay will\n\n-- \n2.55.0.rc2.803.g1fd1e6609c.dirty\n\n"},{"id":"546644","messageId":"20260629-b4-pks-history-drop-v7-10-6e9392a957d8@pks.im","threadId":"65726","inReplyTo":"20260629-b4-pks-history-drop-v7-0-6e9392a957d8@pks.im","subject":"[PATCH v7 10/11] builtin/history: split handling of ref updates into two phases","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-29T07:34:36Z","receivedAt":"2026-06-29T07:35:00Z","isPatch":true,"body":"The function `handle_reference_updates()` is used by git-history(1) to\nupdate all references that refer to commits that have been rewritten. As\nsuch, it performs two steps:\n\n  - It gathers the references that need to be updated in the first\n    place.\n\n  - It prepares and commits the reference transaction.\n\nIn a subsequent commit we'll want to handle those two steps separately.\nPrepare for this by splitting up the function into two.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n builtin/history.c | 100 +++++++++++++++++++++++++++++++++---------------------\n 1 file changed, 62 insertions(+), 38 deletions(-)\n\ndiff --git a/builtin/history.c b/builtin/history.c\nindex 0fc06fb204..22b9fcb4a4 100644\n--- a/builtin/history.c\n+++ b/builtin/history.c\n@@ -333,21 +333,17 @@ static int handle_ref_update(struct ref_transaction *transaction,\n \t\t\t\t      NULL, NULL, 0, reflog_msg, err);\n }\n \n-static int handle_reference_updates(struct rev_info *revs,\n-\t\t\t\t    enum ref_action action,\n-\t\t\t\t    struct commit *original,\n-\t\t\t\t    struct commit *rewritten,\n-\t\t\t\t    const char *reflog_msg,\n-\t\t\t\t    int dry_run,\n-\t\t\t\t    enum replay_empty_commit_action empty)\n+static int compute_pending_ref_updates(struct rev_info *revs,\n+\t\t\t\t       enum ref_action action,\n+\t\t\t\t       struct commit *original,\n+\t\t\t\t       struct commit *rewritten,\n+\t\t\t\t       enum replay_empty_commit_action empty,\n+\t\t\t\t       struct replay_result *result)\n {\n \tconst struct name_decoration *decoration;\n \tstruct replay_revisions_options opts = {\n \t\t.empty = empty,\n \t};\n-\tstruct replay_result result = { 0 };\n-\tstruct ref_transaction *transaction = NULL;\n-\tstruct strbuf err = STRBUF_INIT;\n \tchar hex[GIT_MAX_HEXSZ + 1];\n \tbool detached_head;\n \tint head_flags = 0;\n@@ -359,34 +355,13 @@ static int handle_reference_updates(struct rev_info *revs,\n \n \topts.onto = oid_to_hex_r(hex, &rewritten->object.oid);\n \n-\tret = replay_revisions(revs, &opts, &result);\n+\tret = replay_revisions(revs, &opts, result);\n \tif (ret)\n-\t\tgoto out;\n+\t\treturn ret;\n \n \tif (action != REF_ACTION_BRANCHES && action != REF_ACTION_HEAD)\n \t\tBUG(\"unsupported ref action %d\", action);\n \n-\tif (!dry_run) {\n-\t\ttransaction = ref_store_transaction_begin(get_main_ref_store(revs->repo), 0, &err);\n-\t\tif (!transaction) {\n-\t\t\tret = error(_(\"failed to begin ref transaction: %s\"), err.buf);\n-\t\t\tgoto out;\n-\t\t}\n-\t}\n-\n-\tfor (size_t i = 0; i < result.updates_nr; i++) {\n-\t\tret = handle_ref_update(transaction,\n-\t\t\t\t\tresult.updates[i].refname,\n-\t\t\t\t\t&result.updates[i].new_oid,\n-\t\t\t\t\t&result.updates[i].old_oid,\n-\t\t\t\t\treflog_msg, &err);\n-\t\tif (ret) {\n-\t\t\tret = error(_(\"failed to update ref '%s': %s\"),\n-\t\t\t\t    result.updates[i].refname, err.buf);\n-\t\t\tgoto out;\n-\t\t}\n-\t}\n-\n \t/*\n \t * `replay_revisions()` only updates references that are\n \t * ancestors of `rewritten`, so we need to manually\n@@ -414,14 +389,41 @@ static int handle_reference_updates(struct rev_info *revs,\n \t\t    !detached_head)\n \t\t\tcontinue;\n \n+\t\treplay_result_queue_update(result, decoration->name,\n+\t\t\t\t\t   &original->object.oid,\n+\t\t\t\t\t   &rewritten->object.oid);\n+\t}\n+\n+\treturn 0;\n+}\n+\n+static int apply_pending_ref_updates(struct repository *repo,\n+\t\t\t\t     const struct replay_result *result,\n+\t\t\t\t     const char *reflog_msg,\n+\t\t\t\t     int dry_run)\n+{\n+\tstruct ref_transaction *transaction = NULL;\n+\tstruct strbuf err = STRBUF_INIT;\n+\tint ret;\n+\n+\tif (!dry_run) {\n+\t\ttransaction = ref_store_transaction_begin(get_main_ref_store(repo),\n+\t\t\t\t\t\t\t  0, &err);\n+\t\tif (!transaction) {\n+\t\t\tret = error(_(\"failed to begin ref transaction: %s\"), err.buf);\n+\t\t\tgoto out;\n+\t\t}\n+\t}\n+\n+\tfor (size_t i = 0; i < result->updates_nr; i++) {\n \t\tret = handle_ref_update(transaction,\n-\t\t\t\t\tdecoration->name,\n-\t\t\t\t\t&rewritten->object.oid,\n-\t\t\t\t\t&original->object.oid,\n+\t\t\t\t\tresult->updates[i].refname,\n+\t\t\t\t\t&result->updates[i].new_oid,\n+\t\t\t\t\t&result->updates[i].old_oid,\n \t\t\t\t\treflog_msg, &err);\n \t\tif (ret) {\n \t\t\tret = error(_(\"failed to update ref '%s': %s\"),\n-\t\t\t\t    decoration->name, err.buf);\n+\t\t\t\t    result->updates[i].refname, err.buf);\n \t\t\tgoto out;\n \t\t}\n \t}\n@@ -435,11 +437,33 @@ static int handle_reference_updates(struct rev_info *revs,\n \n out:\n \tref_transaction_free(transaction);\n-\treplay_result_release(&result);\n \tstrbuf_release(&err);\n \treturn ret;\n }\n \n+static int handle_reference_updates(struct rev_info *revs,\n+\t\t\t\t    enum ref_action action,\n+\t\t\t\t    struct commit *original,\n+\t\t\t\t    struct commit *rewritten,\n+\t\t\t\t    const char *reflog_msg,\n+\t\t\t\t    int dry_run,\n+\t\t\t\t    enum replay_empty_commit_action empty)\n+{\n+\tstruct replay_result result = { 0 };\n+\tint ret;\n+\n+\tret = compute_pending_ref_updates(revs, action, original, rewritten,\n+\t\t\t\t\t  empty, &result);\n+\tif (ret)\n+\t\tgoto out;\n+\n+\tret = apply_pending_ref_updates(revs->repo, &result, reflog_msg, dry_run);\n+\n+out:\n+\treplay_result_release(&result);\n+\treturn ret;\n+}\n+\n static int commit_became_empty(struct repository *repo,\n \t\t\t       struct commit *original,\n \t\t\t       struct tree *result)\n\n-- \n2.55.0.rc2.803.g1fd1e6609c.dirty\n\n"},{"id":"546645","messageId":"20260629-b4-pks-history-drop-v7-11-6e9392a957d8@pks.im","threadId":"65726","inReplyTo":"20260629-b4-pks-history-drop-v7-0-6e9392a957d8@pks.im","subject":"[PATCH v7 11/11] builtin/history: implement \"drop\" subcommand","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-06-29T07:34:37Z","receivedAt":"2026-06-29T07:35:05Z","isPatch":true,"body":"A common operation when editing the commit history is to drop a specific\ncommit from the history entirely, but this operation is not currently\ncovered by git-history(1).\n\nA couple of noteworthy bits:\n\n  - This is the first git-history(1) command that will ultimately result\n    in changes to both the index and the working tree. We thus have to\n    add logic to merge resulting changes into those.\n\n  - It is still not possible to replay merge commits, so this limitation\n    is inherited for the new \"drop\" command.\n\n  - For now we refuse to drop root commits. While we _can_ indeed drop\n    root commits in the general case, there are edge cases where the\n    resulting history would become completely empty. This is thus left\n    to a subsequent patch series.\n\nOther than that, most of the logic is rather straight-forward as we can\ncontinue to build on the preexisting logic in git-history(1) for most of\nthe part.\n\nSigned-off-by: Patrick Steinhardt <ps@pks.im>\n---\n Documentation/git-history.adoc |  38 ++-\n builtin/history.c              | 186 ++++++++++++++\n t/meson.build                  |   1 +\n t/t3454-history-drop.sh        | 537 +++++++++++++++++++++++++++++++++++++++++\n 4 files changed, 761 insertions(+), 1 deletion(-)\n\ndiff --git a/Documentation/git-history.adoc b/Documentation/git-history.adoc\nindex 2ba8121795..28b477cd37 100644\n--- a/Documentation/git-history.adoc\n+++ b/Documentation/git-history.adoc\n@@ -8,6 +8,7 @@ git-history - EXPERIMENTAL: Rewrite history\n SYNOPSIS\n --------\n [synopsis]\n+git history drop <commit> [--dry-run] [--update-refs=(branches|head)] [--empty=(drop|keep|abort)]\n git history fixup <commit> [--dry-run] [--update-refs=(branches|head)] [--reedit-message] [--empty=(drop|keep|abort)]\n git history reword <commit> [--dry-run] [--update-refs=(branches|head)]\n git history split <commit> [--dry-run] [--update-refs=(branches|head)] [--] [<pathspec>...]\n@@ -51,13 +52,28 @@ be stateful operations. The limitation can be lifted once (if) Git learns about\n first-class conflicts.\n \n When using `fixup` with `--empty=drop`, dropping the root commit is not yet\n-supported.\n+supported. Likewise, `drop` cannot remove the root commit or a merge commit.\n \n COMMANDS\n --------\n \n The following commands are available to rewrite history in different ways:\n \n+`drop <commit>`::\n+\tRemove the specified commit from the history. All descendants of the\n+\tcommit are replayed directly onto its parent.\n++\n+The root commit cannot be dropped as that may lead to edge cases where refs\n+end up with no commits anymore. Merge commits cannot be dropped either; see\n+LIMITATIONS.\n++\n+If `HEAD` points at a commit that is to be rewritten, the index and working\n+tree are updated to match the new `HEAD`. The command aborts before any\n+references are updated in case local modifications would be overwritten.\n++\n+If replaying any descendant would result in a conflict, the command aborts\n+with an error.\n+\n `fixup <commit>`::\n \tApply the currently staged changes to the specified commit. This is\n \tsimilar in nature to `git commit --fixup=<commit>` followed by `git\n@@ -170,6 +186,26 @@ The staged addition of `unrelated.txt` has been incorporated into the `first`\n commit. All descendant commits have been replayed on top of the rewritten\n history.\n \n+Drop a commit\n+~~~~~~~~~~~~~\n+\n+----------\n+$ git log --oneline\n+abc1234 (HEAD -> main) third\n+def5678 second\n+ghi9012 first\n+\n+$ git history drop 'main^{/second}'\n+\n+$ git log --oneline\n+jkl3456 (HEAD -> main) third\n+ghi9012 first\n+----------\n+\n+The `second` commit has been removed from the history, and `third` has been\n+replayed directly on top of `first`. All branches that pointed at the dropped\n+commit have been moved to its parent.\n+\n Split a commit\n ~~~~~~~~~~~~~~\n \ndiff --git a/builtin/history.c b/builtin/history.c\nindex 22b9fcb4a4..7944207f38 100644\n--- a/builtin/history.c\n+++ b/builtin/history.c\n@@ -17,13 +17,17 @@\n #include \"read-cache.h\"\n #include \"refs.h\"\n #include \"replay.h\"\n+#include \"reset.h\"\n #include \"revision.h\"\n #include \"sequencer.h\"\n #include \"strvec.h\"\n #include \"tree.h\"\n+#include \"tree-walk.h\"\n #include \"unpack-trees.h\"\n #include \"wt-status.h\"\n \n+#define GIT_HISTORY_DROP_USAGE \\\n+\tN_(\"git history drop <commit> [--dry-run] [--update-refs=(branches|head)] [--empty=(drop|keep|abort)]\")\n #define GIT_HISTORY_FIXUP_USAGE \\\n \tN_(\"git history fixup <commit> [--dry-run] [--update-refs=(branches|head)] [--reedit-message] [--empty=(drop|keep|abort)]\")\n #define GIT_HISTORY_REWORD_USAGE \\\n@@ -999,12 +1003,193 @@ static int cmd_history_split(int argc,\n \treturn ret;\n }\n \n+static int update_worktree(struct repository *repo,\n+\t\t\t   const struct commit *old_head,\n+\t\t\t   const struct commit *new_head,\n+\t\t\t   bool dry_run)\n+{\n+\tstruct reset_working_tree_options opts = {\n+\t\t.oid_from = &old_head->object.oid,\n+\t\t.oid = &new_head->object.oid,\n+\t};\n+\tif (dry_run)\n+\t\topts.flags |= RESET_WORKING_TREE_DRY_RUN;\n+\treturn reset_working_tree(repo, &opts);\n+}\n+\n+static int find_head_tree_change(struct repository *repo,\n+\t\t\t\t const struct replay_result *result,\n+\t\t\t\t struct commit **old_head,\n+\t\t\t\t struct commit **new_head,\n+\t\t\t\t bool *changed)\n+{\n+\tconst struct replay_ref_update *head_update = NULL;\n+\tstruct commit *old_head_commit, *new_head_commit;\n+\tstruct tree *old_head_tree, *new_head_tree;\n+\tconst char *head_target;\n+\tint head_flags;\n+\n+\t*changed = false;\n+\n+\thead_target = refs_resolve_ref_unsafe(get_main_ref_store(repo),\n+\t\t\t\t\t      \"HEAD\", RESOLVE_REF_NO_RECURSE,\n+\t\t\t\t\t      NULL, &head_flags);\n+\tif (!head_target)\n+\t\treturn error(_(\"cannot look up HEAD\"));\n+\tif (!(head_flags & REF_ISSYMREF))\n+\t\thead_target = \"HEAD\";\n+\n+\tfor (size_t i = 0; i < result->updates_nr; i++) {\n+\t\tif (!strcmp(result->updates[i].refname, head_target)) {\n+\t\t\thead_update = &result->updates[i];\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\n+\tif (!head_update)\n+\t\treturn 0;\n+\n+\told_head_commit = lookup_commit_reference(repo, &head_update->old_oid);\n+\tnew_head_commit = lookup_commit_reference(repo, &head_update->new_oid);\n+\tif (!old_head_commit || !new_head_commit)\n+\t\treturn error(_(\"cannot resolve HEAD commit\"));\n+\n+\told_head_tree = repo_get_commit_tree(repo, old_head_commit);\n+\tnew_head_tree = repo_get_commit_tree(repo, new_head_commit);\n+\tif (!old_head_tree || !new_head_tree)\n+\t\treturn error(_(\"cannot resolve tree for HEAD\"));\n+\n+\tif (oideq(&old_head_tree->object.oid, &new_head_tree->object.oid))\n+\t\treturn 0;\n+\n+\t*old_head = old_head_commit;\n+\t*new_head = new_head_commit;\n+\t*changed = true;\n+\n+\treturn 0;\n+}\n+\n+static int cmd_history_drop(int argc,\n+\t\t\t    const char **argv,\n+\t\t\t    const char *prefix,\n+\t\t\t    struct repository *repo)\n+{\n+\tconst char * const usage[] = {\n+\t\tGIT_HISTORY_DROP_USAGE,\n+\t\tNULL,\n+\t};\n+\tenum replay_empty_commit_action empty = REPLAY_EMPTY_COMMIT_DROP;\n+\tenum ref_action action = REF_ACTION_DEFAULT;\n+\tint dry_run = 0;\n+\tstruct option options[] = {\n+\t\tOPT_CALLBACK_F(0, \"update-refs\", &action, \"(branches|head)\",\n+\t\t\t       N_(\"control which refs should be updated\"),\n+\t\t\t       PARSE_OPT_NONEG, parse_ref_action),\n+\t\tOPT_BOOL('n', \"dry-run\", &dry_run,\n+\t\t\t N_(\"perform a dry-run without updating any refs\")),\n+\t\tOPT_CALLBACK_F(0, \"empty\", &empty, \"(drop|keep|abort)\",\n+\t\t\t       N_(\"how to handle descendants that become empty\"),\n+\t\t\t       PARSE_OPT_NONEG, parse_opt_empty),\n+\t\tOPT_END(),\n+\t};\n+\tstruct strbuf reflog_msg = STRBUF_INIT;\n+\tstruct commit *original, *rewritten;\n+\tstruct rev_info revs = { 0 };\n+\tstruct replay_result result = { 0 };\n+\tstruct commit *old_head, *new_head;\n+\tbool head_moves = false;\n+\tint ret;\n+\n+\targc = parse_options(argc, argv, prefix, options, usage, 0);\n+\tif (argc != 1) {\n+\t\tret = error(_(\"command expects a single revision\"));\n+\t\tgoto out;\n+\t}\n+\trepo_config(repo, git_default_config, NULL);\n+\n+\tif (action == REF_ACTION_DEFAULT)\n+\t\taction = REF_ACTION_BRANCHES;\n+\n+\toriginal = lookup_commit_reference_by_name(argv[0]);\n+\tif (!original) {\n+\t\tret = error(_(\"commit cannot be found: %s\"), argv[0]);\n+\t\tgoto out;\n+\t}\n+\n+\tif (!original->parents) {\n+\t\tret = error(_(\"cannot drop root commit %s: \"\n+\t\t\t      \"it has no parent to replay onto\"),\n+\t\t\t    argv[0]);\n+\t\tgoto out;\n+\t} else if (original->parents->next) {\n+\t\tret = error(_(\"cannot drop merge commit: %s\"), argv[0]);\n+\t\tgoto out;\n+\t}\n+\n+\tret = setup_revwalk(repo, action, original, &revs);\n+\tif (ret)\n+\t\tgoto out;\n+\n+\trewritten = original->parents->item;\n+\n+\tret = compute_pending_ref_updates(&revs, action, original, rewritten,\n+\t\t\t\t\t  empty, &result);\n+\tif (ret) {\n+\t\tret = error(_(\"failed replaying descendants\"));\n+\t\tgoto out;\n+\t}\n+\n+\t/*\n+\t * If HEAD will move as a result of the rewrite then we'll have to\n+\t * merge in the changes into the worktree and index. This merge can of\n+\t * course conflict, which will cause the whole operation to abort.\n+\t *\n+\t * If we had already updated the refs at that point then we'd have an\n+\t * inconsistent repository state. So we first perform a dry-run merge\n+\t * here before updating refs.\n+\t */\n+\tif (!is_bare_repository()) {\n+\t\tret = find_head_tree_change(repo, &result, &old_head,\n+\t\t\t\t\t    &new_head, &head_moves);\n+\t\tif (ret < 0)\n+\t\t\tgoto out;\n+\n+\t\tif (head_moves && update_worktree(repo, old_head, new_head, true) < 0) {\n+\t\t\tret = error(_(\"dropping this commit would \"\n+\t\t\t\t      \"overwrite local changes; aborting\"));\n+\t\t\tgoto out;\n+\t\t}\n+\t}\n+\n+\tstrbuf_addf(&reflog_msg, \"drop: dropping %s\", argv[0]);\n+\tret = apply_pending_ref_updates(repo, &result, reflog_msg.buf, dry_run);\n+\tif (ret < 0) {\n+\t\tret = error(_(\"failed to update references\"));\n+\t\tgoto out;\n+\t}\n+\n+\tif (!dry_run && head_moves && update_worktree(repo, old_head, new_head, false) < 0) {\n+\t\tret = error(_(\"could not update working tree to new commit %s\"),\n+\t\t\t    oid_to_hex(&new_head->object.oid));\n+\t\tgoto out;\n+\t}\n+\n+\tret = 0;\n+\n+out:\n+\treplay_result_release(&result);\n+\tstrbuf_release(&reflog_msg);\n+\trelease_revisions(&revs);\n+\treturn ret;\n+}\n+\n int cmd_history(int argc,\n \t\tconst char **argv,\n \t\tconst char *prefix,\n \t\tstruct repository *repo)\n {\n \tconst char * const usage[] = {\n+\t\tGIT_HISTORY_DROP_USAGE,\n \t\tGIT_HISTORY_FIXUP_USAGE,\n \t\tGIT_HISTORY_REWORD_USAGE,\n \t\tGIT_HISTORY_SPLIT_USAGE,\n@@ -1012,6 +1197,7 @@ int cmd_history(int argc,\n \t};\n \tparse_opt_subcommand_fn *fn = NULL;\n \tstruct option options[] = {\n+\t\tOPT_SUBCOMMAND(\"drop\", &fn, cmd_history_drop),\n \t\tOPT_SUBCOMMAND(\"fixup\", &fn, cmd_history_fixup),\n \t\tOPT_SUBCOMMAND(\"reword\", &fn, cmd_history_reword),\n \t\tOPT_SUBCOMMAND(\"split\", &fn, cmd_history_split),\ndiff --git a/t/meson.build b/t/meson.build\nindex 2af8d01279..d5e71056b2 100644\n--- a/t/meson.build\n+++ b/t/meson.build\n@@ -399,6 +399,7 @@ integration_tests = [\n   't3451-history-reword.sh',\n   't3452-history-split.sh',\n   't3453-history-fixup.sh',\n+  't3454-history-drop.sh',\n   't3500-cherry.sh',\n   't3501-revert-cherry-pick.sh',\n   't3502-cherry-pick-merge.sh',\ndiff --git a/t/t3454-history-drop.sh b/t/t3454-history-drop.sh\nnew file mode 100755\nindex 0000000000..0f33247212\n--- /dev/null\n+++ b/t/t3454-history-drop.sh\n@@ -0,0 +1,537 @@\n+#!/bin/sh\n+\n+test_description='tests for git-history drop subcommand'\n+\n+. ./test-lib.sh\n+. \"$TEST_DIRECTORY/lib-log-graph.sh\"\n+\n+expect_graph () {\n+\tcat >expect &&\n+\tlib_test_cmp_graph --format=%s \"$@\"\n+}\n+\n+expect_log () {\n+\tgit log --format=\"%s\" \"$@\" >actual &&\n+\tcat >expect &&\n+\ttest_cmp expect actual\n+}\n+\n+test_expect_success 'errors on missing commit argument' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit initial &&\n+\t\ttest_must_fail git history drop 2>err &&\n+\t\ttest_grep \"command expects a single revision\" err\n+\t)\n+'\n+\n+test_expect_success 'errors on too many arguments' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit initial &&\n+\t\ttest_must_fail git history drop HEAD HEAD 2>err &&\n+\t\ttest_grep \"command expects a single revision\" err\n+\t)\n+'\n+\n+test_expect_success 'errors on unknown revision' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit initial &&\n+\t\ttest_must_fail git history drop does-not-exist 2>err &&\n+\t\ttest_grep \"commit cannot be found: does-not-exist\" err\n+\t)\n+'\n+\n+test_expect_success 'errors with invalid --empty= value' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit initial &&\n+\t\ttest_commit second &&\n+\t\ttest_must_fail git history drop --empty=bogus HEAD 2>err &&\n+\t\ttest_grep \"unrecognized.*--empty.*bogus\" err\n+\t)\n+'\n+\n+test_expect_success 'drops a commit in the middle and replays descendants' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\n+\t\tgit symbolic-ref HEAD >expect &&\n+\t\tgit history drop HEAD~ &&\n+\t\tgit symbolic-ref HEAD >actual &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\texpect_log <<-\\EOF &&\n+\t\tthird\n+\t\tfirst\n+\t\tEOF\n+\n+\t\ttest_must_fail git show HEAD:second.t &&\n+\t\ttest_path_is_missing second.t &&\n+\n+\t\tgit reflog >reflog &&\n+\t\ttest_grep \"drop: dropping HEAD~\" reflog\n+\t)\n+'\n+\n+test_expect_success 'drops the HEAD commit' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\n+\t\tgit history drop HEAD &&\n+\n+\t\texpect_log <<-\\EOF\n+\t\tfirst\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'drops a commit on detached HEAD' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\t\tgit checkout --detach HEAD &&\n+\n+\t\tgit history drop HEAD~ &&\n+\n+\t\texpect_log <<-\\EOF\n+\t\tthird\n+\t\tfirst\n+\t\tEOF\n+\t)\n+'\n+\n+# Note: in this case it would actually be fine to drop the root commit, as we\n+# do have a descendant commit, and no reference points to the root commit\n+# directly. So this is something that we may relax eventually.\n+test_expect_success 'refuses to drop the root commit' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\n+\t\ttest_must_fail git history drop HEAD~ 2>err &&\n+\t\ttest_grep \"cannot drop root commit\" err\n+\t)\n+'\n+\n+# In contrast to the above case, we actually don't want to drop the root commit\n+# here as that would cause us to end up with an empty commit graph.\n+test_expect_success 'refuses to drop the root commit when branch becomes empty' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\n+\t\ttest_must_fail git history drop HEAD 2>err &&\n+\t\ttest_grep \"cannot drop root commit\" err\n+\t)\n+'\n+\n+test_expect_success 'refuses to drop a merge commit' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\tgit branch branch &&\n+\t\ttest_commit ours &&\n+\t\tgit switch branch &&\n+\t\ttest_commit theirs &&\n+\t\tgit switch - &&\n+\t\tgit merge theirs &&\n+\n+\t\ttest_must_fail git history drop HEAD 2>err &&\n+\t\ttest_grep \"cannot drop merge commit\" err\n+\t)\n+'\n+\n+test_expect_success 'refuses when descendants contain a merge commit' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\ttest_commit middle &&\n+\t\tgit branch branch &&\n+\t\ttest_commit ours &&\n+\t\tgit switch branch &&\n+\t\ttest_commit theirs &&\n+\t\tgit switch - &&\n+\t\tgit merge theirs &&\n+\n+\t\ttest_must_fail git history drop middle 2>err &&\n+\t\ttest_grep \"replaying merge commits is not supported yet\" err\n+\t)\n+'\n+\n+test_expect_success 'works in a bare repository' '\n+\ttest_when_finished \"rm -rf repo repo.git\" &&\n+\n+\tgit init repo &&\n+\ttest_commit -C repo first &&\n+\ttest_commit -C repo second &&\n+\ttest_commit -C repo third &&\n+\n+\tgit clone --bare repo repo.git &&\n+\t(\n+\t\tcd repo.git &&\n+\n+\t\tgit history drop HEAD~ &&\n+\t\texpect_log <<-\\EOF\n+\t\tthird\n+\t\tfirst\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'updates branches on other lines of descent' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\ttest_commit target &&\n+\t\tgit branch theirs &&\n+\t\ttest_commit ours &&\n+\t\tgit switch theirs &&\n+\t\ttest_commit theirs &&\n+\n+\t\texpect_graph --branches <<-\\EOF &&\n+\t\t* theirs\n+\t\t| * ours\n+\t\t|/\n+\t\t* target\n+\t\t* base\n+\t\tEOF\n+\n+\t\tgit history drop target &&\n+\n+\t\texpect_graph --branches <<-\\EOF\n+\t\t* ours\n+\t\t| * theirs\n+\t\t|/\n+\t\t* base\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'moves branch pointing at dropped commit to its parent' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo --initial-branch=main &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\tgit branch points-at-second &&\n+\t\ttest_commit third &&\n+\n+\t\tgit rev-parse first >expect &&\n+\t\tgit history drop second &&\n+\t\tgit rev-parse points-at-second >actual &&\n+\t\ttest_cmp expect actual &&\n+\n+\t\texpect_log --format=\"%s %D\" --branches <<-\\EOF\n+\t\tthird HEAD -> main\n+\t\tfirst tag: first, points-at-second\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--dry-run prints ref updates without modifying repo' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo --initial-branch=main &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\tgit branch branch &&\n+\t\ttest_commit middle &&\n+\t\ttest_commit ours &&\n+\t\tgit switch branch &&\n+\t\ttest_commit theirs &&\n+\n+\t\tgit refs list >refs-expect &&\n+\t\tgit history drop --dry-run main~ >updates &&\n+\t\tgit refs list >refs-actual &&\n+\t\ttest_cmp refs-expect refs-actual &&\n+\t\ttest_grep \"update refs/heads/main\" updates &&\n+\n+\t\tgit update-ref --stdin <updates &&\n+\t\texpect_log main <<-\\EOF\n+\t\tours\n+\t\tbase\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--dry-run detects conflicts with modified working tree' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo --initial-branch=main &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second modify-me &&\n+\t\techo modified >modify-me &&\n+\n+\t\tgit refs list >refs-expect &&\n+\t\tgit diff >diff-expect &&\n+\t\ttest_must_fail git history drop --dry-run HEAD 2>err &&\n+\t\ttest_grep \"dropping this commit would overwrite local changes\" err &&\n+\t\tgit diff >diff-actual &&\n+\t\tgit refs list >refs-actual &&\n+\n+\t\ttest_cmp diff-expect diff-actual &&\n+\t\ttest_cmp refs-expect refs-actual\n+\t)\n+'\n+\n+test_expect_success '--update-refs=head updates only HEAD' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo --initial-branch=main &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\ttest_commit target &&\n+\t\tgit branch theirs &&\n+\t\ttest_commit ours &&\n+\t\tgit switch theirs &&\n+\t\ttest_commit theirs &&\n+\n+\t\t# When told to update HEAD only, the command refuses to\n+\t\t# rewrite commits that are not an ancestor of HEAD.\n+\t\ttest_must_fail git history drop --update-refs=head main 2>err &&\n+\t\ttest_grep \"rewritten commit must be an ancestor of HEAD\" err &&\n+\n+\t\texpect_graph --branches <<-\\EOF &&\n+\t\t* theirs\n+\t\t| * ours\n+\t\t|/\n+\t\t* target\n+\t\t* base\n+\t\tEOF\n+\n+\t\tgit switch main &&\n+\t\tgit history drop --update-refs=head target &&\n+\n+\t\texpect_graph --branches <<-\\EOF\n+\t\t* ours\n+\t\t| * theirs\n+\t\t| * target\n+\t\t|/\n+\t\t* base\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success 'conflict with replayed commit aborts cleanly' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\ttest_commit conflict-a file &&\n+\t\ttest_commit conflict-b file &&\n+\n+\t\tgit refs list >refs-expect &&\n+\t\ttest_must_fail git history drop HEAD~ 2>err &&\n+\t\ttest_grep \"failed replaying descendants\" err &&\n+\t\tgit refs list >refs-actual &&\n+\t\ttest_cmp refs-expect refs-actual\n+\t)\n+'\n+\n+# Build a history where a descendant of the drop target reverts the change\n+# introduced by the drop target. After dropping, the descendant's diff applies\n+# against a tree that already lacks the change, so it becomes empty.\n+setup_empty_descendant_repo () {\n+\tgit init \"$1\" &&\n+\t(\n+\t\tcd \"$1\" &&\n+\t\techo C1 >file &&\n+\t\tgit add file &&\n+\t\tgit commit -m \"base\" &&\n+\t\tgit tag base &&\n+\t\techo C2 >file &&\n+\t\tgit add file &&\n+\t\tgit commit -m \"drop-me\" &&\n+\t\tgit tag drop-me &&\n+\t\ttest_commit middle &&\n+\t\techo C1 >file &&\n+\t\tgit add file &&\n+\t\tgit commit -m \"revert-drop-me\" &&\n+\t\tgit tag revert-drop-me\n+\t)\n+}\n+\n+test_expect_success '--empty=drop drops descendants that become empty' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tsetup_empty_descendant_repo repo &&\n+\t(\n+\t\tcd repo &&\n+\n+\t\tgit history drop --empty=drop drop-me &&\n+\n+\t\texpect_log <<-\\EOF\n+\t\tmiddle\n+\t\tbase\n+\t\tEOF\n+\t)\n+'\n+\n+test_expect_success '--empty=keep keeps descendants that become empty' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tsetup_empty_descendant_repo repo &&\n+\t(\n+\t\tcd repo &&\n+\n+\t\tgit history drop --empty=keep drop-me &&\n+\n+\t\texpect_log <<-\\EOF &&\n+\t\trevert-drop-me\n+\t\tmiddle\n+\t\tbase\n+\t\tEOF\n+\t\tgit diff HEAD~ HEAD >diff &&\n+\t\ttest_must_be_empty diff\n+\t)\n+'\n+\n+test_expect_success '--empty=abort errors out when a descendant becomes empty' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tsetup_empty_descendant_repo repo &&\n+\t(\n+\t\tcd repo &&\n+\n+\t\ttest_must_fail git history drop --empty=abort drop-me 2>err &&\n+\t\ttest_grep \"became empty after replay\" err\n+\t)\n+'\n+\n+test_expect_success 'updates index and worktree when HEAD moves' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\n+\t\tgit history drop second &&\n+\n+\t\t# Worktree should no longer contain second.t.\n+\t\ttest_path_is_missing second.t &&\n+\t\ttest_path_is_file first.t &&\n+\t\ttest_path_is_file third.t &&\n+\n+\t\t# Index and worktree should both match the new HEAD.\n+\t\tgit status --porcelain --untracked-files=no >status &&\n+\t\ttest_must_be_empty status\n+\t)\n+'\n+\n+test_expect_success 'updates worktree when dropping HEAD itself' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\n+\t\tgit history drop HEAD &&\n+\n+\t\ttest_path_is_missing second.t &&\n+\t\ttest_path_is_file first.t &&\n+\n+\t\tgit status --porcelain --untracked-files=no >status &&\n+\t\ttest_must_be_empty status\n+\t)\n+'\n+\n+test_expect_success 'preserves unrelated unstaged modifications' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\techo first-content >unrelated.txt &&\n+\t\tgit add unrelated.txt &&\n+\t\tgit commit -m \"add unrelated\" &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\n+\t\techo locally-modified >unrelated.txt &&\n+\n+\t\tgit diff >diff-expect &&\n+\t\tgit history drop second &&\n+\t\tgit diff >diff-actual &&\n+\t\ttest_cmp diff-expect diff-actual &&\n+\t\ttest_path_is_missing second.t\n+\t)\n+'\n+\n+test_expect_success 'preserves unrelated staged changes' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\techo first-content >unrelated.txt &&\n+\t\tgit add unrelated.txt &&\n+\t\tgit commit -m \"add unrelated\" &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\n+\t\techo staged-change >unrelated.txt &&\n+\t\tgit add unrelated.txt &&\n+\n+\t\tgit diff --cached >diff-expect &&\n+\t\tgit history drop second &&\n+\t\tgit diff --cached >diff-actual &&\n+\t\ttest_cmp diff-expect diff-actual &&\n+\t\ttest_path_is_missing second.t\n+\t)\n+'\n+\n+test_expect_success 'aborts when local modifications would be overwritten' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit base &&\n+\t\ttest_commit conflict &&\n+\n+\t\techo local-edit >conflict.t &&\n+\t\tgit diff >diff-expect &&\n+\t\ttest_must_fail git history drop HEAD 2>err &&\n+\t\ttest_grep \"would overwrite local changes\" err &&\n+\t\tgit diff >diff-actual &&\n+\t\ttest_cmp diff-expect diff-actual\n+\t)\n+'\n+\n+test_done\n\n-- \n2.55.0.rc2.803.g1fd1e6609c.dirty\n\n"},{"id":"546705","messageId":"xmqqv7b1unxl.fsf@gitster.g","threadId":"65726","inReplyTo":"20260629-b4-pks-history-drop-v7-11-6e9392a957d8@pks.im","subject":"Re: [PATCH v7 11/11] builtin/history: implement \"drop\" subcommand","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-06-29T19:49:42Z","receivedAt":"2026-06-29T19:49:46Z","isPatch":true,"body":"Patrick Steinhardt <ps@pks.im> writes:\n\n> +static int find_head_tree_change(struct repository *repo,\n> +\t\t\t\t const struct replay_result *result,\n> +\t\t\t\t struct commit **old_head,\n> +\t\t\t\t struct commit **new_head,\n> +\t\t\t\t bool *changed)\n> +{\n> +\tconst struct replay_ref_update *head_update = NULL;\n> +\tstruct commit *old_head_commit, *new_head_commit;\n> +\tstruct tree *old_head_tree, *new_head_tree;\n> +\tconst char *head_target;\n> +\tint head_flags;\n> +\n> +\t*changed = false;\n> +\n> +\thead_target = refs_resolve_ref_unsafe(get_main_ref_store(repo),\n> +\t\t\t\t\t      \"HEAD\", RESOLVE_REF_NO_RECURSE,\n> +\t\t\t\t\t      NULL, &head_flags);\n> +\tif (!head_target)\n> +\t\treturn error(_(\"cannot look up HEAD\"));\n\nHere head_target would be something like \"refs/heads/master\", or\nwhatever the \"HEAD\" happens to point at.\n\n> +\tif (!(head_flags & REF_ISSYMREF))\n> +\t\thead_target = \"HEAD\";\n\nBut if it is not a symref, then it is a detached HEAD.  We manually\nset it to \"HEAD\" again.  We know head_target was not NULL, so what\ndid we receive in it from refs_resolve_ref_unsafe() call, before we\noverwrite it here?\n\n> +\tfor (size_t i = 0; i < result->updates_nr; i++) {\n> +\t\tif (!strcmp(result->updates[i].refname, head_target)) {\n> +\t\t\thead_update = &result->updates[i];\n> +\t\t\tbreak;\n> +\t\t}\n> +\t}\n\nWe see if we are going to update the currently checked out branch.\n\nThe .updates[] array was earlier populated by the caller that called\ncompute_pending_ref_updates() before it called us.\n\n> +\tif (!head_update)\n> +\t\treturn 0;\n\nAnd we return if not.  That is a simple and happy case.  Otherwise\nwe'd tell the caller about the updated commit and tree (strictly\nspeaking that would be redundant, but since we have it already, it\nis better to give them to the caller instead of forcing the caller\nto unwrap the commit).\n\n> +static int cmd_history_drop(int argc,\n> +\t\t\t    const char **argv,\n> +\t\t\t    const char *prefix,\n> +\t\t\t    struct repository *repo)\n> +{\n> +...\n> +\tstruct option options[] = {\n> +\t\tOPT_CALLBACK_F(0, \"update-refs\", &action, \"(branches|head)\",\n> +\t\t\t       N_(\"control which refs should be updated\"),\n> +\t\t\t       PARSE_OPT_NONEG, parse_ref_action),\n> ...\n> +\t\tOPT_END(),\n> +\t};\n> ...\n> +\tret = compute_pending_ref_updates(&revs, action, original, rewritten,\n> +\t\t\t\t\t  empty, &result);\n\nHere we call the function.  When action is \"--update-refs=head\",\ndoesn't this code in compute_pending_ref_updates() ... \n\n\t\tif (action == REF_ACTION_HEAD &&\n\t\t    decoration->type != DECORATION_REF_HEAD)\n\t\t\tcontinue;\n\n... skip any reference name (loaded by load_ref_decorations() lazily\nby calling get_name_decoration()) that is not \"HEAD\", which would\nmean we end up not finding any hits in the find_head_tree_change()\nfunction call we make later ...\n\n> +\tif (ret) {\n> +\t\tret = error(_(\"failed replaying descendants\"));\n> +\t\tgoto out;\n> +\t}\n> + ...\n> +\tif (!is_bare_repository()) {\n> +\t\tret = find_head_tree_change(repo, &result, &old_head,\n> +\t\t\t\t\t    &new_head, &head_moves);\n\n... here?\n"},{"id":"546867","messageId":"akTqJw0qlN0RGfyz@pks.im","threadId":"65726","inReplyTo":"xmqqv7b1unxl.fsf@gitster.g","subject":"Re: [PATCH v7 11/11] builtin/history: implement \"drop\" subcommand","fromName":"Patrick Steinhardt","fromEmail":"ps@pks.im","sentAt":"2026-07-01T10:21:27Z","receivedAt":"2026-07-01T10:21:34Z","isPatch":true,"body":"On Mon, Jun 29, 2026 at 12:49:42PM -0700, Junio C Hamano wrote:\n> Patrick Steinhardt <ps@pks.im> writes:\n> \n> > +static int find_head_tree_change(struct repository *repo,\n> > +\t\t\t\t const struct replay_result *result,\n> > +\t\t\t\t struct commit **old_head,\n> > +\t\t\t\t struct commit **new_head,\n> > +\t\t\t\t bool *changed)\n> > +{\n> > +\tconst struct replay_ref_update *head_update = NULL;\n> > +\tstruct commit *old_head_commit, *new_head_commit;\n> > +\tstruct tree *old_head_tree, *new_head_tree;\n> > +\tconst char *head_target;\n> > +\tint head_flags;\n> > +\n> > +\t*changed = false;\n> > +\n> > +\thead_target = refs_resolve_ref_unsafe(get_main_ref_store(repo),\n> > +\t\t\t\t\t      \"HEAD\", RESOLVE_REF_NO_RECURSE,\n> > +\t\t\t\t\t      NULL, &head_flags);\n> > +\tif (!head_target)\n> > +\t\treturn error(_(\"cannot look up HEAD\"));\n> \n> Here head_target would be something like \"refs/heads/master\", or\n> whatever the \"HEAD\" happens to point at.\n> \n> > +\tif (!(head_flags & REF_ISSYMREF))\n> > +\t\thead_target = \"HEAD\";\n> \n> But if it is not a symref, then it is a detached HEAD.  We manually\n> set it to \"HEAD\" again.  We know head_target was not NULL, so what\n> did we receive in it from refs_resolve_ref_unsafe() call, before we\n> overwrite it here?\n\nTrue, this is redundant indeed, as the returned name would already be\n\"HEAD\" in that case.\n\n[snip]\n> > +static int cmd_history_drop(int argc,\n> > +\t\t\t    const char **argv,\n> > +\t\t\t    const char *prefix,\n> > +\t\t\t    struct repository *repo)\n> > +{\n> > +...\n> > +\tstruct option options[] = {\n> > +\t\tOPT_CALLBACK_F(0, \"update-refs\", &action, \"(branches|head)\",\n> > +\t\t\t       N_(\"control which refs should be updated\"),\n> > +\t\t\t       PARSE_OPT_NONEG, parse_ref_action),\n> > ...\n> > +\t\tOPT_END(),\n> > +\t};\n> > ...\n> > +\tret = compute_pending_ref_updates(&revs, action, original, rewritten,\n> > +\t\t\t\t\t  empty, &result);\n> \n> Here we call the function.  When action is \"--update-refs=head\",\n> doesn't this code in compute_pending_ref_updates() ... \n> \n> \t\tif (action == REF_ACTION_HEAD &&\n> \t\t    decoration->type != DECORATION_REF_HEAD)\n> \t\t\tcontinue;\n> \n> ... skip any reference name (loaded by load_ref_decorations() lazily\n> by calling get_name_decoration()) that is not \"HEAD\", which would\n> mean we end up not finding any hits in the find_head_tree_change()\n> function call we make later ...\n> \n> > +\tif (ret) {\n> > +\t\tret = error(_(\"failed replaying descendants\"));\n> > +\t\tgoto out;\n> > +\t}\n> > + ...\n> > +\tif (!is_bare_repository()) {\n> > +\t\tret = find_head_tree_change(repo, &result, &old_head,\n> > +\t\t\t\t\t    &new_head, &head_moves);\n> \n> ... here?\n\nNo, because `replay_revisions()` populates the result with the resolved\nbranch name, and we're also searching for the same branch name inside\n`find_head_tree_change()`.\n\nPatrick\n"}]}