# Idea: Add a filter option to 'git rebase'

4 messages from 2017-01-20 to 2017-01-23. Participants: Philip Oakley, Thomas Braun, Johannes Schindelin.
Thread: https://gitlist.dev/t/44925

## Philip Oakley, 2017-01-20 22:28

Subject: Idea: Add a filter option to 'git rebase'
Message-ID: <8AED6D90D2B64AE3A63C6195CA983FE8@PhilipOakley>
URL: https://gitlist.dev/e/8AED6D90D2B64AE3A63C6195CA983FE8%40PhilipOakley

```
A recent question on stackoverflow 
http://stackoverflow.com/questions/41753252/drop-commits-by-commit-message-in-git-rebase 
sought to remove automatically commits that could be identified by relevant 
words in the commit message.

I had thought that the ubiquitous `git filter-branch` should be able to do 
this sort of thing. I was wrong. (It was pointed out to me that...) The man 
page notes that removing a commit via filter-branch does not remove the 
changes from following commits and directs readers to using `git rebase(1)`.

However the rebase command does not have any filter option to allow the 
automatic population of its TODO list with the appropriate 
pick/edit/drop/etc. values.

It does feel as if a --filter style option would be a useful addition to 
rebase to complement the filter-branch options once the current conversion 
from script to code is complete.

Is this something that should be put in the 'worth considering' pile?

--
Philip 


```

## Thomas Braun, 2017-01-20 23:35

Subject: Re: Idea: Add a filter option to 'git rebase'
Message-ID: <0bd00cda-65d0-eeba-d8b9-a839e76d8e88@virtuell-zuhause.de>
URL: https://gitlist.dev/e/0bd00cda-65d0-eeba-d8b9-a839e76d8e88%40virtuell-zuhause.de
In-Reply-To: <8AED6D90D2B64AE3A63C6195CA983FE8@PhilipOakley>

```
Am 20.01.2017 um 23:28 schrieb Philip Oakley:
> A recent question on stackoverflow
> http://stackoverflow.com/questions/41753252/drop-commits-by-commit-message-in-git-rebase
> sought to remove automatically commits that could be identified by
> relevant words in the commit message.
> 
> I had thought that the ubiquitous `git filter-branch` should be able to
> do this sort of thing. I was wrong. (It was pointed out to me that...)
> The man page notes that removing a commit via filter-branch does not
> remove the changes from following commits and directs readers to using
> `git rebase(1)`.
> 
> However the rebase command does not have any filter option to allow the
> automatic population of its TODO list with the appropriate
> pick/edit/drop/etc. values.

Well you can use an arbitrary shell command as editor, so something like

$ GIT_SEQUENCE_EDITOR="sed -i -re 's/^pick /edit /'" git rebase -i master

will change pick to edit of all commits.

Maybe that can be mentioned in the man page of rebase?

```

## Philip Oakley, 2017-01-21 23:18

Subject: Re: Idea: Add a filter option to 'git rebase'
Message-ID: <7EEBBEA4046743C9AE04AE50EE30D72A@PhilipOakley>
URL: https://gitlist.dev/e/7EEBBEA4046743C9AE04AE50EE30D72A%40PhilipOakley
In-Reply-To: <0bd00cda-65d0-eeba-d8b9-a839e76d8e88@virtuell-zuhause.de>

```
From: "Thomas Braun"  Friday, January 20, 2017 11:35 PM
> Am 20.01.2017 um 23:28 schrieb Philip Oakley:
>> A recent question on stackoverflow
>> http://stackoverflow.com/questions/41753252/drop-commits-by-commit-message-in-git-rebase
>> sought to remove automatically commits that could be identified by
>> relevant words in the commit message.
>>
>> I had thought that the ubiquitous `git filter-branch` should be able to
>> do this sort of thing. I was wrong. (It was pointed out to me that...)
>> The man page notes that removing a commit via filter-branch does not
>> remove the changes from following commits and directs readers to using
>> `git rebase(1)`.
>>
>> However the rebase command does not have any filter option to allow the
>> automatic population of its TODO list with the appropriate
>> pick/edit/drop/etc. values.
>
> Well you can use an arbitrary shell command as editor, so something like
>
> $ GIT_SEQUENCE_EDITOR="sed -i -re 's/^pick /edit /'" git rebase -i master
>
> will change pick to edit of all commits.
>
> Maybe that can be mentioned in the man page of rebase?
>

I had been more thinking of a process that passed single sha1's to the 
filter on each pass through the rebase list, so that the coding was simpler, 
plus the --interactive could be used, if required, for final refinement 
(gitk being handy for that).

However, a mention in the man pages would be zero code cost, and could help.

--

Philip 


```

## Johannes Schindelin, 2017-01-23 11:57

Subject: Re: Idea: Add a filter option to 'git rebase'
Message-ID: <alpine.DEB.2.20.1701231214190.3469@virtualbox>
URL: https://gitlist.dev/e/alpine.DEB.2.20.1701231214190.3469%40virtualbox
In-Reply-To: <7EEBBEA4046743C9AE04AE50EE30D72A@PhilipOakley>

```
Hi,

On Sat, 21 Jan 2017, Philip Oakley wrote:

> From: "Thomas Braun"  Friday, January 20, 2017 11:35 PM
> > Am 20.01.2017 um 23:28 schrieb Philip Oakley:
> > > A recent question on stackoverflow
> > > http://stackoverflow.com/questions/41753252/drop-commits-by-commit-message-in-git-rebase
> > > sought to remove automatically commits that could be identified by
> > > relevant words in the commit message.

Interesting. As long as those magic words appear in the oneline, I would
simply call the command ":%s/^pick.*<magic-word>/#&/".

But that is neither scripted nor does it work with magic words elsewhere
in the commit message (let alone more sophisticated filtering based on,
say, authorship or magic words in the diff's "+" lines).

> > > I had thought that the ubiquitous `git filter-branch` should be able
> > > to do this sort of thing. I was wrong. (It was pointed out to me
> > > that...) The man page notes that removing a commit via filter-branch
> > > does not remove the changes from following commits and directs
> > > readers to using `git rebase(1)`.

Right, filter-branch never cherry-picks patches, but instead complete
trees.

> > > However the rebase command does not have any filter option to allow
> > > the automatic population of its TODO list with the appropriate
> > > pick/edit/drop/etc. values.
> >
> > Well you can use an arbitrary shell command as editor, so something
> > like
> >
> > $ GIT_SEQUENCE_EDITOR="sed -i -re 's/^pick /edit /'" git rebase -i master
> >
> > will change pick to edit of all commits.
> >
> > Maybe that can be mentioned in the man page of rebase?

Please note that using a script instead of a command, you can do really
sophisticated things including sophisticated re-writing of the edit script
as well as calling the GIT_EDITOR in the end to re-establish
interactivity. I do exactly that in my Git garden shears [*1*].

> I had been more thinking of a process that passed single sha1's to the
> filter on each pass through the rebase list, so that the coding was
> simpler, plus the --interactive could be used, if required, for final
> refinement (gitk being handy for that).
> 
> However, a mention in the man pages would be zero code cost, and could
> help.

In the (frustratingly) long run, we will want to add a command-line option
that allows overriding the edit script generation, paired with a
command-line option that generates the current version of the edit script
(in my rebase--helper work that is still under review, the latter option
already exists).

That way, you have full flexibility and could even generate the entire
edit script from scratch, rather than letting rebase -i generate one and
then filtering it.

Ciao,
Johannes

Footnote *1*:
https://github.com/git-for-windows/build-extra/blob/master/shears.sh

```
