interactive -p: add new `--auto-advance` flag

When using the interactive add, reset, stash or checkout machinery, we do not have the option of reworking with a file when selecting hunks, because the session automatically advances to the next file or ends if we have just one file. Introduce the flag `--auto-advance` which auto advances by default, when interactively selecting patches with the '--patch' option. However, the `--no-auto-advance` option does not auto advance, thereby allowing users the option to rework with files. Signed-off-by: Abraham Samuel Adekunle <abrahamadekunle50@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Abraham Samuel Adekunle committed Feb 14, 2026 at 12:03 UTC 06c81a1118a6c9a77505b0ae3092576fa4c01763
7 files changed +29 -1
add-interactive.c
+2
@@ -64,6 +64,7 @@ void init_add_i_state(struct add_i_state *s, struct repository *r,
64 s->r = r;
65 s->context = -1;
66 s->interhunkcontext = -1;
67 + s->auto_advance = add_p_opt->auto_advance;
68
69 s->use_color_interactive = check_color_config(r, "color.interactive");
70
@@ -1017,6 +1018,7 @@ static int run_patch(struct add_i_state *s, const struct pathspec *ps,
1018 struct add_p_opt add_p_opt = {
1019 .context = s->context,
1020 .interhunkcontext = s->interhunkcontext,
1021 + .auto_advance = s->auto_advance
1022 };
1023 struct strvec args = STRVEC_INIT;
1024 struct pathspec ps_selected = { 0 };
add-interactive.h
+3 -1
@@ -6,9 +6,10 @@
6 struct add_p_opt {
7 int context;
8 int interhunkcontext;
9 + int auto_advance;
10 };
11
11 -#define ADD_P_OPT_INIT { .context = -1, .interhunkcontext = -1 }
12 +#define ADD_P_OPT_INIT { .context = -1, .interhunkcontext = -1, .auto_advance = 1 }
13
14 struct add_i_state {
15 struct repository *r;
@@ -29,6 +30,7 @@ struct add_i_state {
30 int use_single_key;
31 char *interactive_diff_filter, *interactive_diff_algorithm;
32 int context, interhunkcontext;
33 + int auto_advance;
34 };
35
36 void init_add_i_state(struct add_i_state *s, struct repository *r,
builtin/add.c
+4
@@ -256,6 +256,8 @@ static struct option builtin_add_options[] = {
256 OPT_GROUP(""),
257 OPT_BOOL('i', "interactive", &add_interactive, N_("interactive picking")),
258 OPT_BOOL('p', "patch", &patch_interactive, N_("select hunks interactively")),
259 + OPT_BOOL(0, "auto-advance", &add_p_opt.auto_advance,
260 + N_("auto advance to the next file when selecting hunks interactively")),
261 OPT_DIFF_UNIFIED(&add_p_opt.context),
262 OPT_DIFF_INTERHUNK_CONTEXT(&add_p_opt.interhunkcontext),
263 OPT_BOOL('e', "edit", &edit_interactive, N_("edit current diff and apply")),
@@ -418,6 +420,8 @@ int cmd_add(int argc,
420 die(_("the option '%s' requires '%s'"), "--unified", "--interactive/--patch");
421 if (add_p_opt.interhunkcontext != -1)
422 die(_("the option '%s' requires '%s'"), "--inter-hunk-context", "--interactive/--patch");
423 + if (!add_p_opt.auto_advance)
424 + die(_("the option '%s' requires '%s'"), "--no-auto-advance", "--interactive/--patch");
425 }
426
427 if (edit_interactive) {
builtin/checkout.c
+7
@@ -63,6 +63,7 @@ struct checkout_opts {
63 int patch_mode;
64 int patch_context;
65 int patch_interhunk_context;
66 + int auto_advance;
67 int quiet;
68 int merge;
69 int force;
@@ -111,6 +112,7 @@ struct checkout_opts {
112 .merge = -1, \
113 .patch_context = -1, \
114 .patch_interhunk_context = -1, \
115 + .auto_advance = 1, \
116 }
117
118 struct branch_info {
@@ -549,6 +551,7 @@ static int checkout_paths(const struct checkout_opts *opts,
551 struct add_p_opt add_p_opt = {
552 .context = opts->patch_context,
553 .interhunkcontext = opts->patch_interhunk_context,
554 + .auto_advance = opts->auto_advance
555 };
556 const char *rev = new_branch_info->name;
557 char rev_oid[GIT_MAX_HEXSZ + 1];
@@ -1801,6 +1804,8 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
1804 die(_("the option '%s' requires '%s'"), "--unified", "--patch");
1805 if (opts->patch_interhunk_context != -1)
1806 die(_("the option '%s' requires '%s'"), "--inter-hunk-context", "--patch");
1807 + if (!opts->auto_advance)
1808 + die(_("the option '%s' requires '%s'"), "--no-auto-advance", "--patch");
1809 }
1810
1811 if (opts->show_progress < 0) {
@@ -1999,6 +2004,8 @@ int cmd_checkout(int argc,
2004 OPT_BOOL(0, "guess", &opts.dwim_new_local_branch,
2005 N_("second guess 'git checkout <no-such-branch>' (default)")),
2006 OPT_BOOL(0, "overlay", &opts.overlay_mode, N_("use overlay mode (default)")),
2007 + OPT_BOOL(0, "auto-advance", &opts.auto_advance,
2008 + N_("auto advance to the next file when selecting hunks interactively")),
2009 OPT_END()
2010 };
2011
builtin/reset.c
+4
@@ -371,6 +371,8 @@ int cmd_reset(int argc,
371 PARSE_OPT_OPTARG,
372 option_parse_recurse_submodules_worktree_updater),
373 OPT_BOOL('p', "patch", &patch_mode, N_("select hunks interactively")),
374 + OPT_BOOL(0, "auto-advance", &add_p_opt.auto_advance,
375 + N_("auto advance to the next file when selecting hunks interactively")),
376 OPT_DIFF_UNIFIED(&add_p_opt.context),
377 OPT_DIFF_INTERHUNK_CONTEXT(&add_p_opt.interhunkcontext),
378 OPT_BOOL('N', "intent-to-add", &intent_to_add,
@@ -443,6 +445,8 @@ int cmd_reset(int argc,
445 die(_("the option '%s' requires '%s'"), "--unified", "--patch");
446 if (add_p_opt.interhunkcontext != -1)
447 die(_("the option '%s' requires '%s'"), "--inter-hunk-context", "--patch");
448 + if (!add_p_opt.auto_advance)
449 + die(_("the option '%s' requires '%s'"), "--no-auto-advance", "--patch");
450 }
451
452 /* git reset tree [--] paths... can be used to
builtin/stash.c
+8
@@ -1849,6 +1849,8 @@ static int push_stash(int argc, const char **argv, const char *prefix,
1849 N_("stash staged changes only")),
1850 OPT_BOOL('p', "patch", &patch_mode,
1851 N_("stash in patch mode")),
1852 + OPT_BOOL(0, "auto-advance", &add_p_opt.auto_advance,
1853 + N_("auto advance to the next file when selecting hunks interactively")),
1854 OPT_DIFF_UNIFIED(&add_p_opt.context),
1855 OPT_DIFF_INTERHUNK_CONTEXT(&add_p_opt.interhunkcontext),
1856 OPT__QUIET(&quiet, N_("quiet mode")),
@@ -1911,6 +1913,8 @@ static int push_stash(int argc, const char **argv, const char *prefix,
1913 die(_("the option '%s' requires '%s'"), "--unified", "--patch");
1914 if (add_p_opt.interhunkcontext != -1)
1915 die(_("the option '%s' requires '%s'"), "--inter-hunk-context", "--patch");
1916 + if (!add_p_opt.auto_advance)
1917 + die(_("the option '%s' requires '%s'"), "--no-auto-advance", "--patch");
1918 }
1919
1920 if (add_p_opt.context < -1)
@@ -1952,6 +1956,8 @@ static int save_stash(int argc, const char **argv, const char *prefix,
1956 N_("stash staged changes only")),
1957 OPT_BOOL('p', "patch", &patch_mode,
1958 N_("stash in patch mode")),
1959 + OPT_BOOL(0, "auto-advance", &add_p_opt.auto_advance,
1960 + N_("auto advance to the next file when selecting hunks interactively")),
1961 OPT_DIFF_UNIFIED(&add_p_opt.context),
1962 OPT_DIFF_INTERHUNK_CONTEXT(&add_p_opt.interhunkcontext),
1963 OPT__QUIET(&quiet, N_("quiet mode")),
@@ -1983,6 +1989,8 @@ static int save_stash(int argc, const char **argv, const char *prefix,
1989 die(_("the option '%s' requires '%s'"), "--unified", "--patch");
1990 if (add_p_opt.interhunkcontext != -1)
1991 die(_("the option '%s' requires '%s'"), "--inter-hunk-context", "--patch");
1992 + if (!add_p_opt.auto_advance)
1993 + die(_("the option '%s' requires '%s'"), "--no-auto-advance", "--patch");
1994 }
1995
1996 ret = do_push_stash(&ps, stash_msg, quiet, keep_index,
t/t9902-completion.sh
+1
@@ -2601,6 +2601,7 @@ test_expect_success 'double dash "git checkout"' '
2601 --ignore-skip-worktree-bits Z
2602 --ignore-other-worktrees Z
2603 --recurse-submodules Z
2604 + --auto-advance Z
2605 --progress Z
2606 --guess Z
2607 --no-guess Z