git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3 4/9] builtin/reflog: implement subcommand to write new entries

From
Patrick Steinhardt <ps@pks.im>
Date
Jul 29, 2025, 08:55 UTC
Message-ID
<20250729-pks-reflog-append-v3-4-9614d310f073@pks.im>
In-Reply-To
<20250729-pks-reflog-append-v3-0-9614d310f073@pks.im>

While we provide a couple of subcommands in git-reflog(1) to remove reflog entries, we don't provide any to write new entries. Obviously this is not an operation that really would be needed for many use cases out there, or otherwise people would have complained that such a command does not exist yet. But the introduction of the "reftable" backend changes the picture a bit, as it is now basically impossible to manually append a reflog entry if one wanted to do so due to the binary format.

Plug this gap by introducing a simple "write" subcommand. For now, all this command does is to append a single new reflog entry with the given object IDs and message to the reflog. More specifically, it is not yet possible to:

  - Write multiple reflog entries at once.
  - Insert reflog entries at arbitrary indices.
  - Specify the date of the reflog entry.
  - Insert reflog entries that refer to nonexistent objects.

If required, those features can be added at a future point in time. For now though, the new command aims to fulfill the most basic use cases while being as strict as possible when it comes to verifying parameters.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 Documentation/git-reflog.adoc |   7 +++
 builtin/reflog.c              |  65 ++++++++++++++++++++++
 t/meson.build                 |   1 +
 t/t1421-reflog-write.sh       | 126 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 199 insertions(+)
diff --git a/Documentation/git-reflog.adoc b/Documentation/git-reflog.adoc
index c3801b82fb6..34232a539a7 100644
--- a/Documentation/git-reflog.adoc
+++ b/Documentation/git-reflog.adoc
@@ -12,6 +12,7 @@ SYNOPSIS
 git reflog [show] [<log-options>] [<ref>]
 git reflog list
 git reflog exists <ref>
+git reflog write <ref> <old-oid> <new-oid> <message>
 git reflog delete [--rewrite] [--updateref]
 	[--dry-run | -n] [--verbose] <ref>@{<specifier>}...
 git reflog drop [--all [--single-worktree] | <refs>...]
@@ -47,6 +48,12 @@ The "exists" subcommand checks whether a ref has a reflog.  It exits
 with zero status if the reflog exists, and non-zero status if it does
 not.
 
+The "write" subcommand writes a single entry to the reflog of a given
+reference. This new entry is appended to the reflog and will thus become
+the most recent entry. The reference name must be fully qualified. Both the old
+and new object IDs must not be abbreviated and must point to existing objects.
+The reflog message gets normalized.
+
 The "delete" subcommand deletes single entries from the reflog, but
 not the reflog itself. Its argument must be an _exact_ entry (e.g. "`git
 reflog delete master@{2}`"). This subcommand is also typically not used
diff --git a/builtin/reflog.c b/builtin/reflog.c
index b00b3f9edc9..a1b4e022041 100644
--- a/builtin/reflog.c
+++ b/builtin/reflog.c
@@ -3,6 +3,8 @@
 #include "builtin.h"
 #include "config.h"
 #include "gettext.h"
+#include "hex.h"
+#include "object-store.h"
 #include "revision.h"
 #include "reachable.h"
 #include "wildmatch.h"
@@ -20,6 +22,9 @@
 #define BUILTIN_REFLOG_EXISTS_USAGE \
 	N_("git reflog exists <ref>")
 
+#define BUILTIN_REFLOG_WRITE_USAGE \
+	N_("git reflog write <ref> <old-oid> <new-oid> <message>")
+
 #define BUILTIN_REFLOG_DELETE_USAGE \
 	N_("git reflog delete [--rewrite] [--updateref]\n" \
 	   "                  [--dry-run | -n] [--verbose] <ref>@{<specifier>}...")
@@ -47,6 +52,11 @@ static const char *const reflog_exists_usage[] = {
 	NULL,
 };
 
+static const char *const reflog_write_usage[] = {
+	BUILTIN_REFLOG_WRITE_USAGE,
+	NULL,
+};
+
 static const char *const reflog_delete_usage[] = {
 	BUILTIN_REFLOG_DELETE_USAGE,
 	NULL
@@ -66,6 +76,7 @@ static const char *const reflog_usage[] = {
 	BUILTIN_REFLOG_SHOW_USAGE,
 	BUILTIN_REFLOG_LIST_USAGE,
 	BUILTIN_REFLOG_EXISTS_USAGE,
+	BUILTIN_REFLOG_WRITE_USAGE,
 	BUILTIN_REFLOG_DELETE_USAGE,
 	BUILTIN_REFLOG_DROP_USAGE,
 	BUILTIN_REFLOG_EXPIRE_USAGE,
@@ -392,6 +403,59 @@ static int cmd_reflog_drop(int argc, const char **argv, const char *prefix,
 	return ret;
 }
 
+static int cmd_reflog_write(int argc, const char **argv, const char *prefix,
+			    struct repository *repo)
+{
+	const struct option options[] = {
+		OPT_END()
+	};
+	struct object_id old_oid, new_oid;
+	struct strbuf err = STRBUF_INIT;
+	struct ref_transaction *tx;
+	const char *ref, *message;
+	int ret;
+
+	argc = parse_options(argc, argv, prefix, options, reflog_write_usage, 0);
+	if (argc != 4)
+		usage_with_options(reflog_write_usage, options);
+
+	ref = argv[0];
+	if (!is_root_ref(ref) && check_refname_format(ref, 0))
+		die(_("invalid reference name: %s"), ref);
+
+	ret = get_oid_hex_algop(argv[1], &old_oid, repo->hash_algo);
+	if (ret)
+		die(_("invalid old object ID: '%s'"), argv[1]);
+	if (!is_null_oid(&old_oid) && !has_object(the_repository, &old_oid, 0))
+		die(_("old object '%s' does not exist"), argv[1]);
+
+	ret = get_oid_hex_algop(argv[2], &new_oid, repo->hash_algo);
+	if (ret)
+		die(_("invalid new object ID: '%s'"), argv[2]);
+	if (!is_null_oid(&new_oid) && !has_object(the_repository, &new_oid, 0))
+		die(_("new object '%s' does not exist"), argv[2]);
+
+	message = argv[3];
+
+	tx = ref_store_transaction_begin(get_main_ref_store(repo), 0, &err);
+	if (!tx)
+		die(_("cannot start transaction: %s"), err.buf);
+
+	ret = ref_transaction_update_reflog(tx, ref, &new_oid, &old_oid,
+					    git_committer_info(0),
+					    message, 0, &err);
+	if (ret)
+		die(_("cannot queue reflog update: %s"), err.buf);
+
+	ret = ref_transaction_commit(tx, &err);
+	if (ret)
+		die(_("cannot commit reflog update: %s"), err.buf);
+
+	ref_transaction_free(tx);
+	strbuf_release(&err);
+	return 0;
+}
+
 /*
  * main "reflog"
  */
@@ -405,6 +469,7 @@ int cmd_reflog(int argc,
 		OPT_SUBCOMMAND("show", &fn, cmd_reflog_show),
 		OPT_SUBCOMMAND("list", &fn, cmd_reflog_list),
 		OPT_SUBCOMMAND("exists", &fn, cmd_reflog_exists),
+		OPT_SUBCOMMAND("write", &fn, cmd_reflog_write),
 		OPT_SUBCOMMAND("delete", &fn, cmd_reflog_delete),
 		OPT_SUBCOMMAND("drop", &fn, cmd_reflog_drop),
 		OPT_SUBCOMMAND("expire", &fn, cmd_reflog_expire),
diff --git a/t/meson.build b/t/meson.build
index d052fc3e23d..adcdf09e740 100644
--- a/t/meson.build
+++ b/t/meson.build
@@ -220,6 +220,7 @@ integration_tests = [
   't1418-reflog-exists.sh',
   't1419-exclude-refs.sh',
   't1420-lost-found.sh',
+  't1421-reflog-write.sh',
   't1430-bad-ref-name.sh',
   't1450-fsck.sh',
   't1451-fsck-buffer.sh',
diff --git a/t/t1421-reflog-write.sh b/t/t1421-reflog-write.sh
new file mode 100755
index 00000000000..6cad64f40ab
--- /dev/null
+++ b/t/t1421-reflog-write.sh
@@ -0,0 +1,126 @@
+#!/bin/sh
+
+test_description='Manually write reflog entries'
+
+. ./test-lib.sh
+
+SIGNATURE="C O Mitter <committer@example.com> 1112911993 -0700"
+
+test_reflog_matches () {
+	repo="$1" &&
+	refname="$2" &&
+	cat >actual &&
+	test-tool -C "$repo" ref-store main for-each-reflog-ent "$refname" >expected &&
+	test_cmp expected actual
+}
+
+test_expect_success 'invalid number of arguments' '
+	test_when_finished "rm -rf repo" &&
+	git init repo &&
+	(
+		cd repo &&
+		for args in "" "1" "1 2" "1 2 3" "1 2 3 4 5"
+		do
+			test_must_fail git reflog write $args 2>err &&
+			test_grep "usage: git reflog write" err || return 1
+		done
+	)
+'
+
+test_expect_success 'invalid refname' '
+	test_when_finished "rm -rf repo" &&
+	git init repo &&
+	(
+		cd repo &&
+		test_must_fail git reflog write "refs/heads/ invalid" $ZERO_OID $ZERO_OID first 2>err &&
+		test_grep "invalid reference name: " err
+	)
+'
+
+test_expect_success 'unqualified refname is rejected' '
+	test_when_finished "rm -rf repo" &&
+	git init repo &&
+	(
+		cd repo &&
+		test_must_fail git reflog write unqualified $ZERO_OID $ZERO_OID first 2>err &&
+		test_grep "invalid reference name: " err
+	)
+'
+
+test_expect_success 'nonexistent object IDs' '
+	test_when_finished "rm -rf repo" &&
+	git init repo &&
+	(
+		cd repo &&
+		test_must_fail git reflog write refs/heads/something $(test_oid deadbeef) $ZERO_OID old-object-id 2>err &&
+		test_grep "old object .* does not exist" err &&
+		test_must_fail git reflog write refs/heads/something $ZERO_OID $(test_oid deadbeef) new-object-id 2>err &&
+		test_grep "new object .* does not exist" err
+	)
+'
+
+test_expect_success 'abbreviated object IDs' '
+	test_when_finished "rm -rf repo" &&
+	git init repo &&
+	(
+		cd repo &&
+		test_must_fail git reflog write refs/heads/something 12345 $ZERO_OID old-object-id 2>err &&
+		test_grep "invalid old object ID" err &&
+		test_must_fail git reflog write refs/heads/something $ZERO_OID 12345 new-object-id 2>err &&
+		test_grep "invalid new object ID" err
+	)
+'
+
+test_expect_success 'reflog message gets normalized' '
+	test_when_finished "rm -rf repo" &&
+	git init repo &&
+	(
+		cd repo &&
+		test_commit initial &&
+		COMMIT_OID=$(git rev-parse HEAD) &&
+		git reflog write HEAD $COMMIT_OID $COMMIT_OID "$(printf "message\nwith\nnewlines")" &&
+		git reflog show -1 --format=%gs HEAD >actual &&
+		echo "message with newlines" >expected &&
+		test_cmp expected actual
+	)
+'
+
+test_expect_success 'simple writes' '
+	test_when_finished "rm -rf repo" &&
+	git init repo &&
+	(
+		cd repo &&
+		test_commit initial &&
+		COMMIT_OID=$(git rev-parse HEAD) &&
+
+		git reflog write refs/heads/something $ZERO_OID $COMMIT_OID first &&
+		test_reflog_matches . refs/heads/something <<-EOF &&
+		$ZERO_OID $COMMIT_OID $SIGNATURE	first
+		EOF
+
+		git reflog write refs/heads/something $COMMIT_OID $COMMIT_OID second &&
+		# Note: the old object ID of the second reflog entry is broken.
+		# This will be fixed in subsequent commits.
+		test_reflog_matches . refs/heads/something <<-EOF
+		$ZERO_OID $COMMIT_OID $SIGNATURE	first
+		$ZERO_OID $COMMIT_OID $SIGNATURE	second
+		EOF
+	)
+'
+
+test_expect_success 'can write to root ref' '
+	test_when_finished "rm -rf repo" &&
+	git init repo &&
+	(
+		cd repo &&
+		test_commit initial &&
+		COMMIT_OID=$(git rev-parse HEAD) &&
+
+		git reflog write ROOT_REF_HEAD $ZERO_OID $COMMIT_OID first &&
+		test_reflog_matches . ROOT_REF_HEAD <<-EOF
+		$ZERO_OID $COMMIT_OID $SIGNATURE	first
+		EOF
+	)
+'
+
+test_done
-- 
2.50.1.619.g074bbf1d35.dirty
Previous: Patrick SteinhardtNext: Junio C Hamano
Message 65 of 114 in “refs: fix migration of reflog entries”
  1. 0/8 refs: fix migration of reflog entriesPatrick Steinhardt, Jul 22, 2025
  2. 1/8 Documentation/git-reflog: convert to use synopsis typePatrick Steinhardt, Jul 22, 2025
  3. Junio C HamanoJul 22, 2025
  4. 2/8 builtin/reflog: improve grouping of subcommandsPatrick Steinhardt, Jul 22, 2025
  5. Justin ToblerJul 23, 2025
  6. Patrick SteinhardtJul 24, 2025
  7. Junio C HamanoJul 24, 2025
  8. 3/8 refs: export `ref_transaction_update_reflog()`Patrick Steinhardt, Jul 22, 2025
  9. Justin ToblerJul 23, 2025
  10. Karthik NayakJul 24, 2025
  11. Toon ClaesJul 24, 2025
  12. 4/8 builtin/reflog: implement subcommand to write new entriesPatrick Steinhardt, Jul 22, 2025
  13. Justin ToblerJul 23, 2025
  14. Patrick SteinhardtJul 24, 2025
  15. Toon ClaesJul 24, 2025
  16. Patrick SteinhardtJul 25, 2025
  17. SZEDER GáborJul 24, 2025
  18. Junio C HamanoJul 24, 2025
  19. Patrick SteinhardtJul 25, 2025
  20. Junio C HamanoJul 25, 2025
  21. 5/8 ident: fix type of string length parameterPatrick Steinhardt, Jul 22, 2025
  22. 6/8 refs: fix identity for migrated reflogsPatrick Steinhardt, Jul 22, 2025
  23. Justin ToblerJul 23, 2025
  24. Patrick SteinhardtJul 24, 2025
  25. Karthik NayakJul 24, 2025
  26. Toon ClaesJul 24, 2025
  27. 7/8 refs: stop unsetting REF_HAVE_OLD for log-only updatesPatrick Steinhardt, Jul 22, 2025
  28. Justin ToblerJul 23, 2025
  29. Patrick SteinhardtJul 24, 2025
  30. Karthik NayakJul 24, 2025
  31. Patrick SteinhardtJul 24, 2025
  32. 8/8 refs: fix invalid old object IDs when migrating reflogsPatrick Steinhardt, Jul 22, 2025
  33. Junio C HamanoJul 22, 2025
  34. Patrick SteinhardtJul 23, 2025
  35. 0/8 refs: fix migration of reflog entriesPatrick Steinhardt, Jul 25, 2025
  36. 1/8 Documentation/git-reflog: convert to use synopsis typePatrick Steinhardt, Jul 25, 2025
  37. 2/8 builtin/reflog: improve grouping of subcommandsPatrick Steinhardt, Jul 25, 2025
  38. 3/8 refs: export `ref_transaction_update_reflog()`Patrick Steinhardt, Jul 25, 2025
  39. 4/8 builtin/reflog: implement subcommand to write new entriesPatrick Steinhardt, Jul 25, 2025
  40. Kristoffer HaugsbakkJul 28, 2025
  41. Junio C HamanoJul 28, 2025
  42. Karthik NayakJul 28, 2025
  43. Junio C HamanoJul 28, 2025
  44. Karthik NayakJul 30, 2025
  45. Ben KnobleJul 29, 2025
  46. Kristoffer HaugsbakkJul 29, 2025
  47. Patrick SteinhardtJul 29, 2025
  48. Junio C HamanoJul 29, 2025
  49. Patrick SteinhardtJul 30, 2025
  50. Patrick SteinhardtJul 30, 2025
  51. 5/8 ident: fix type of string length parameterPatrick Steinhardt, Jul 25, 2025
  52. 6/8 refs: fix identity for migrated reflogsPatrick Steinhardt, Jul 25, 2025
  53. 7/8 refs: stop unsetting REF_HAVE_OLD for log-only updatesPatrick Steinhardt, Jul 25, 2025
  54. Jeff KingJul 25, 2025
  55. Patrick SteinhardtJul 28, 2025
  56. Jeff KingJul 29, 2025
  57. Patrick SteinhardtJul 29, 2025
  58. 8/8 refs: fix invalid old object IDs when migrating reflogsPatrick Steinhardt, Jul 25, 2025
  59. 0/9 refs: fix migration of reflog entriesPatrick Steinhardt, Jul 29, 2025
  60. 1/9 Documentation/git-reflog: convert to use synopsis typePatrick Steinhardt, Jul 29, 2025
  61. 2/9 builtin/reflog: improve grouping of subcommandsPatrick Steinhardt, Jul 29, 2025
  62. 3/9 refs: export `ref_transaction_update_reflog()`Patrick Steinhardt, Jul 29, 2025
  63. Toon ClaesAug 1, 2025
  64. Patrick SteinhardtAug 4, 2025
  65. 4/9 builtin/reflog: implement subcommand to write new entriesPatrick Steinhardt, Jul 29, 2025
  66. Junio C HamanoJul 29, 2025
  67. Toon ClaesAug 1, 2025
  68. Patrick SteinhardtAug 4, 2025
  69. 5/9 ident: fix type of string length parameterPatrick Steinhardt, Jul 29, 2025
  70. 6/9 refs: fix identity for migrated reflogsPatrick Steinhardt, Jul 29, 2025
  71. 7/9 refs/files: detect race when generating reflog entry for HEADPatrick Steinhardt, Jul 29, 2025
  72. Junio C HamanoJul 29, 2025
  73. Toon ClaesAug 1, 2025
  74. Jeff KingAug 2, 2025
  75. Patrick SteinhardtAug 4, 2025
  76. Jeff KingAug 4, 2025
  77. 8/9 refs: stop unsetting REF_HAVE_OLD for log-only updatesPatrick Steinhardt, Jul 29, 2025
  78. 9/9 refs: fix invalid old object IDs when migrating reflogsPatrick Steinhardt, Jul 29, 2025
  79. 0/9 refs: fix migration of reflog entriesPatrick Steinhardt, Aug 4, 2025
  80. 1/9 Documentation/git-reflog: convert to use synopsis typePatrick Steinhardt, Aug 4, 2025
  81. 2/9 builtin/reflog: improve grouping of subcommandsPatrick Steinhardt, Aug 4, 2025
  82. 3/9 refs: export `ref_transaction_update_reflog()`Patrick Steinhardt, Aug 4, 2025
  83. 4/9 builtin/reflog: implement subcommand to write new entriesPatrick Steinhardt, Aug 4, 2025
  84. 5/9 ident: fix type of string length parameterPatrick Steinhardt, Aug 4, 2025
  85. 6/9 refs: fix identity for migrated reflogsPatrick Steinhardt, Aug 4, 2025
  86. 7/9 refs/files: detect race when generating reflog entry for HEADPatrick Steinhardt, Aug 4, 2025
  87. Jeff KingAug 4, 2025
  88. 8/9 refs: stop unsetting REF_HAVE_OLD for log-only updatesPatrick Steinhardt, Aug 4, 2025
  89. 9/9 refs: fix invalid old object IDs when migrating reflogsPatrick Steinhardt, Aug 4, 2025
  90. 0/9 refs: fix migration of reflog entriesPatrick Steinhardt, Aug 5, 2025
  91. 1/9 Documentation/git-reflog: convert to use synopsis typePatrick Steinhardt, Aug 5, 2025
  92. Jean-Noël AVILAAug 5, 2025
  93. Junio C HamanoAug 5, 2025
  94. Patrick SteinhardtAug 6, 2025
  95. 2/9 builtin/reflog: improve grouping of subcommandsPatrick Steinhardt, Aug 5, 2025
  96. 3/9 refs: export `ref_transaction_update_reflog()`Patrick Steinhardt, Aug 5, 2025
  97. 4/9 builtin/reflog: implement subcommand to write new entriesPatrick Steinhardt, Aug 5, 2025
  98. 5/9 ident: fix type of string length parameterPatrick Steinhardt, Aug 5, 2025
  99. 6/9 refs: fix identity for migrated reflogsPatrick Steinhardt, Aug 5, 2025
  100. 7/9 refs/files: detect race when generating reflog entry for HEADPatrick Steinhardt, Aug 5, 2025
  101. 8/9 refs: stop unsetting REF_HAVE_OLD for log-only updatesPatrick Steinhardt, Aug 5, 2025
  102. 9/9 refs: fix invalid old object IDs when migrating reflogsPatrick Steinhardt, Aug 5, 2025
  103. Jeff KingAug 5, 2025
  104. Patrick SteinhardtAug 6, 2025
  105. 0/9 refs: fix migration of reflog entriesPatrick Steinhardt, Aug 6, 2025
  106. 1/9 Documentation/git-reflog: convert to use synopsis typePatrick Steinhardt, Aug 6, 2025
  107. 2/9 builtin/reflog: improve grouping of subcommandsPatrick Steinhardt, Aug 6, 2025
  108. 3/9 refs: export `ref_transaction_update_reflog()`Patrick Steinhardt, Aug 6, 2025
  109. 4/9 builtin/reflog: implement subcommand to write new entriesPatrick Steinhardt, Aug 6, 2025
  110. 5/9 ident: fix type of string length parameterPatrick Steinhardt, Aug 6, 2025
  111. 6/9 refs: fix identity for migrated reflogsPatrick Steinhardt, Aug 6, 2025
  112. 7/9 refs/files: detect race when generating reflog entry for HEADPatrick Steinhardt, Aug 6, 2025
  113. 8/9 refs: stop unsetting REF_HAVE_OLD for log-only updatesPatrick Steinhardt, Aug 6, 2025
  114. 9/9 refs: fix invalid old object IDs when migrating reflogsPatrick Steinhardt, Aug 6, 2025

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.