Volume XXII, number 279Tuesday, October 6, 2026Latest message 23 minutes ago

The Git List

News and archive of git@vger.kernel.org, since April 2005

[RFC] git-brebase

13 messages between Oct 3, 2026 and Oct 4, 2026, from Alejandro Colomar, Nico Williams.

Plain Markdown or JSON for tools and agents.

Alejandro ColomarOct 3, 2026, 20:11 UTC on lore
Hi!

I've significantly improved the idea from the original thread, thanks to suggestions from several people (the most fundamental, by Nico Williams and Ben Boeckel), and inspired to write it after knowing about the tool written by Nico Williams and Viktor Dukhovni (but I implemented it from scratch, without reading their implementation, other than looking at the file size --which, being under 100 LoC, gave me the confidence that it could be implemented easily--).

I believe now, after several improvements, it is not only simpler than git-imerge, but also more powerful.

I've not used git-imerge, but I've watched the youtube video of the talk in which the author explains how it works (and it's quite nice, to be fair). First some similarities between both:

-  Both git-imerge and my tool support --first-parent (per the README of
   git-imerge).  My tool has support for this by using git-bisect(1)
   interally for the bisection.
   This feature was suggested to me by Ben Boeckel.
-  Both git-imerge and my tool reach the same tip after a successful
   session.  They differ in the intermediate history.

Below goes an overview of key limitations of the git-imerge approach (IMO), and which are not present in mine. If git-imerge supports any of this, I'm sorry; I didn't find them in their README.

-  git-imerge creates a 2D matrix of conflict resolutions.  This is nice
   for the case of two flat branches.  However, the branch to be rebased
   might also contain merge commits.  This is less common than having
   merge commits in the target branch, but it happens, and in those
   cases, it's frequent to want to keep the structure of the branch,
   with --rebase-merges.  My tool has support for this by using
   git-rebase(1) internally for the rebases.
-  One may want to skip arbitrary commits (because they're known to be
   broken, and possibly immediately reverted).  For that, I've provided
   a specific flag, --pre-exec, which is similar to git-rebase(1)'s
   --exec, but which is executed before each rebase operation, at the
   BISECT_HEAD commit.  An exit code of 125 skips that revision
   immediately, without trying to do any rebases.
   This feature was suggested to me by Ben Boeckel.
-  One may also want to find and resolve semantic conflicts that do not
   appear as physical text conflicts.  For that, I've provided a
   specific flag, --post-exec, which is similar to --pre-exec, but runs
   after each successful git-rebase(1) operation.  This would usually
   build and test the software itself.
   This feature was suggested to me by Ben Boeckel.
-  git-imerge is limited when rebasing trees of branches.  Let's
   consider something more complex:
	*---*---*---*---*---M
	 \
	  *---*---A---*---B
	   \ /     \
	    *       *---C---D
   Let's say M is master, to which we want to rebase the tree composed
   of branches A, B, and C, so that it results in this:
	*---*---*---*---*---M
	                     \
	                      *---*---A'--*---B'
	                       \ /     \
	                        *       *---C'--D'
   With my tool, this becomes trivial (I've been doing this all day
   earlier today, resolving in a few hours what would have taken me
   days).  The approach in this case would be to rebase from root to
   leaves, but to solve conlicts from leaves to root:
	1)  Use git-brebase to rebase A until the first conflict with M.
	2)  Abort the conflicting rebase.  We don't want to resolve the
	    conflict yet, or we'd have to repeat the same resolution
	    later.
	3)  Rebase all its direct descendants into the new A, by
	    performing operations 1 and 2 but with the descendant
	    branches.  Do this recursively with children of children.
	4)  Once all descendants have been moved below the new A, it's
	    time to solve the conflicts in A.  This will result in
	    advancing A by just one commit of M, since we had aborted
	    exactly at the conflicting rebase.
	5)  Rebase the direct descendants on top of the new A, this time
	    with git-rebase(1) --not brebase!-- with --interactive,
	    dropping all commits that exist in A.  This avoids resolving
	    the same conflicts again.  Do this recursively with children
	    of children.
	6)  Rinse and repeat since step 1, until everything is
	    successful.  At that point, we have reached the end of the
	    session.
   This approach is different from git-imerge, in that git-imerge
   manages in a single session the entire rebase operation until
   success, while my tool performs each conflict resolution in a single
   step, and they are entirely independent, and can be interrupted to do
   other work.  My tool requires repeated invocations until reaching the
   end point, denoted by a successful exit status.
Something that git-imerge has that my tool hasn't is the ability to do
merge commits.  My tool exclusively does rebases.  However, once the end
commit is reached, creating that could be used to produce a merge
commit.  It could be done by first reaching the rebase tip in a
disposable branch, then perform a regular merge commit with
git-merge(1), and resolve conflicts by doing something like
	$ git checkout disposable -- .
and then finish the merge.  It's not a critical limitation of my tool,
IMO.  (Although, admittedly, it's a trick that not everyone would know
to do.)

Another difference is that, by doing rebases, my tool doesn't remember the entire history matrix that git-imerge holds while doing the work. I see this as an advantage, as once we've finished one conflict step, and we've verified with git-range-diff(1) and with proper testing that it's correct, the extra history would clutter the 'git log --graph --oneline HEAD target current' (something essential when doing these operations). Having a lean history in the process helps get it right, being able to check important commits in the log.

Now about details of the implementaion:
-  The tool supports --first-parent, and passes it transparently to
   git-bisect(1).
-  The tool supports the flags --pre-exec and --post-exec, which are
   interpreted especially by the tool.
-  The tool accepts other flags, and passes them transparently to
   git-rebase(1).  If some flag isn't supported by git-rebase(1), it
   will be that program which will complain.  Also, I haven't made an
   attempt to validate that the flags passed make sense with this tool
   (for example, passing --abort would be accepted by git-rebase(1), but
    it wouldn't make sense, and would probably fail at some point).
   It would be good to curate a list of flags that make sense.
-  My tool, being a simple shell script with rudimentary option parsing,
   only accepts flags that take a single shell argument.  That is,
   --foo=bar is ok, but --foo bar is not okay (and will probably result
   in parsing errors).
-  The tool is meant to be used almost as a drop-in of git-rebase(1).
   It does the same thing, except that instead of rebasing on the
   target, it rebases on the first commit of the target branch which
   has conflicts.
(It has grown a bit fatter than it was, but it's still way below
 git-imerge.)
	$ wc -l <src/bin/git-brebase 
	164

We'll discuss the exact way it should be integrated within git(1), but first it'd be interesting to get feedback about the tool itself, regardless of the actual form. Actually, because of the specialized flags --pre-exec and --post-exec, and the --first-parent flag from git-bisect(1) --and the fact that it runs git-bisect(1) machinery--, I'm not entirely sure that it should be just a new flag to git-rebase(1). It might be confusing to have these three flags being dependent on another flag, and not being able to use this within a git-bisect(1) session, unlike other git-rebase(1) operations. That might call for a new git command.

Please let me know any feedback!  :)

Junio, since you seemed to love git-imerge, I wonder what you'll think of this tool. :-)

Having presented the tool, below goes the implementation.

Have a lovely day! Alex

--- #!/bin/bash # Copyright 2026, Alejandro Colomar <alx@kernel.org> # SPDX-License-Identifier: GPL-3.0-or-later

set -Eeufo pipefail; shopt -s lastpipe;

err()
{
	>&2 printf '%s\n' "$(basename "$0"): error: $*";
	exit 1;
}
fp='';
other='';
pre='';
post='';
while test $# -ge 1; do
	case "$1" in
	--first-parent)
		fp='--first-parent';
		;;
	--pre-exec=*)
		echo "$1" \
		| sed 's/--pre-exec=//' \
		| read -r pre;
		;;
	--post-exec=*)
		echo "$1" \
		| sed 's/--post-exec=//' \
		| read -r post;
		;;
	-*)
		other="$other $1";
		;;
	*)
		break;
		;;
	esac;
	shift;
done;
gbopts="$fp";
gropts="$other";
if test $# -lt 1; then
	err 'Missing target commit.';
fi;
if test $# -gt 1; then
	err 'Too many arguments.';
fi;
git rev-list -1 "$1" \
| read -r tgt;
git rev-parse --abbrev-ref HEAD \
| read -r branch;
# Set up the callback script for 'git rebase run'.
mktemp \
| read -r callback;
cat >"$callback" <<__EOF__
#!/bin/bash
	set -Eeufo pipefail;
	shopt -s lastpipe;
	git rev-list -1 HEAD \
	| read -r bisect_head;
	if test -n '$pre'; then
		printf '%s' 'Pre-rebase exec: ';
		pre='$pre';
		if
			\$pre;
			x="\$?";
			true;
		then
			case "\$x" in
			0)
				echo 'success';
				;;
			125)
				echo 'skip';
				git checkout --detach "\$bisect_head" 2>/dev/null;
				exit 125;
				;;
			*)
				echo "failure (\$x)";
				git checkout --detach "\$bisect_head" 2>/dev/null;
				exit "\$x";
				;;
			esac;
		fi;
	fi;
	git switch '$branch' >/dev/null 2>/dev/null;
	git rev-list -1 HEAD \
	| read -r old_head;
	printf '%s' 'Rebase: ';
	if git rebase $gropts "\$bisect_head" >/dev/null 2>/dev/null; then
		echo 'success';
	else
		echo 'conflict';
		git rebase --abort >/dev/null;
		git checkout --detach "\$bisect_head" 2>/dev/null;
		exit 1;
	fi;
	if test -n '$post'; then
		printf '%s' 'Post-rebase exec: ';
		post='$post';
		if
			\$post;
			x="\$?";
			true;
		then
			case "\$x" in
			0)
				echo 'success';
				;;
			125)
				echo 'skip';
				git reset --hard "\$old_head";
				git checkout --detach "\$bisect_head" 2>/dev/null;
				exit 125;
				;;
			*)
				echo "failure (\$x)";
				git reset --hard "\$old_head";
				git checkout --detach "\$bisect_head" 2>/dev/null;
				exit "\$x";
				;;
			esac;
		fi;
	fi;
	git checkout --detach "\$bisect_head" 2>/dev/null;
	exit 0;
__EOF__
chmod +x "$callback";
# Try the target first.
git checkout --detach "$tgt" 2>/dev/null;
if "$callback"; then
	exit 0;
fi;
git status;
# Bisect.
# shellcheck disable=SC2248  # gbopts may hold multiple options
git bisect start $gbopts >/dev/null;
git bisect bad "$tgt" >/dev/null;
git merge-base "$branch" "$tgt" \
| xargs -I{} git bisect good {};
git bisect run "$callback";
git rev-list -1 bisect/bad \
| read -r bad;
git bisect reset >/dev/null 2>/dev/null;
# Perform the conflicting rebase
git switch "$branch";
# shellcheck disable=SC2086  # gropts may hold multiple options
git rebase $gropts "$bad";
if test -v post; then
	echo 'Running post-rebase exec.';
	$post;
fi;
-- 
<https://www.alejandro-colomar.es>
Alejandro ColomarOct 3, 2026, 20:13 UTC in reply to Alejandro Colomar on lore

Re: [RFC] git-brebase

(I meant this to be a reply to the previous thread, but I somehow
 forgot while writing it.  Here's a link, for context.)
<https://lore.kernel.org/git/ar5KL4_IKXYbx3Sb@debian/T/#u>

Cheers, Alex

Show 357 quoted lines
> Date: 2026-10-03 22:11:54+0200
> From: Alejandro Colomar <alx@kernel.org>
>
> Hi!
> 
> I've significantly improved the idea from the original thread, thanks to
> suggestions from several people (the most fundamental, by Nico Williams
> and Ben Boeckel), and inspired to write it after knowing about the tool
> written by Nico Williams and Viktor Dukhovni (but I implemented it from
> scratch, without reading their implementation, other than looking at the
> file size --which, being under 100 LoC, gave me the confidence that it
> could be implemented easily--).
> 
> I believe now, after several improvements, it is not only simpler than
> git-imerge, but also more powerful.
> 
> I've not used git-imerge, but I've watched the youtube video of the talk
> in which the author explains how it works (and it's quite nice, to be
> fair).  First some similarities between both:
> 
> -  Both git-imerge and my tool support --first-parent (per the README of
>    git-imerge).  My tool has support for this by using git-bisect(1)
>    interally for the bisection.
> 
>    This feature was suggested to me by Ben Boeckel.
> 
> -  Both git-imerge and my tool reach the same tip after a successful
>    session.  They differ in the intermediate history.
> 
> Below goes an overview of key limitations of the git-imerge approach
> (IMO), and which are not present in mine.  If git-imerge supports any of
> this, I'm sorry; I didn't find them in their README.
> 
> -  git-imerge creates a 2D matrix of conflict resolutions.  This is nice
>    for the case of two flat branches.  However, the branch to be rebased
>    might also contain merge commits.  This is less common than having
>    merge commits in the target branch, but it happens, and in those
>    cases, it's frequent to want to keep the structure of the branch,
>    with --rebase-merges.  My tool has support for this by using
>    git-rebase(1) internally for the rebases.
> 
> -  One may want to skip arbitrary commits (because they're known to be
>    broken, and possibly immediately reverted).  For that, I've provided
>    a specific flag, --pre-exec, which is similar to git-rebase(1)'s
>    --exec, but which is executed before each rebase operation, at the
>    BISECT_HEAD commit.  An exit code of 125 skips that revision
>    immediately, without trying to do any rebases.
> 
>    This feature was suggested to me by Ben Boeckel.
> 
> -  One may also want to find and resolve semantic conflicts that do not
>    appear as physical text conflicts.  For that, I've provided a
>    specific flag, --post-exec, which is similar to --pre-exec, but runs
>    after each successful git-rebase(1) operation.  This would usually
>    build and test the software itself.
> 
>    This feature was suggested to me by Ben Boeckel.
> 
> -  git-imerge is limited when rebasing trees of branches.  Let's
>    consider something more complex:
> 
> 	*---*---*---*---*---M
> 	 \
> 	  *---*---A---*---B
> 	   \ /     \
> 	    *       *---C---D
> 
>    Let's say M is master, to which we want to rebase the tree composed
>    of branches A, B, and C, so that it results in this:
> 
> 	*---*---*---*---*---M
> 	                     \
> 	                      *---*---A'--*---B'
> 	                       \ /     \
> 	                        *       *---C'--D'
> 
>    With my tool, this becomes trivial (I've been doing this all day
>    earlier today, resolving in a few hours what would have taken me
>    days).  The approach in this case would be to rebase from root to
>    leaves, but to solve conlicts from leaves to root:
> 
> 	1)  Use git-brebase to rebase A until the first conflict with M.
> 
> 	2)  Abort the conflicting rebase.  We don't want to resolve the
> 	    conflict yet, or we'd have to repeat the same resolution
> 	    later.
> 
> 	3)  Rebase all its direct descendants into the new A, by
> 	    performing operations 1 and 2 but with the descendant
> 	    branches.  Do this recursively with children of children.
> 
> 	4)  Once all descendants have been moved below the new A, it's
> 	    time to solve the conflicts in A.  This will result in
> 	    advancing A by just one commit of M, since we had aborted
> 	    exactly at the conflicting rebase.
> 
> 	5)  Rebase the direct descendants on top of the new A, this time
> 	    with git-rebase(1) --not brebase!-- with --interactive,
> 	    dropping all commits that exist in A.  This avoids resolving
> 	    the same conflicts again.  Do this recursively with children
> 	    of children.
> 
> 	6)  Rinse and repeat since step 1, until everything is
> 	    successful.  At that point, we have reached the end of the
> 	    session.
> 
>    This approach is different from git-imerge, in that git-imerge
>    manages in a single session the entire rebase operation until
>    success, while my tool performs each conflict resolution in a single
>    step, and they are entirely independent, and can be interrupted to do
>    other work.  My tool requires repeated invocations until reaching the
>    end point, denoted by a successful exit status.
> 
> Something that git-imerge has that my tool hasn't is the ability to do
> merge commits.  My tool exclusively does rebases.  However, once the end
> commit is reached, creating that could be used to produce a merge
> commit.  It could be done by first reaching the rebase tip in a
> disposable branch, then perform a regular merge commit with
> git-merge(1), and resolve conflicts by doing something like
> 	$ git checkout disposable -- .
> and then finish the merge.  It's not a critical limitation of my tool,
> IMO.  (Although, admittedly, it's a trick that not everyone would know
> to do.)
> 
> Another difference is that, by doing rebases, my tool doesn't remember
> the entire history matrix that git-imerge holds while doing the work.
> I see this as an advantage, as once we've finished one conflict step,
> and we've verified with git-range-diff(1) and with proper testing that
> it's correct, the extra history would clutter the
> 'git log --graph --oneline HEAD target current' (something essential
> when doing these operations).  Having a lean history in the process
> helps get it right, being able to check important commits in the log.
> 
> Now about details of the implementaion:
> 
> -  The tool supports --first-parent, and passes it transparently to
>    git-bisect(1).
> 
> -  The tool supports the flags --pre-exec and --post-exec, which are
>    interpreted especially by the tool.
> 
> -  The tool accepts other flags, and passes them transparently to
>    git-rebase(1).  If some flag isn't supported by git-rebase(1), it
>    will be that program which will complain.  Also, I haven't made an
>    attempt to validate that the flags passed make sense with this tool
>    (for example, passing --abort would be accepted by git-rebase(1), but
>     it wouldn't make sense, and would probably fail at some point).
> 
>    It would be good to curate a list of flags that make sense.
> 
> -  My tool, being a simple shell script with rudimentary option parsing,
>    only accepts flags that take a single shell argument.  That is,
>    --foo=bar is ok, but --foo bar is not okay (and will probably result
>    in parsing errors).
> 
> -  The tool is meant to be used almost as a drop-in of git-rebase(1).
>    It does the same thing, except that instead of rebasing on the
>    target, it rebases on the first commit of the target branch which
>    has conflicts.
> 
> (It has grown a bit fatter than it was, but it's still way below
>  git-imerge.)
> 
> 	$ wc -l <src/bin/git-brebase 
> 	164
> 
> We'll discuss the exact way it should be integrated within git(1), but
> first it'd be interesting to get feedback about the tool itself,
> regardless of the actual form.  Actually, because of the specialized
> flags --pre-exec and --post-exec, and the --first-parent flag from
> git-bisect(1) --and the fact that it runs git-bisect(1) machinery--, I'm
> not entirely sure that it should be just a new flag to git-rebase(1).
> It might be confusing to have these three flags being dependent on
> another flag, and not being able to use this within a git-bisect(1)
> session, unlike other git-rebase(1) operations.  That might call for
> a new git command.
> 
> Please let me know any feedback!  :)
> 
> Junio, since you seemed to love git-imerge, I wonder what you'll think
> of this tool.  :-)
> 
> Having presented the tool, below goes the implementation.
> 
> 
> Have a lovely day!
> Alex
> 
> ---
> #!/bin/bash
> # Copyright 2026, Alejandro Colomar <alx@kernel.org>
> # SPDX-License-Identifier: GPL-3.0-or-later
> 
> set -Eeufo pipefail;
> shopt -s lastpipe;
> 
> err()
> {
> 	>&2 printf '%s\n' "$(basename "$0"): error: $*";
> 	exit 1;
> }
> 
> fp='';
> other='';
> pre='';
> post='';
> while test $# -ge 1; do
> 	case "$1" in
> 	--first-parent)
> 		fp='--first-parent';
> 		;;
> 	--pre-exec=*)
> 		echo "$1" \
> 		| sed 's/--pre-exec=//' \
> 		| read -r pre;
> 		;;
> 	--post-exec=*)
> 		echo "$1" \
> 		| sed 's/--post-exec=//' \
> 		| read -r post;
> 		;;
> 	-*)
> 		other="$other $1";
> 		;;
> 	*)
> 		break;
> 		;;
> 	esac;
> 	shift;
> done;
> gbopts="$fp";
> gropts="$other";
> 
> if test $# -lt 1; then
> 	err 'Missing target commit.';
> fi;
> if test $# -gt 1; then
> 	err 'Too many arguments.';
> fi;
> git rev-list -1 "$1" \
> | read -r tgt;
> git rev-parse --abbrev-ref HEAD \
> | read -r branch;
> 
> # Set up the callback script for 'git rebase run'.
> mktemp \
> | read -r callback;
> cat >"$callback" <<__EOF__
> #!/bin/bash
> 
> 	set -Eeufo pipefail;
> 	shopt -s lastpipe;
> 
> 	git rev-list -1 HEAD \
> 	| read -r bisect_head;
> 
> 	if test -n '$pre'; then
> 		printf '%s' 'Pre-rebase exec: ';
> 		pre='$pre';
> 		if
> 			\$pre;
> 			x="\$?";
> 			true;
> 		then
> 			case "\$x" in
> 			0)
> 				echo 'success';
> 				;;
> 			125)
> 				echo 'skip';
> 				git checkout --detach "\$bisect_head" 2>/dev/null;
> 				exit 125;
> 				;;
> 			*)
> 				echo "failure (\$x)";
> 				git checkout --detach "\$bisect_head" 2>/dev/null;
> 				exit "\$x";
> 				;;
> 			esac;
> 		fi;
> 	fi;
> 
> 	git switch '$branch' >/dev/null 2>/dev/null;
> 	git rev-list -1 HEAD \
> 	| read -r old_head;
> 	printf '%s' 'Rebase: ';
> 	if git rebase $gropts "\$bisect_head" >/dev/null 2>/dev/null; then
> 		echo 'success';
> 	else
> 		echo 'conflict';
> 		git rebase --abort >/dev/null;
> 		git checkout --detach "\$bisect_head" 2>/dev/null;
> 		exit 1;
> 	fi;
> 
> 	if test -n '$post'; then
> 		printf '%s' 'Post-rebase exec: ';
> 		post='$post';
> 		if
> 			\$post;
> 			x="\$?";
> 			true;
> 		then
> 			case "\$x" in
> 			0)
> 				echo 'success';
> 				;;
> 			125)
> 				echo 'skip';
> 				git reset --hard "\$old_head";
> 				git checkout --detach "\$bisect_head" 2>/dev/null;
> 				exit 125;
> 				;;
> 			*)
> 				echo "failure (\$x)";
> 				git reset --hard "\$old_head";
> 				git checkout --detach "\$bisect_head" 2>/dev/null;
> 				exit "\$x";
> 				;;
> 			esac;
> 		fi;
> 	fi;
> 	git checkout --detach "\$bisect_head" 2>/dev/null;
> 	exit 0;
> __EOF__
> chmod +x "$callback";
> 
> # Try the target first.
> git checkout --detach "$tgt" 2>/dev/null;
> if "$callback"; then
> 	exit 0;
> fi;
> git status;
> 
> # Bisect.
> # shellcheck disable=SC2248  # gbopts may hold multiple options
> git bisect start $gbopts >/dev/null;
> git bisect bad "$tgt" >/dev/null;
> git merge-base "$branch" "$tgt" \
> | xargs -I{} git bisect good {};
> git bisect run "$callback";
> git rev-list -1 bisect/bad \
> | read -r bad;
> git bisect reset >/dev/null 2>/dev/null;
> 
> # Perform the conflicting rebase
> git switch "$branch";
> # shellcheck disable=SC2086  # gropts may hold multiple options
> git rebase $gropts "$bad";
> if test -v post; then
> 	echo 'Running post-rebase exec.';
> 	$post;
> fi;
> 
> 
> -- 
> <https://www.alejandro-colomar.es>
-- 
<https://www.alejandro-colomar.es>
Nico WilliamsOct 3, 2026, 20:48 UTC in reply to Nico Williams on lore

Re: [RFC] git-brebase

On Sat, Oct 03, 2026 at 03:39:41PM -0500, Nico Williams wrote:
> that it deserves a name.  But also, `git-rebase(1)` should always have
> been this useful, so that argues for this to be either... a new option
> like `--onto-first-conflict`, or even a new default behavior.

I.e., maybe if I run `git rebase $upstream/$branch` and there's conflicts then it should do this bisection to drop me at the first upstream commit to introduce conflicts, stop, inform me of this, inform me that after I finish resolving conflicts I need to run `git rebase --continue` until that's done, that I should then `git rebase $upstream/$branch` again.

Additionally when `git rebase --continue` finishes successfully, if we're not at `$upstream/$branch` then `--continue` should run `git rebase $upstream/$branch` directly. This means recording the target committish along with other git rebase state during the rebase. This would feel very natural to me.

If we take this approach then we'd need an option not to enable this behavior but to disable it, something like `--direct`.

The more I think about it, the more I want this approach to be the default for `git rebase`. But maybe there's a good reason not to? Maybe first ship the option, then later make it the default?

Nico
Alejandro ColomarOct 3, 2026, 20:56 UTC in reply to Nico Williams on lore

Re: [RFC] git-brebase

Hi Nico,
> Date: 2026-10-03 15:39:41-0500
> From: Nico Williams <nico@cryptonector.com>
>
[...]
Show 7 quoted lines
> > It might be confusing to have these three flags being dependent on
> > another flag, and not being able to use this within a git-bisect(1)
> 
> IMO that's not a problem at all.  There are a lot of Unix/Linux commands
> that have flags that only make sense when used with other specific
> flags.  So I still like a `--first-conflict` or `--onto-first-conflict`
> option.
Yeah, it could make sense.  I'm not sure, but it could be.
> (I really like `--pre-exec` and `--post-exec`, BTW.)
:)
Show 9 quoted lines
> > session, unlike other git-rebase(1) operations.  That might call for
> > a new git command.
> 
> That might still be the case in that this will be such a useful tool
> that it deserves a name.  But also, `git-rebase(1)` should always have
> been this useful, so that argues for this to be either... a new option
> like `--onto-first-conflict`, or even a new default behavior.
> 
> Does jj have a feature like this?  What do they call it?
No idea.
[...]
Show 8 quoted lines
> > while test $# -ge 1; do
> 
> I normally use
> 
>   while getopts +:<short-options-here> opt; do ...
> 
> I also have a getopts_long-like function (see my gists) for bash if you
> like.

I think getopts(1) is not usable for git(1)-related scripts, because getopts(1) interprets '--' as the end of the options, but git(1) uses it for distinguishing commits from paths. If anyone shows me how it can be used, I'd be interested, because I've hit this issue in the past with other script.

Show 7 quoted lines
> > [...]
> > 
> > # Set up the callback script for 'git rebase run'.
> > mktemp \
> > | read -r callback;
> 
> I like to set a `trap` to remove temp files.

Hmmm, makes sense. If so, I'll also try to filter out the line that prints the name of the command, since 'git bisect run' prints it, and we don't want users to try to open a file that doens't exist.

Show 8 quoted lines
> > cat >"$callback" <<__EOF__
> > #!/bin/bash
> > ...
> > __EOF__
> > chmod +x "$callback";
> 
> Here what might be better is to have a command-line option to execute
> this callback without having to write it to a file,
How would you do it?
> and use environment
> variables to pass arguments to it.

The callback doesn't really need any arguments, since 'git bisect run' won't pass any arguments to it.

Show 9 quoted lines
> > # Perform the conflicting rebase
> > git switch "$branch";
> 
> Ah, that came from:
> 
> > git rev-parse --abbrev-ref HEAD \
> > | read -r branch;
> 
> which means I can't use this in detached HEAD mode :(
Oh!  I wasn't aware that git-rebase(1) supported detached HEAD mode.
> I work in detached HEAD mode almost exclusively.  I know, that's..
> weird.  But it works for me.

Ouch! Indeed. :) Out of curiosity, are there any interesting reasons for such self-implied pain?

> Can we avoid forcing the user to be on a
> branch?

I guess I could keep a variable that remembers the state of the HEAD across all the rebases. It should be doable. I'll have a look (maybe tomorrow).

Have a lovely night! Alex

> Nico
> -- 
-- 
<https://www.alejandro-colomar.es>
Alejandro ColomarOct 3, 2026, 21:07 UTC in reply to Nico Williams on lore

Re: [RFC] git-brebase

Hi Nico,
Show 20 quoted lines
> Date: 2026-10-03 15:48:46-0500
> From: Nico Williams <nico@cryptonector.com>
>
> On Sat, Oct 03, 2026 at 03:39:41PM -0500, Nico Williams wrote:
> > that it deserves a name.  But also, `git-rebase(1)` should always have
> > been this useful, so that argues for this to be either... a new option
> > like `--onto-first-conflict`, or even a new default behavior.
> 
> I.e., maybe if I run `git rebase $upstream/$branch` and there's
> conflicts then it should do this bisection to drop me at the first
> upstream commit to introduce conflicts, stop, inform me of this, inform
> me that after I finish resolving conflicts I need to run `git rebase
> --continue` until that's done, that I should then `git rebase
> $upstream/$branch` again.
> 
> Additionally when `git rebase --continue` finishes successfully, if
> we're not at `$upstream/$branch` then `--continue` should run `git
> rebase $upstream/$branch` directly.  This means recording the target
> committish along with other git rebase state during the rebase.  This
> would feel very natural to me.

What would --abort do? Go back to the begining of this iteration, or to the very first initial state of the branch?

The useful thing would be to just go back to the begining of this iteration --that is essential for rebasing entire trees of branches, as explained in the previous message--.

Since --abort doesn't go all the way back, I think --continue shouldn't continue all the way forward, for consistency.

If that's desired behavior, it should go in this tool, and not as part of git-rebase(1). git-rebase(1) is a much simpler and much more fundamental tool, which is used to build this more complex tool. That's one reason I'm rather opposed to having this as part of git-rebase(1); it would confuse about the responsibility of git-rebase(1). IMO, git-rebase(1) is a plumbing command, and git-brebase would be a porcelain thingy.

> If we take this approach then we'd need an option not to enable this
> behavior but to disable it, something like `--direct`.
That hints it might be just be a different command.
Show 6 quoted lines
> The more I think about it, the more I want this approach to be the
> default for `git rebase`.  But maybe there's a good reason not to?
> Maybe first ship the option, then later make it the default?
> 
> Nico
> -- 

Have a lovely night! Alex

-- 
<https://www.alejandro-colomar.es>
Nico WilliamsOct 3, 2026, 21:17 UTC in reply to Alejandro Colomar on lore

Re: [RFC] git-brebase

On Sat, Oct 03, 2026 at 11:07:04PM +0200, Alejandro Colomar wrote:
> Since --abort doesn't go all the way back, I think --continue shouldn't
> continue all the way forward, for consistency.
Fair.
Show 12 quoted lines
> If that's desired behavior, it should go in this tool, and not as part
> of git-rebase(1).  git-rebase(1) is a much simpler and much more
> fundamental tool, which is used to build this more complex tool.
> That's one reason I'm rather opposed to having this as part of
> git-rebase(1); it would confuse about the responsibility of
> git-rebase(1).  IMO, git-rebase(1) is a plumbing command, and
> git-brebase would be a porcelain thingy.
> 
> > If we take this approach then we'd need an option not to enable this
> > behavior but to disable it, something like `--direct`.
> 
> That hints it might be just be a different command.

If this was 2007, and you were writing the first version of `rebase`, and you had already worked out that you wanted this feature, what would you do then? Would you make it the default? I _think_ I would.

Basically, this makes rebasing much nicer, so why not make it the default?

Nico
Alejandro ColomarOct 3, 2026, 21:17 UTC in reply to Alejandro Colomar on lore

Re: [RFC] git-brebase

Show 79 quoted lines
> Date: 2026-10-03 22:56:32+0200
> From: Alejandro Colomar <alx@kernel.org>
>
> Hi Nico,
> 
> > Date: 2026-10-03 15:39:41-0500
> > From: Nico Williams <nico@cryptonector.com>
> >
> [...]
> > > It might be confusing to have these three flags being dependent on
> > > another flag, and not being able to use this within a git-bisect(1)
> > 
> > IMO that's not a problem at all.  There are a lot of Unix/Linux commands
> > that have flags that only make sense when used with other specific
> > flags.  So I still like a `--first-conflict` or `--onto-first-conflict`
> > option.
> 
> Yeah, it could make sense.  I'm not sure, but it could be.
> 
> > (I really like `--pre-exec` and `--post-exec`, BTW.)
> 
> :)
> 
> > > session, unlike other git-rebase(1) operations.  That might call for
> > > a new git command.
> > 
> > That might still be the case in that this will be such a useful tool
> > that it deserves a name.  But also, `git-rebase(1)` should always have
> > been this useful, so that argues for this to be either... a new option
> > like `--onto-first-conflict`, or even a new default behavior.
> > 
> > Does jj have a feature like this?  What do they call it?
> 
> No idea.
> 
> [...]
> > > while test $# -ge 1; do
> > 
> > I normally use
> > 
> >   while getopts +:<short-options-here> opt; do ...
> > 
> > I also have a getopts_long-like function (see my gists) for bash if you
> > like.
> 
> I think getopts(1) is not usable for git(1)-related scripts, because
> getopts(1) interprets '--' as the end of the options, but git(1) uses it
> for distinguishing commits from paths.  If anyone shows me how it can be
> used, I'd be interested, because I've hit this issue in the past with
> other script.
> 
> > > [...]
> > > 
> > > # Set up the callback script for 'git rebase run'.
> > > mktemp \
> > > | read -r callback;
> > 
> > I like to set a `trap` to remove temp files.
> 
> Hmmm, makes sense.  If so, I'll also try to filter out the line that
> prints the name of the command, since 'git bisect run' prints it, and we
> don't want users to try to open a file that doens't exist.
> 
> > > cat >"$callback" <<__EOF__
> > > #!/bin/bash
> > > ...
> > > __EOF__
> > > chmod +x "$callback";
> > 
> > Here what might be better is to have a command-line option to execute
> > this callback without having to write it to a file,
> 
> How would you do it?
> 
> > and use environment
> > variables to pass arguments to it.
> 
> The callback doesn't really need any arguments, since 'git bisect run'
> won't pass any arguments to it.
Self-correction: 'git rebase run' does actually pass arguments to the
command.  However, I still don't see the need.

Cheers, Alex

Show 36 quoted lines
> 
> > > # Perform the conflicting rebase
> > > git switch "$branch";
> > 
> > Ah, that came from:
> > 
> > > git rev-parse --abbrev-ref HEAD \
> > > | read -r branch;
> > 
> > which means I can't use this in detached HEAD mode :(
> 
> Oh!  I wasn't aware that git-rebase(1) supported detached HEAD mode.
> 
> > I work in detached HEAD mode almost exclusively.  I know, that's..
> > weird.  But it works for me.
> 
> Ouch!  Indeed.  :)
> Out of curiosity, are there any interesting reasons for such
> self-implied pain?
> 
> > Can we avoid forcing the user to be on a
> > branch?
> 
> I guess I could keep a variable that remembers the state of the HEAD
> across all the rebases.  It should be doable.  I'll have a look (maybe
> tomorrow).
> 
> 
> Have a lovely night!
> Alex
> 
> > Nico
> > -- 
> 
> -- 
> <https://www.alejandro-colomar.es>
-- 
<https://www.alejandro-colomar.es>
Nico WilliamsOct 3, 2026, 20:39 UTC in reply to Alejandro Colomar on lore

Re: [RFC] git-brebase

On Sat, Oct 03, 2026 at 10:11:47PM +0200, Alejandro Colomar wrote:
Show 8 quoted lines
> We'll discuss the exact way it should be integrated within git(1), but
> first it'd be interesting to get feedback about the tool itself,
> regardless of the actual form.  Actually, because of the specialized
> flags --pre-exec and --post-exec, and the --first-parent flag from
> git-bisect(1) --and the fact that it runs git-bisect(1) machinery--, I'm
> not entirely sure that it should be just a new flag to git-rebase(1).
> It might be confusing to have these three flags being dependent on
> another flag, and not being able to use this within a git-bisect(1)

IMO that's not a problem at all. There are a lot of Unix/Linux commands that have flags that only make sense when used with other specific flags. So I still like a `--first-conflict` or `--onto-first-conflict` option.

(I really like `--pre-exec` and `--post-exec`, BTW.)
> session, unlike other git-rebase(1) operations.  That might call for
> a new git command.

That might still be the case in that this will be such a useful tool that it deserves a name. But also, `git-rebase(1)` should always have been this useful, so that argues for this to be either... a new option like `--onto-first-conflict`, or even a new default behavior.

Does jj have a feature like this?  What do they call it?
Show 19 quoted lines
> ---
> #!/bin/bash
> # Copyright 2026, Alejandro Colomar <alx@kernel.org>
> # SPDX-License-Identifier: GPL-3.0-or-later
> 
> set -Eeufo pipefail;
> shopt -s lastpipe;
> 
> err()
> {
> 	>&2 printf '%s\n' "$(basename "$0"): error: $*";
> 	exit 1;
> }
> 
> fp='';
> other='';
> pre='';
> post='';
> while test $# -ge 1; do
I normally use
  while getopts +:<short-options-here> opt; do ...

I also have a getopts_long-like function (see my gists) for bash if you like.

Show 5 quoted lines
> [...]
> 
> # Set up the callback script for 'git rebase run'.
> mktemp \
> | read -r callback;
I like to set a `trap` to remove temp files.
Show 5 quoted lines
> cat >"$callback" <<__EOF__
> #!/bin/bash
> ...
> __EOF__
> chmod +x "$callback";

Here what might be better is to have a command-line option to execute this callback without having to write it to a file, and use environment variables to pass arguments to it.

> # Perform the conflicting rebase
> git switch "$branch";
Ah, that came from:
> git rev-parse --abbrev-ref HEAD \
> | read -r branch;
which means I can't use this in detached HEAD mode :(

I work in detached HEAD mode almost exclusively. I know, that's.. weird. But it works for me. Can we avoid forcing the user to be on a branch?

Nico
Alejandro ColomarOct 3, 2026, 21:29 UTC in reply to Nico Williams on lore

Re: [RFC] git-brebase

Hi Nico,
Show 28 quoted lines
> Date: 2026-10-03 16:17:22-0500
> From: Nico Williams <nico@cryptonector.com>
>
> On Sat, Oct 03, 2026 at 11:07:04PM +0200, Alejandro Colomar wrote:
> > Since --abort doesn't go all the way back, I think --continue shouldn't
> > continue all the way forward, for consistency.
> 
> Fair.
> 
> > If that's desired behavior, it should go in this tool, and not as part
> > of git-rebase(1).  git-rebase(1) is a much simpler and much more
> > fundamental tool, which is used to build this more complex tool.
> > That's one reason I'm rather opposed to having this as part of
> > git-rebase(1); it would confuse about the responsibility of
> > git-rebase(1).  IMO, git-rebase(1) is a plumbing command, and
> > git-brebase would be a porcelain thingy.
> > 
> > > If we take this approach then we'd need an option not to enable this
> > > behavior but to disable it, something like `--direct`.
> > 
> > That hints it might be just be a different command.
> 
> If this was 2007, and you were writing the first version of `rebase`,
> and you had already worked out that you wanted this feature, what would
> you do then?  Would you make it the default?  I _think_ I would.
> 
> Basically, this makes rebasing much nicer, so why not make it the
> default?
I'm currently defaulting to brebase for every rebase I want to do.

However, I still use the regular git-rebase(1) for more precise operations. To be specific, I use it for changing history (without moving the base), and I also use it for resolving conflicts as part of a git-brebase operation. They seem to me to be different tools, even though they're clearly related.

I think we use git-rebase(1) for what we should be using git-brebase only because we didn't have the latter. That might be the reason we confuse them and think they're the same tool. They're used for very different operations.

Cheers, Alex

> 
> Nico
> -- 
-- 
<https://www.alejandro-colomar.es>
Alejandro ColomarOct 3, 2026, 21:38 UTC in reply to Nico Williams on lore

Re: [RFC] git-brebase

Hi Nico,
Show 16 quoted lines
> Date: 2026-10-03 16:13:25-0500
> From: Nico Williams <nico@cryptonector.com>
>
> On Sat, Oct 03, 2026 at 10:56:25PM +0200, Alejandro Colomar wrote:
> > > I also have a getopts_long-like function (see my gists) for bash if you
> > > like.
> > 
> > I think getopts(1) is not usable for git(1)-related scripts, because
> > getopts(1) interprets '--' as the end of the options, but git(1) uses it
> > for distinguishing commits from paths.  If anyone shows me how it can be
> > used, I'd be interested, because I've hit this issue in the past with
> > other script.
> 
> https://gist.github.com/nicowilliams/f3fe2b10b380aecdef403acb246dced2
> 
> Though there's many ways to do this.

That one still consumes the '--', but we don't want to consume it. We want it to remain there in $@ after the options have been parsed.

Show 15 quoted lines
> > > > cat >"$callback" <<__EOF__
> > > > #!/bin/bash
> > > > ...
> > > > __EOF__
> > > > chmod +x "$callback";
> > > 
> > > Here what might be better is to have a command-line option to execute
> > > this callback without having to write it to a file,
> > 
> > How would you do it?
> 
> I'd have an option or sub-command of the main script that says "do the
> callback thing", then when you run `git bisect run ...` put in the name
> of this script as the command and the "do the callback thing" option
> next.
I'd need to see some code.  I'm not seeing it.  :)
Show 8 quoted lines
> > > and use environment
> > > variables to pass arguments to it.
> > 
> > The callback doesn't really need any arguments, since 'git bisect run'
> > won't pass any arguments to it.
> 
> But you're embedding values into the temp executable script -- if you
> don't have that any more you'll have to pass those in.

But why would we want to not have it? That would complicate the script, no?

Show 38 quoted lines
> > > which means I can't use this in detached HEAD mode :(
> > 
> > Oh!  I wasn't aware that git-rebase(1) supported detached HEAD mode.
> 
> Sure does!
> 
> > > I work in detached HEAD mode almost exclusively.  I know, that's..
> > > weird.  But it works for me.
> > 
> > Ouch!  Indeed.  :)
> > Out of curiosity, are there any interesting reasons for such
> > self-implied pain?
> 
> I often do:
> 
> : ; git checkout origin/master
> : ; <do some work>
> : ; git add ...; git commit -m '...'
> : ; git push myfork HEAD:refs/heads/the-branch-name-here  # <-- I name it here
> 
> then open a PR.
> 
> Now I don't have a branch here, but who cares?  If I switch to other
> work and later want to come back to this work I'll either a) create a
> local branch then, and/or b) when I resume work on the first thing I'll
> `git checkout myfork/the-branch-name-here` and...  once more work in
> detached HEAD mode.
> 
> And if I need to see "what was I doing?" then I use `git log --oneline`
> and `git reflog` and I quickly see the remote branch of interest.
> 
> The remote branches are the symbolic names I need to preserve, and my
> clone will know them, so I only need local branch names for things I
> work on w/o a network or over a long time.
> 
> I do exaggerate a bit.  I do this a lot, but maybe not quite "almost
> exclusively".  Often I'm forced to have a local branch by opinionated
> tools other than git itself.

Hmmm, actually resembles what I do. I use branches, then push to a remote, and once it's in the remote, I remove the local branch. I try to remove the local branches as soon as I can, because that way I don't need to remember whether there was something I forgot to push, or I wanted to explicitly discard it. If there's no local branch, there's no confusion. Since I work with two local computers, having the source of truth be the remote makes it less ambiguous. But while working locally, the branch helps a lot.

Anyway, I've patched it to work with detached HEAD. (I need to remember to add two traps, now.)

	diff --git i/src/bin/git-brebase w/src/bin/git-brebase
	index d652c37ef08d..a57acc350d7e 100755
	--- i/src/bin/git-brebase
	+++ w/src/bin/git-brebase
	@@ -50,8 +50,16 @@ if test $# -gt 1; then
	 fi;
	 git rev-list -1 "$1" \
	 | read -r tgt;
	-git rev-parse --abbrev-ref HEAD \
	+
	+mktemp \
	 | read -r branch;
	+{
	+       git rev-parse --abbrev-ref HEAD;
	+       git rev-list -1 HEAD;
	+} \
	+| sed '/^HEAD$/d' \
	+| sed '1!d' \
	+>"$branch";
	 
	 # Set up the callback script for 'git rebase run'.
	 mktemp \
	@@ -91,7 +99,8 @@ cat >"$callback" <<__EOF__
			fi;
		fi;
	 
	-       git switch '$branch' >/dev/null 2>/dev/null;
	+       cat '$branch' \
	+       | xargs -I{} git checkout {} >/dev/null 2>/dev/null;
		git rev-list -1 HEAD \
		| read -r old_head;
		printf '%s' 'Rebase: ';
	@@ -103,6 +112,13 @@ cat >"$callback" <<__EOF__
			git checkout --detach "\$bisect_head" 2>/dev/null;
			exit 1;
		fi;
	+       {
	+               git rev-parse --abbrev-ref HEAD;
	+               git rev-list -1 HEAD;
	+       } \
	+       | sed '/^HEAD$/d' \
	+       | sed '1!d' \
	+       >"$branch";
	 
		if test -n '$post'; then
			printf '%s' 'Post-rebase exec: ';
	@@ -146,7 +162,8 @@ fi;
	 # shellcheck disable=SC2248  # gbopts may hold multiple options
	 git bisect start $gbopts >/dev/null;
	 git bisect bad "$tgt" >/dev/null;
	-git merge-base "$branch" "$tgt" \
	+cat "$branch" \
	+| xargs -I{} git merge-base {} "$tgt" \
	 | xargs -I{} git bisect good {};
	 git bisect run "$callback";
	 git rev-list -1 bisect/bad \
	@@ -154,7 +171,8 @@ git rev-list -1 bisect/bad \
	 git bisect reset >/dev/null 2>/dev/null;
	 
	 # Perform the conflicting rebase
	-git switch "$branch";
	+cat "$branch" \
	+| xargs -I{} git checkout {};
	 # shellcheck disable=SC2086  # gropts may hold multiple options
	 git rebase $gropts "$bad";
	 if test -v post; then
I've tested it, and it works fine with a detached HEAD.

Cheers, Alex

> 
> Nico
> -- 
-- 
<https://www.alejandro-colomar.es>
Nico WilliamsOct 3, 2026, 22:19 UTC in reply to Alejandro Colomar on lore

Re: [RFC] git-brebase

On Sat, Oct 03, 2026 at 11:38:29PM +0200, Alejandro Colomar wrote:
Show 6 quoted lines
> > I'd have an option or sub-command of the main script that says "do the
> > callback thing", then when you run `git bisect run ...` put in the name
> > of this script as the command and the "do the callback thing" option
> > next.
> 
> I'd need to see some code.  I'm not seeing it.  :)
Warning: NOT TESTED.
Warning: I did not first adopt your other patch to support detached HEAD
mode.
Look, no temp file in sight:
	@@ -1,164 +1,164 @@
	 #!/bin/bash
	 # Copyright 2026, Alejandro Colomar <alx@kernel.org>
	 # SPDX-License-Identifier: GPL-3.0-or-later
	 
	 set -Eeufo pipefail;
	 shopt -s lastpipe;
	 
	 err()
	 {
	 	>&2 printf '%s\n' "$(basename "$0"): error: $*";
	 	exit 1;
	 }
	 
	 fp='';
	 other='';
	 pre='';
	 post='';
	+callback=false;
	 while test $# -ge 1; do
	 	case "$1" in
	 	--first-parent)
	 		fp='--first-parent';
	 		;;
	 	--pre-exec=*)
	 		echo "$1" \
	 		| sed 's/--pre-exec=//' \
	 		| read -r pre;
	 		;;
	 	--post-exec=*)
	 		echo "$1" \
	 		| sed 's/--post-exec=//' \
	 		| read -r post;
	 		;;
	+	--bisect-run-callback)
	+		callback=true
	+		break;;
	 	-*)
	 		other="$other $1";
	 		;;
	 	*)
	 		break;
	 		;;
	 	esac;
	 	shift;
	 done;
	-gbopts="$fp";
	-gropts="$other";
	 
	-if test $# -lt 1; then
	-	err 'Missing target commit.';
	-fi;
	-if test $# -gt 1; then
	-	err 'Too many arguments.';
	-fi;
	-git rev-list -1 "$1" \
	-| read -r tgt;
	-git rev-parse --abbrev-ref HEAD \
	-| read -r branch;
	-
	-# Set up the callback script for 'git rebase run'.
	-mktemp \
	-| read -r callback;
	-cat >"$callback" <<__EOF__
	-#!/bin/bash
	-
	-	set -Eeufo pipefail;
	-	shopt -s lastpipe;
	+if $callback; then
	+	# Positional arguments to the bisect run callback
	+	branch="$1"
	+	gropts="$2"
	+	pre="${3:-}"
	+	post="${4:-}"
	 
	 	git rev-list -1 HEAD \
	 	| read -r bisect_head;
	 
	-	if test -n '$pre'; then
	+	if test -n "$pre"; then
	 		printf '%s' 'Pre-rebase exec: ';
	-		pre='$pre';
	 		if
	-			\$pre;
	-			x="\$?";
	+			$pre;
	+			x="$?";
	 			true;
	 		then
	-			case "\$x" in
	+			case "$x" in
	 			0)
	 				echo 'success';
	 				;;
	 			125)
	 				echo 'skip';
	-				git checkout --detach "\$bisect_head" 2>/dev/null;
	+				git checkout --detach "$bisect_head" 2>/dev/null;
	 				exit 125;
	 				;;
	 			*)
	-				echo "failure (\$x)";
	-				git checkout --detach "\$bisect_head" 2>/dev/null;
	-				exit "\$x";
	+				echo "failure ($x)";
	+				git checkout --detach "$bisect_head" 2>/dev/null;
	+				exit "$x";
	 				;;
	 			esac;
	 		fi;
	 	fi;
	 
	-	git switch '$branch' >/dev/null 2>/dev/null;
	+	git switch "$branch" >/dev/null 2>/dev/null;
	 	git rev-list -1 HEAD \
	 	| read -r old_head;
	 	printf '%s' 'Rebase: ';
	-	if git rebase $gropts "\$bisect_head" >/dev/null 2>/dev/null; then
	+	if git rebase $gropts "$bisect_head" >/dev/null 2>/dev/null; then
	 		echo 'success';
	 	else
	 		echo 'conflict';
	 		git rebase --abort >/dev/null;
	-		git checkout --detach "\$bisect_head" 2>/dev/null;
	+		git checkout --detach "$bisect_head" 2>/dev/null;
	 		exit 1;
	 	fi;
	 
	-	if test -n '$post'; then
	+	if test -n "$post"; then
	 		printf '%s' 'Post-rebase exec: ';
	-		post='$post';
	 		if
	-			\$post;
	-			x="\$?";
	+			$post;
	+			x="$?";
	 			true;
	 		then
	-			case "\$x" in
	+			case "$x" in
	 			0)
	 				echo 'success';
	 				;;
	 			125)
	 				echo 'skip';
	-				git reset --hard "\$old_head";
	-				git checkout --detach "\$bisect_head" 2>/dev/null;
	+				git reset --hard "$old_head";
	+				git checkout --detach "$bisect_head" 2>/dev/null;
	 				exit 125;
	 				;;
	 			*)
	 				echo "failure (\$x)";
	-				git reset --hard "\$old_head";
	-				git checkout --detach "\$bisect_head" 2>/dev/null;
	-				exit "\$x";
	+				git reset --hard "$old_head";
	+				git checkout --detach "$bisect_head" 2>/dev/null;
	+				exit "$x";
	 				;;
	 			esac;
	 		fi;
	 	fi;
	-	git checkout --detach "\$bisect_head" 2>/dev/null;
	+	git checkout --detach "$bisect_head" 2>/dev/null;
	 	exit 0;
	-__EOF__
	-chmod +x "$callback";
	+fi
	+
	+gbopts="$fp";
	+gropts="$other";
	+
	+if test $# -lt 1; then
	+	err 'Missing target commit.';
	+fi;
	+if test $# -gt 1; then
	+	err 'Too many arguments.';
	+fi;
	+git rev-list -1 "$1" \
	+| read -r tgt;
	+git rev-parse --abbrev-ref HEAD \
	+| read -r branch;
	 
	 # Try the target first.
	 git checkout --detach "$tgt" 2>/dev/null;
	-if "$callback"; then
	+if "$callback" "$branch" "$gropts" "$pre" "$post"; then
	 	exit 0;
	 fi;
	 git status;
	 
	 # Bisect.
	 # shellcheck disable=SC2248  # gbopts may hold multiple options
	 git bisect start $gbopts >/dev/null;
	 git bisect bad "$tgt" >/dev/null;
	 git merge-base "$branch" "$tgt" \
	 | xargs -I{} git bisect good {};
	-git bisect run "$callback";
	+git bisect run "$0" --bisect-run-callback "$branch" "$gropts" "$pre" "$post";
	 git rev-list -1 bisect/bad \
	 | read -r bad;
	 git bisect reset >/dev/null 2>/dev/null;
	 
	 # Perform the conflicting rebase
	 git switch "$branch";
	 # shellcheck disable=SC2086  # gropts may hold multiple options
	 git rebase $gropts "$bad";
	 if test -v post; then
	 	echo 'Running post-rebase exec.';
	 	$post;
	 fi;
Show 11 quoted lines
> > > > and use environment
> > > > variables to pass arguments to it.
> > > 
> > > The callback doesn't really need any arguments, since 'git bisect run'
> > > won't pass any arguments to it.
> > 
> > But you're embedding values into the temp executable script -- if you
> > don't have that any more you'll have to pass those in.
> 
> But why would we want to not have it?
> That would complicate the script, no?

Because I don't want it writing temp files unless absolutely necessary. Even with a `trap` this can leave garbage behind. Better to avoid it.

Plus I... just don't like that style of bash scripting, and sure, that's just personal preference.

Nico
Nico WilliamsOct 3, 2026, 21:13 UTC in reply to Alejandro Colomar on lore

Re: [RFC] git-brebase

On Sat, Oct 03, 2026 at 10:56:25PM +0200, Alejandro Colomar wrote:
Show 8 quoted lines
> > I also have a getopts_long-like function (see my gists) for bash if you
> > like.
> 
> I think getopts(1) is not usable for git(1)-related scripts, because
> getopts(1) interprets '--' as the end of the options, but git(1) uses it
> for distinguishing commits from paths.  If anyone shows me how it can be
> used, I'd be interested, because I've hit this issue in the past with
> other script.
https://gist.github.com/nicowilliams/f3fe2b10b380aecdef403acb246dced2
Though there's many ways to do this.
Show 10 quoted lines
> > > cat >"$callback" <<__EOF__
> > > #!/bin/bash
> > > ...
> > > __EOF__
> > > chmod +x "$callback";
> > 
> > Here what might be better is to have a command-line option to execute
> > this callback without having to write it to a file,
> 
> How would you do it?

I'd have an option or sub-command of the main script that says "do the callback thing", then when you run `git bisect run ...` put in the name of this script as the command and the "do the callback thing" option next.

Show 5 quoted lines
> > and use environment
> > variables to pass arguments to it.
> 
> The callback doesn't really need any arguments, since 'git bisect run'
> won't pass any arguments to it.

But you're embedding values into the temp executable script -- if you don't have that any more you'll have to pass those in.

> > which means I can't use this in detached HEAD mode :(
> 
> Oh!  I wasn't aware that git-rebase(1) supported detached HEAD mode.
Sure does!
Show 6 quoted lines
> > I work in detached HEAD mode almost exclusively.  I know, that's..
> > weird.  But it works for me.
> 
> Ouch!  Indeed.  :)
> Out of curiosity, are there any interesting reasons for such
> self-implied pain?
I often do:

: ; git checkout origin/master : ; <do some work> : ; git add ...; git commit -m '...' : ; git push myfork HEAD:refs/heads/the-branch-name-here # <-- I name it here

then open a PR.

Now I don't have a branch here, but who cares? If I switch to other work and later want to come back to this work I'll either a) create a local branch then, and/or b) when I resume work on the first thing I'll `git checkout myfork/the-branch-name-here` and... once more work in detached HEAD mode.

And if I need to see "what was I doing?" then I use `git log --oneline` and `git reflog` and I quickly see the remote branch of interest.

The remote branches are the symbolic names I need to preserve, and my clone will know them, so I only need local branch names for things I work on w/o a network or over a long time.

I do exaggerate a bit. I do this a lot, but maybe not quite "almost exclusively". Often I'm forced to have a local branch by opinionated tools other than git itself.

Nico
Nico WilliamsOct 3, 2026, 21:50 UTC in reply to Alejandro Colomar on lore

Re: [RFC] git-brebase

On Sat, Oct 03, 2026 at 11:29:40PM +0200, Alejandro Colomar wrote:
Show 7 quoted lines
> I'm currently defaulting to brebase for every rebase I want to do.
> 
> However, I still use the regular git-rebase(1) for more precise
> operations.  To be specific, I use it for changing history (without
> moving the base), and I also use it for resolving conflicts as part of
> a git-brebase operation.  They seem to me to be different tools, even
> though they're clearly related.

I see now. Though, of course, if I use git-rebase but don't change the base, then obviously I want a plain rebase. But, ok.

Back to recent threads