You can use the git add -e
command and pass it as a script editor (in the example below it is called filterpatch
), which will edit the patch to your requirements (see the "EDITING PATCHES" section in the git add
documentation):
EDITOR="filterpatch 10..20" git add -e some_file.txt
For convenience, you can add the git alias as follows:
git config alias.manual-add '!EDITOR="filterpatch $2" git add -e $1; :'
An example of a silly filterpatch
script that adds the foo
prefix to all added lines in the specified patch range:
#!/bin/bash -x sed -i "$1 s/^+\([^+]\)/+foo \1/" "$2"
Usage example:
git manual-add some_file.txt 13,16
So, the rest is about the correct implementation of the filterpatch
script - it should analyze the diff and cancel the selection that does not belong to the target range.
source share