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

Re: [PATCH v3 4/4] stash: honor stash.index in apply, pop modes

From
PWPhillip Wood <phillip.wood123@gmail.com>
Date
Sep 22, 2025, 14:11 UTC
Message-ID
<d7721018-3b2f-4764-b33d-4fc6bdc034a1@gmail.com>
In-Reply-To
<8e6cafbf3a01b968663b65559acf3df615eecbad.1758505011.git.ben.knoble+github@gmail.com>
Hi Ben
On 22/09/2025 02:39, D. Ben Knoble wrote:
Show 7 quoted lines
> With stash.index=true, git-stash(1) command now tries to reinstate the
> index by default in the "apply" and "pop" modes. Not doing so creates a
> common trap [1], [2]: "git stash apply" is not the reverse of "git stash
> push" because carefully staged indices are lost and have to be manually
> recreated. OTOH, this mode is not always desirable and may create more
> conflicts when applying stashes. As usual, "--no-index" will disable
> this behavior if you set "stash.index".

Thanks for updating the tests, they look good. As I said before I don't have a strong opinion about this change but I certainly don't object to it. I think this change will also affect the behavior of "git merge/pull/rebase --autostash" which we should maybe call out in the commit message. I don't think that change in behavior is a problem as it is probably what the user would expect when they set this config.

Thanks
Phillip
Show 110 quoted lines
> [1]: https://lore.kernel.org/git/CAPx1GvcxyDDQmCssMjEnt6JoV6qPc5ZUpgPLX3mpUC_4PNYA1w@mail.gmail.com/
> [2]: https://lore.kernel.org/git/c5a811ac-8cd3-c389-ac6d-29020a648c87@gmail.com/
> 
> Signed-off-by: D. Ben Knoble <ben.knoble+github@gmail.com>
> ---
>   Documentation/config/stash.adoc |  5 +++++
>   builtin/stash.c                 |  9 ++++++--
>   t/t3903-stash.sh                | 37 +++++++++++++++++++++++++++++++++
>   3 files changed, 49 insertions(+), 2 deletions(-)
> 
> diff --git a/Documentation/config/stash.adoc b/Documentation/config/stash.adoc
> index ec1edaeba6..e556105a15 100644
> --- a/Documentation/config/stash.adoc
> +++ b/Documentation/config/stash.adoc
> @@ -1,3 +1,8 @@
> +stash.index::
> +	If this is set to true, `git stash apply` and `git stash pop` will
> +	behave as if `--index` was supplied. Defaults to false. See the
> +	descriptions in linkgit:git-stash[1].
> +
>   stash.showIncludeUntracked::
>   	If this is set to true, the `git stash show` command will show
>   	the untracked files of a stash entry.  Defaults to false. See
> diff --git a/builtin/stash.c b/builtin/stash.c
> index d9b478d1d1..8a0eef3c70 100644
> --- a/builtin/stash.c
> +++ b/builtin/stash.c
> @@ -130,6 +130,7 @@ static struct strbuf stash_index_path = STRBUF_INIT;
>   static int show_stat = 1;
>   static int show_patch;
>   static int show_include_untracked;
> +static int use_index;
>   
>   /*
>    * w_commit is set to the commit containing the working tree
> @@ -662,7 +663,7 @@ static int apply_stash(int argc, const char **argv, const char *prefix,
>   {
>   	int ret = -1;
>   	int quiet = 0;
> -	int index = 0;
> +	int index = use_index;
>   	struct stash_info info = STASH_INFO_INIT;
>   	struct option options[] = {
>   		OPT__QUIET(&quiet, N_("be quiet, only report errors")),
> @@ -759,7 +760,7 @@ static int pop_stash(int argc, const char **argv, const char *prefix,
>   		     struct repository *repo UNUSED)
>   {
>   	int ret = -1;
> -	int index = 0;
> +	int index = use_index;
>   	int quiet = 0;
>   	struct stash_info info = STASH_INFO_INIT;
>   	struct option options[] = {
> @@ -864,6 +865,10 @@ static int git_stash_config(const char *var, const char *value,
>   		show_include_untracked = git_config_bool(var, value);
>   		return 0;
>   	}
> +	if (!strcmp(var, "stash.index")) {
> +		use_index = git_config_bool(var, value);
> +		return 0;
> +	}
>   	return git_diff_basic_config(var, value, ctx, cb);
>   }
>   
> diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
> index b8936a653b..d6127173b1 100755
> --- a/t/t3903-stash.sh
> +++ b/t/t3903-stash.sh
> @@ -1595,4 +1595,41 @@ setup_stash()
>   	)
>   '
>   
> +test_expect_success 'stash.index=true implies --index' '
> +	# setup for a few related tests
> +	test_commit file base &&
> +	echo index >file &&
> +	git add file &&
> +	echo working >file &&
> +	git stash &&
> +
> +	test_when_finished "git reset --hard" &&
> +	git -c stash.index=true stash apply &&
> +	echo index >expect &&
> +	git show :0:file >actual &&
> +	test_cmp expect actual &&
> +	echo working >expect &&
> +	test_cmp expect file
> +'
> +
> +test_expect_success 'stash.index=true overridden by --no-index' '
> +	test_when_finished "git reset --hard" &&
> +	git -c stash.index=true stash apply --no-index &&
> +	echo base >expect &&
> +	git show :0:file >actual &&
> +	test_cmp expect actual &&
> +	echo working >expect &&
> +	test_cmp expect file
> +'
> +
> +test_expect_success 'stash.index=false overridden by --index' '
> +	test_when_finished "git reset --hard" &&
> +	git -c stash.index=false stash apply --index &&
> +	echo index >expect &&
> +	git show :0:file >actual &&
> +	test_cmp expect actual &&
> +	echo working >expect &&
> +	test_cmp expect file
> +'
> +
>   test_done
Previous: D. Ben KnobleNext: D. Ben Knoble
Message 29 of 38 in “make stash apply with --index by default”
  1. 0/9 make stash apply with --index by defaultD. Ben Knoble, May 10, 2025
  2. 1/9 t3903: reduce dependencies on previous testsD. Ben Knoble, May 10, 2025
  3. 2/9 t3905: remove unneeded blank lineD. Ben Knoble, May 10, 2025
  4. 3/9 BreakingChanges: announce stash {apply,pop} will imply --indexD. Ben Knoble, May 10, 2025
  5. 4/9 stash: restore the index by default when breaking changes are enabledD. Ben Knoble, May 10, 2025
  6. 5/9 t0450: mark stash documentation as a known discrepancyD. Ben Knoble, May 10, 2025
  7. 6/9 t3903: adjust stash test to account for --[no-]index with breaking changesD. Ben Knoble, May 10, 2025
  8. 7/9 t3904: adjust stash -p test to account for index states with breaking changesD. Ben Knoble, May 10, 2025
  9. 8/9 t3905: adjust stash -u tests for breaking changesD. Ben Knoble, May 10, 2025
  10. 9/9 t3906: adjust stash submodule tests to account for breaking changesD. Ben Knoble, May 10, 2025
  11. Junio C HamanoMay 12, 2025
  12. D. Ben KnobleMay 20, 2025
  13. D. Ben KnobleMay 20, 2025
  14. 0/4 Teach git-stash to use --index from configD. Ben Knoble, Sep 16, 2025
  15. 1/4 t3903: reduce dependencies on previous testsD. Ben Knoble, Sep 16, 2025
  16. 2/4 t3905: remove unneeded blank lineD. Ben Knoble, Sep 16, 2025
  17. 3/4 stash: refactor private config globalsD. Ben Knoble, Sep 16, 2025
  18. 4/4 stash: honor stash.index in apply, pop modesD. Ben Knoble, Sep 16, 2025
  19. Phillip WoodSep 16, 2025
  20. D. Ben KnobleSep 16, 2025
  21. Phillip WoodSep 16, 2025
  22. D. Ben KnobleSep 16, 2025
  23. Junio C HamanoSep 16, 2025
  24. 0/4 Teach git-stash to use --index from configD. Ben Knoble, Sep 22, 2025
  25. 1/4 t3903: reduce dependencies on previous testsD. Ben Knoble, Sep 22, 2025
  26. 2/4 t3905: remove unneeded blank lineD. Ben Knoble, Sep 22, 2025
  27. 3/4 stash: refactor private config globalsD. Ben Knoble, Sep 22, 2025
  28. 4/4 stash: honor stash.index in apply, pop modesD. Ben Knoble, Sep 22, 2025
  29. Phillip WoodSep 22, 2025
  30. D. Ben KnobleSep 24, 2025
  31. Phillip WoodSep 29, 2025
  32. doc: explain the impact of stash.index on --autostash optionsD. Ben Knoble, Oct 6, 2025
  33. Kristoffer HaugsbakkOct 9, 2025
  34. D. Ben KnobleOct 11, 2025
  35. Junio C HamanoOct 11, 2025
  36. Ben KnobleOct 12, 2025
  37. Kristoffer HaugsbakkOct 13, 2025
  38. D. Ben KnobleOct 14, 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.