Git development
 help / color / mirror / Atom feed
From: Johannes Schindelin <Johannes.Schindelin@gmx.de>
To: Matthias Beyer <mail@beyermatthias.de>
Cc: git@vger.kernel.org, neikos@neikos.email
Subject: Re: Programmatically edit the git rebase sequence?
Date: Wed, 8 Jul 2026 19:58:24 +0200 (CEST)	[thread overview]
Message-ID: <32e8c83a-69f3-8ef3-8d9c-f9860fda2fbb@gmx.de> (raw)
In-Reply-To: <akei64goQf3nFhX4@hikari>

Hi Matthias,

On Fri, 3 Jul 2026, Matthias Beyer wrote:

> in a recent conversation at work, the question of how to
> programmatically edit the git-rebase sequence came up.

The easiest way is probably to have the interactive rebase stop at the top
of the rebase script:

	# insert a `b` (for `break`) before the first line
	GIT_SEQUENCE_EDITOR="sed -i 1ib" git rebase -i ...

(Note that this is GNU sed syntax, with BSD/macOS sed you'll have to get a
bit creative, or write a real script and refer to it via the path.)

Then you can use a script to transform the rebase script before `git
rebase --continue`.

I frequently use this technique with LLMs because they seem to be unable
to control an interactive editor correctly.

> I have a branch that touches a number of files, adds some files and
> removes some files.
> When rebasing, I want to split all commits that touched a certain subset
> of files, for the clearity of the history.
> 
> I look at the output of
> 
>     git log master..mybranch --oneline --diff-filter=M -- "./subdir/*.rs"

Noting that the OIDs in $(git rev-parse --git-path
rebase-merge/git-rebase-todo) are expanded, you could match them via the
OIDs obtained using `rev-list` instead of `log`, to enumerate the OIDs
instead of full commit history, then turning that into some kind of regex.
Something along the lines

	regex=$(git rev-list master..mybranch --diff-filter=M -- "subdir/*.rs" |
		# read all lines, then turn newlines into `\|`
		sed ':1;$b2;N;b1;:2;s/\n/\\|/g')

	GIT_REBASE_TODO="$(git rev-parse --git-path rebase-merge/git-rebase-todo)"
	test -z "$regex" ||
	sed -i "s/^pick \($regex\)/edit \1/" $GIT_REBASE_TODO

This _should_ work, but you'll most likely want to adapt it to your
particular use cases.

Ciao,
Johannes

> 
> to find all commits in that subdir that only touched the files. All of
> these commits are to be "edit"ed.
> 
> Now I fire up `git rebase -i master` and manually(!) match the list from
> above `git-log` call and find the respective commits to edit them.
> 
> Is there a way I am not aware of to do that manual step programatically?
> Something like
> 
>     git rebase -i master --edit-commits="$(git log master..mybranch --diff-filter=M --format="%H" -- "./subdir/*.rs")"
> 
> would be convenient here, although I would understand if that is too
> much clutter for the already very heavy git CLI interface :-)
> 
> Maybe I am just not aware of the obvious solution - I would be happy to
> learn that there is already one!
> 
> Best,
> Matthias
> 

      parent reply	other threads:[~2026-07-08 17:58 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-03 12:02 Programmatically edit the git rebase sequence? Matthias Beyer
2026-07-03 12:17 ` Michal Suchánek
2026-07-03 13:42 ` brian m. carlson
2026-07-03 14:33   ` Matt Hunter
2026-07-03 15:31   ` D. Ben Knoble
2026-07-08 17:58 ` Johannes Schindelin [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=32e8c83a-69f3-8ef3-8d9c-f9860fda2fbb@gmx.de \
    --to=johannes.schindelin@gmx.de \
    --cc=git@vger.kernel.org \
    --cc=mail@beyermatthias.de \
    --cc=neikos@neikos.email \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox