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

[PATCH v5 3/4] builtin/stash: provide a way to export stashes to a ref

From
brian m. carlson <sandals@crustytoothpaste.net>
Date
May 8, 2025, 23:44 UTC
Message-ID
<20250508234458.3665894-4-sandals@crustytoothpaste.net>
In-Reply-To
<20250508234458.3665894-1-sandals@crustytoothpaste.net>

A common user problem is how to sync in-progress work to another machine. Users currently must use some sort of transfer of the working tree, which poses security risks and also necessarily causes the index to become dirty. The experience is suboptimal and frustrating for users.

A reasonable idea is to use the stash for this purpose, but the stash is stored in the reflog, not in a ref, and as such it cannot be pushed or pulled. This also means that it cannot be saved into a bundle or preserved elsewhere, which is a problem when using throwaway development environments.

Let's solve this problem by allowing the user to export the stash to a ref (or, to just write it into the repository and print the hash, à la git commit-tree). Introduce git stash export, which writes a chain of commits where the first parent is always a chain to the previous stash, or to a single, empty commit (for the final item) and the second is the stash commit normally written to the reflog.

Iterate over each stash from topmost to bottomost, looking up the data for each one, and then create the chain from the single empty commit back up in reverse order. Generate a predictable empty commit so our behavior is reproducible. Create a useful commit message, preserving the author and committer information, to help users identify stash commits when viewing them as normal commits.

If the user has specified specific stashes they'd like to export instead, use those instead of iterating over all of the stashes.

As part of this, specifically request quiet behavior when looking up the OID for a revision because we will eventually hit a revision that doesn't exist and we don't want to die when that occurs.

Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net>
---
 Documentation/git-stash.adoc |  22 +++-
 builtin/stash.c              | 202 +++++++++++++++++++++++++++++++++++
 2 files changed, 223 insertions(+), 1 deletion(-)
diff --git a/Documentation/git-stash.adoc b/Documentation/git-stash.adoc
index 1a5177f498..e8efd43ba4 100644
--- a/Documentation/git-stash.adoc
+++ b/Documentation/git-stash.adoc
@@ -23,6 +23,7 @@ SYNOPSIS
 'git stash' clear
 'git stash' create [<message>]
 'git stash' store [(-m | --message) <message>] [-q | --quiet] <commit>
+'git stash' export (--print | --to-ref <ref>) [<stash>...]
 
 DESCRIPTION
 -----------
@@ -154,6 +155,12 @@ store::
 	reflog.  This is intended to be useful for scripts.  It is
 	probably not the command you want to use; see "push" above.
 
+export ( --print | --to-ref <ref> ) [<stash>...]::
+
+	Export the specified stashes, or all of them if none are specified, to
+	a chain of commits which can be transferred using the normal fetch and
+	push mechanisms, then imported using the `import` subcommand.
+
 OPTIONS
 -------
 -a::
@@ -242,6 +249,19 @@ literally (including newlines and quotes).
 +
 Quiet, suppress feedback messages.
 
+--print::
+	This option is only valid for `export`.
++
+Create the chain of commits representing the exported stashes without
+storing it anywhere in the ref namespace and print the object ID to
+standard output.  This is designed for scripts.
+
+--to-ref::
+	This option is only valid for `export`.
++
+Create the chain of commits representing the exported stashes and store
+it to the specified ref.
+
 \--::
 	This option is only valid for `push` command.
 +
@@ -259,7 +279,7 @@ For more details, see the 'pathspec' entry in linkgit:gitglossary[7].
 
 <stash>::
 	This option is only valid for `apply`, `branch`, `drop`, `pop`,
-	`show` commands.
+	`show`, and `export` commands.
 +
 A reference of the form `stash@{<revision>}`. When no `<stash>` is
 given, the latest stash is assumed (that is, `stash@{0}`).
diff --git a/builtin/stash.c b/builtin/stash.c
index 8ee6752efa..4a42126d02 100644
--- a/builtin/stash.c
+++ b/builtin/stash.c
@@ -29,6 +29,8 @@
 #include "diffcore.h"
 #include "reflog.h"
 #include "add-interactive.h"
+#include "oid-array.h"
+#include "commit.h"
 
 #define INCLUDE_ALL_FILES 2
 
@@ -56,6 +58,8 @@
 	   "          [-u | --include-untracked] [-a | --all] [<message>]")
 #define BUILTIN_STASH_CREATE_USAGE \
 	N_("git stash create [<message>]")
+#define BUILTIN_STASH_EXPORT_USAGE \
+	N_("git stash export (--print | --to-ref <ref>) [<stash>...]")
 #define BUILTIN_STASH_CLEAR_USAGE \
 	"git stash clear"
 
@@ -71,6 +75,7 @@ static const char * const git_stash_usage[] = {
 	BUILTIN_STASH_CLEAR_USAGE,
 	BUILTIN_STASH_CREATE_USAGE,
 	BUILTIN_STASH_STORE_USAGE,
+	BUILTIN_STASH_EXPORT_USAGE,
 	NULL
 };
 
@@ -124,6 +129,12 @@ static const char * const git_stash_save_usage[] = {
 	NULL
 };
 
+static const char * const git_stash_export_usage[] = {
+	BUILTIN_STASH_EXPORT_USAGE,
+	NULL
+};
+
+
 static const char ref_stash[] = "refs/stash";
 static struct strbuf stash_index_path = STRBUF_INIT;
 
@@ -1896,6 +1907,196 @@ static int save_stash(int argc, const char **argv, const char *prefix,
 	return ret;
 }
 
+static int write_commit_with_parents(struct repository *r,
+				     struct object_id *out,
+				     const struct object_id *oid,
+				     struct commit_list *parents)
+{
+	size_t author_len, committer_len;
+	struct commit *this;
+	const char *orig_author, *orig_committer;
+	char *author = NULL, *committer = NULL;
+	const char *buffer;
+	unsigned long bufsize;
+	const char *p;
+	struct strbuf msg = STRBUF_INIT;
+	int ret = 0;
+	struct ident_split id;
+
+	this = lookup_commit_reference(r, oid);
+	buffer = repo_get_commit_buffer(r, this, &bufsize);
+	orig_author = find_commit_header(buffer, "author", &author_len);
+	orig_committer = find_commit_header(buffer, "committer", &committer_len);
+	if (split_ident_line(&id, orig_author, author_len) < 0 ||
+	    split_ident_line(&id, orig_committer, committer_len) < 0) {
+		ret = error(_("invalid author or committer for %s"), oid_to_hex(oid));
+		goto out;
+	}
+	p = strstr(buffer, "\n\n");
+
+	if (!orig_author || !orig_committer || !p) {
+		ret = error(_("cannot parse commit %s"), oid_to_hex(oid));
+		goto out;
+	}
+	/* Jump to message. */
+	p += 2;
+	strbuf_addstr(&msg, "git stash: ");
+	strbuf_add(&msg, p, bufsize - (p - buffer));
+
+	author = xmemdupz(orig_author, author_len);
+	committer = xmemdupz(orig_committer, committer_len);
+
+	if (commit_tree_extended(msg.buf, msg.len,
+				 r->hash_algo->empty_tree, parents,
+				 out, author, committer,
+				 NULL, NULL)) {
+		ret = error(_("could not write commit"));
+		goto out;
+	}
+out:
+	strbuf_release(&msg);
+	repo_unuse_commit_buffer(r, this, buffer);
+	free_commit_list(parents);
+	free(author);
+	free(committer);
+	return ret;
+}
+
+static int do_export_stash(struct repository *r,
+			   const char *ref,
+			   int argc,
+			   const char **argv)
+{
+	struct object_id base;
+	struct object_context unused;
+	struct commit *prev;
+	struct oid_array items = OID_ARRAY_INIT;
+	int res = 0;
+	int i;
+	struct strbuf revision = STRBUF_INIT;
+	const char *author, *committer;
+
+	/*
+	 * This is an arbitrary, fixed date, specifically the one used by git
+	 * format-patch.  The goal is merely to produce reproducible output.
+	 */
+	prepare_fallback_ident("git stash", "git@stash");
+	author = fmt_ident("git stash", "git@stash", WANT_BLANK_IDENT,
+			   "2001-09-17T00:00:00Z", 0);
+	committer = fmt_ident("git stash", "git@stash", WANT_BLANK_IDENT,
+			      "2001-09-17T00:00:00Z", 0);
+
+	/* First, we create a single empty commit. */
+	if (commit_tree_extended("", 0, r->hash_algo->empty_tree, NULL,
+				 &base, author, committer, NULL, NULL))
+		return error(_("unable to write base commit"));
+
+	prev = lookup_commit_reference(r, &base);
+
+	if (argc) {
+		/*
+		 * Find each specified stash, and load data into the array.
+		 */
+		for (i = 0; i < argc; i++) {
+			struct object_id oid;
+			if (parse_revision(&revision, argv[i], 1) ||
+			    get_oid_with_context(r, revision.buf,
+						 GET_OID_QUIETLY | GET_OID_GENTLY,
+						 &oid, &unused)) {
+				res = error(_("unable to find stash entry %s"), argv[i]);
+				goto out;
+			}
+			oid_array_append(&items, &oid);
+		}
+	} else {
+		/*
+		 * Walk the reflog, finding each stash entry, and load data into the
+		 * array.
+		 */
+		for (i = 0;; i++) {
+			char buf[32];
+			struct object_id oid;
+
+			snprintf(buf, sizeof(buf), "%d", i);
+			if (parse_revision(&revision, buf, 1) ||
+			    get_oid_with_context(r, revision.buf,
+						 GET_OID_QUIETLY | GET_OID_GENTLY,
+						 &oid, &unused))
+				break;
+			oid_array_append(&items, &oid);
+		}
+	}
+
+	/*
+	 * Now, create a set of commits identical to the regular stash commits,
+	 * but where their first parents form a chain to our original empty
+	 * base commit.
+	 */
+	for (i = items.nr - 1; i >= 0; i--) {
+		struct commit_list *parents = NULL;
+		struct commit_list **next = &parents;
+		struct object_id out;
+		const struct object_id *oid = items.oid + i;
+
+		next = commit_list_append(prev, next);
+		next = commit_list_append(lookup_commit_reference(r, oid), next);
+		res = write_commit_with_parents(r, &out, oid, parents);
+		if (res)
+			goto out;
+		prev = lookup_commit_reference(r, &out);
+	}
+	if (ref)
+		refs_update_ref(get_main_ref_store(r), NULL, ref,
+				&prev->object.oid, NULL, 0, UPDATE_REFS_DIE_ON_ERR);
+	else
+		puts(oid_to_hex(&prev->object.oid));
+out:
+	strbuf_release(&revision);
+	oid_array_clear(&items);
+
+	return res;
+}
+
+enum export_action {
+	ACTION_NONE,
+	ACTION_PRINT,
+	ACTION_TO_REF,
+};
+
+static int export_stash(int argc,
+			const char **argv,
+			const char *prefix,
+			struct repository *repo)
+{
+	const char *ref = NULL;
+	enum export_action action = ACTION_NONE;
+	struct option options[] = {
+		OPT_CMDMODE(0, "print", &action,
+			    N_("print the object ID instead of writing it to a ref"),
+			    ACTION_PRINT),
+		OPT_CMDMODE(0, "to-ref", &action,
+			    N_("save the data to the given ref"),
+			    ACTION_TO_REF),
+		OPT_END()
+	};
+
+	argc = parse_options(argc, argv, prefix, options,
+			     git_stash_export_usage,
+			     PARSE_OPT_KEEP_DASHDASH);
+
+	if (action == ACTION_NONE) {
+		return error(_("exactly one of --print and --to-ref is required"));
+	} else if (action == ACTION_TO_REF) {
+		if (!argc)
+			return error(_("--to-ref requires an argument"));
+		ref = argv[0];
+		argc--;
+		argv++;
+	}
+
+	return do_export_stash(repo, ref, argc, argv);
+}
+
 int cmd_stash(int argc,
 	      const char **argv,
 	      const char *prefix,
@@ -1916,6 +2117,7 @@ int cmd_stash(int argc,
 		OPT_SUBCOMMAND("store", &fn, store_stash),
 		OPT_SUBCOMMAND("create", &fn, create_stash),
 		OPT_SUBCOMMAND("push", &fn, push_stash_unassumed),
+		OPT_SUBCOMMAND("export", &fn, export_stash),
 		OPT_SUBCOMMAND_F("save", &fn, save_stash, PARSE_OPT_NOCOMPLETE),
 		OPT_END()
 	};
Previous: Junio C HamanoNext: Junio C Hamano
Message 4 of 72 in “Importing and exporting stashes to refs”
  1. 0/4 Importing and exporting stashes to refsbrian m. carlson, May 8, 2025
  2. 2/4 builtin/stash: factor out revision parsing into a functionbrian m. carlson, May 8, 2025
  3. Junio C HamanoMay 9, 2025
  4. 3/4 builtin/stash: provide a way to export stashes to a refbrian m. carlson, May 8, 2025
  5. Junio C HamanoMay 9, 2025
  6. Junio C HamanoMay 9, 2025
  7. Jeff KingMay 10, 2025
  8. Phillip WoodMay 12, 2025
  9. Junio C HamanoMay 12, 2025
  10. 1/4 object-name: make get_oid quietly return an errorbrian m. carlson, May 8, 2025
  11. Junio C HamanoMay 9, 2025
  12. brian m. carlsonMay 9, 2025
  13. 4/4 builtin/stash: provide a way to import stashes from a refbrian m. carlson, May 8, 2025
  14. Junio C HamanoMay 9, 2025
  15. brian m. carlsonMay 11, 2025
  16. Jeff KingMay 10, 2025
  17. Junio C HamanoMay 12, 2025
  18. Jeff KingMay 12, 2025
  19. Junio C HamanoMay 12, 2025
  20. brian m. carlsonMay 12, 2025
  21. Jeff KingMay 10, 2025
  22. Phillip WoodMay 12, 2025
  23. Junio C HamanoMay 9, 2025
  24. brian m. carlsonMay 9, 2025
  25. D. Ben KnobleMay 9, 2025
  26. brian m. carlsonMay 9, 2025
  27. D. Ben KnobleMay 10, 2025
  28. Makefile: avoid constant rebuilds with compilation databasebrian m. carlson, May 22, 2025
  29. 3/5 builtin/stash: factor out revision parsing into a functionbrian m. carlson, May 22, 2025
  30. Junio C HamanoMay 22, 2025
  31. brian m. carlsonMay 23, 2025
  32. Junio C HamanoMay 24, 2025
  33. brian m. carlsonMay 26, 2025
  34. 2/5 reflog-walk: expose read_complete_reflogbrian m. carlson, May 22, 2025
  35. Ramsay JonesMay 22, 2025
  36. brian m. carlsonMay 23, 2025
  37. Ramsay JonesMay 24, 2025
  38. brian m. carlsonMay 26, 2025
  39. Phillip WoodMay 29, 2025
  40. Junio C HamanoMay 29, 2025
  41. 0/5 Importing and exporting stashes to refsbrian m. carlson, May 22, 2025
  42. 0/4 Importing and exporting stashes to refsbrian m. carlson, Jun 1, 2025
  43. 1/4 object-name: make get_oid quietly return an errorbrian m. carlson, Jun 1, 2025
  44. 2/4 builtin/stash: factor out revision parsing into a functionbrian m. carlson, Jun 1, 2025
  45. 4/4 builtin/stash: provide a way to import stashes from a refbrian m. carlson, Jun 1, 2025
  46. 3/4 builtin/stash: provide a way to export stashes to a refbrian m. carlson, Jun 1, 2025
  47. Phillip WoodJun 5, 2025
  48. Kristoffer HaugsbakkJun 11, 2025
  49. brian m. carlsonJun 11, 2025
  50. Phillip WoodJun 5, 2025
  51. 0/4 Importing and exporting stashes to refsbrian m. carlson, Jun 12, 2025
  52. 2/4 builtin/stash: factor out revision parsing into a functionbrian m. carlson, Jun 12, 2025
  53. 1/4 object-name: make get_oid quietly return an errorbrian m. carlson, Jun 12, 2025
  54. 3/4 builtin/stash: provide a way to export stashes to a refbrian m. carlson, Jun 12, 2025
  55. 4/4 builtin/stash: provide a way to import stashes from a refbrian m. carlson, Jun 12, 2025
  56. Phillip WoodJun 25, 2025
  57. Junio C HamanoJun 25, 2025
  58. 1/5 object-name: make get_oid quietly return an errorbrian m. carlson, May 22, 2025
  59. Junio C HamanoMay 22, 2025
  60. 5/5 builtin/stash: provide a way to import stashes from a refbrian m. carlson, May 22, 2025
  61. Junio C HamanoMay 22, 2025
  62. brian m. carlsonMay 26, 2025
  63. Junio C HamanoMay 22, 2025
  64. Phillip WoodMay 23, 2025
  65. 4/5 builtin/stash: provide a way to export stashes to a refbrian m. carlson, May 22, 2025
  66. Junio C HamanoMay 22, 2025
  67. brian m. carlsonMay 26, 2025
  68. Phillip WoodMay 29, 2025
  69. brian m. carlsonMay 22, 2025
  70. Junio C HamanoMay 22, 2025
  71. Kristoffer HaugsbakkJun 11, 2025
  72. brian m. carlsonJun 12, 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.