# Programmatically edit the git rebase sequence?

6 messages from 2026-07-03 to 2026-07-08. Participants: Matthias Beyer, Michal Suchánek, brian m. carlson, Matt Hunter, D. Ben Knoble, Johannes Schindelin.
Thread: https://gitlist.dev/t/65919

## Matthias Beyer, 2026-07-03 12:02

Subject: Programmatically edit the git rebase sequence?
Message-ID: <akei64goQf3nFhX4@hikari>

```
Hi git people,

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

Example use case:

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"

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

```

## Michal Suchánek, 2026-07-03 12:17

Subject: Re: Programmatically edit the git rebase sequence?
Message-ID: <akeoaWAkrENbpi1G@kunlun.suse.cz>
In-Reply-To: <akei64goQf3nFhX4@hikari>

```
On Fri, Jul 03, 2026 at 02:02:33PM +0200, Matthias Beyer wrote:
> Hi git people,
> 
> in a recent conversation at work, the question of how to
> programmatically edit the git-rebase sequence came up.
> 
> Example use case:
> 
> 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"
> 
> 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!

Hello,

you could probably set a script as EDITOR (environment variable). Not
great for one-off scripts I guess.

Thanks

Michal

```

## brian m. carlson, 2026-07-03 13:42

Subject: Re: Programmatically edit the git rebase sequence?
Message-ID: <ake8OAIyK-ELs-fU@fruit.crustytoothpaste.net>
In-Reply-To: <akei64goQf3nFhX4@hikari>

```
On 2026-07-03 at 12:02:33, Matthias Beyer wrote:
> Hi git people,
> 
> in a recent conversation at work, the question of how to
> programmatically edit the git-rebase sequence came up.
> 
> Example use case:
> 
> 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"
> 
> 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 :-)

Yes, such a thing exists.  You want `GIT_SEQUENCE_EDITOR`, which is an
`EDITOR`-like command that edits the rebase list in place.  So tools
like `ed`, `ex`, `sed -i`, `perl -i`, or `ruby -i` would be useful here.

So you might want something like this (untested):

    GIT_SEQUENCE_EDITOR="perl -pi -e 's/^pick ($(git log master..mybranch --diff-filter=M --format="%h" -- "./subdir/*.rs" | paste -d '\''|'\'' -s -))/edit \$1/'" \
    git rebase -i master

Note the use of `%h`, since by default the object IDs are abbreviated.

If you want something simpler, you can also write a shell script which
edits the first argument in place and specify that.  Arbitrary shell is
allowed in `GIT_SEQUENCE_EDITOR`, much like in `EDITOR` and `VISUAL`.

I personally use this alias, which explicitly does not edit the sequence
list, to automatically squash in all squash and fixup commits without
prompting:

    srebase = "!f() { GIT_SEQUENCE_EDITOR=true git rebase -m -i --autosquash \"$@\"; };f"
-- 
brian m. carlson (they/them)
Toronto, Ontario, CA

```

## Matt Hunter, 2026-07-03 14:33

Subject: Re: Programmatically edit the git rebase sequence?
Message-ID: <DJP0D7IZJ8WX.S1DDPT3KTQ95@lfurio.us>
In-Reply-To: <ake8OAIyK-ELs-fU@fruit.crustytoothpaste.net>

```
On Fri Jul 3, 2026 at 9:42 AM EDT, brian m. carlson wrote:
> On 2026-07-03 at 12:02:33, Matthias Beyer wrote:
>> 
>> 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 :-)
>
> Yes, such a thing exists.  You want `GIT_SEQUENCE_EDITOR`, which is an
> `EDITOR`-like command that edits the rebase list in place.  So tools
> like `ed`, `ex`, `sed -i`, `perl -i`, or `ruby -i` would be useful here.

Interesting tip - thanks!

>
> I personally use this alias, which explicitly does not edit the sequence
> list, to automatically squash in all squash and fixup commits without
> prompting:
>
>     srebase = "!f() { GIT_SEQUENCE_EDITOR=true git rebase -m -i --autosquash \"$@\"; };f"

'git rebase --autosquash' does work now without the need to invoke
--interactive mode.  I believe we got this with f8f87e082798
(Merge branch 'ak/rebase-autosquash')

```

## D. Ben Knoble, 2026-07-03 15:31

Subject: Re: Programmatically edit the git rebase sequence?
Message-ID: <CALnO6CCaAaVTDABQT9APasnhmvL3w_C1VKo3UYhgDt29OYPmFg@mail.gmail.com>
In-Reply-To: <ake8OAIyK-ELs-fU@fruit.crustytoothpaste.net>

```
On Fri, Jul 3, 2026 at 9:46 AM brian m. carlson
<sandals@crustytoothpaste.net> wrote:
>
> On 2026-07-03 at 12:02:33, Matthias Beyer wrote:
> > 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 :-)
>
> Yes, such a thing exists.  You want `GIT_SEQUENCE_EDITOR`, which is an
> `EDITOR`-like command that edits the rebase list in place.  So tools
> like `ed`, `ex`, `sed -i`, `perl -i`, or `ruby -i` would be useful here.
>
> So you might want something like this (untested):
>
>     GIT_SEQUENCE_EDITOR="perl -pi -e 's/^pick ($(git log master..mybranch --diff-filter=M --format="%h" -- "./subdir/*.rs" | paste -d '\''|'\'' -s -))/edit \$1/'" \
>     git rebase -i master

Yep. Although, the last time I wrote a program that used
GIT_SEQUENCE_EDITOR, I had to deal with enough shell-nesting that it
was more convenient to make the editor program separate:

- git-split-topic [1] sets up a sequence editor with some interpolated
arguments that also re-invokes the original
- split-topic-editor [2] pre-processes the rebase script with ed

[1]: https://github.com/benknoble/Dotfiles/blob/ca48a09f783b78e038a41c5d60ee6b163337f580/links/bin/git-split-topic#L47-L53
[2]: https://github.com/benknoble/Dotfiles/blob/master/links/bin/split-topic-editor

See the comments in [1] for some weirdness in the invocation of the
sequence editor, where it gets "$@" appended to the command string
(meaning the last command in a chain might need to be written
specially).

And yes, I'm sure there's a few ways for things to go wrong with the
way some of the shell script variables are embedded into strings for
another shell to evaluate later; if I rewrote with Zsh, I could at
least use the ${(q)var} forms to perhaps handle that better…

-- 
D. Ben Knoble

```

## Johannes Schindelin, 2026-07-08 17:58

Subject: Re: Programmatically edit the git rebase sequence?
Message-ID: <32e8c83a-69f3-8ef3-8d9c-f9860fda2fbb@gmx.de>
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
> 

```
