在 git 中,如何以编程方式 delete/drop 来自历史记录的特定提交(变基)?

In git how do I delete/drop a specific commit from history (rebase) programmatially?

我知道如何交互式修改git历史记录和drop特定提交,但我没有找到完全自动执行此操作的方法.

我想识别特定的提交(例如包含魔术字符串)并通过脚本删除它们。

像这样:

# identify and remove all commits which would be merged to master but should not
for i in $(git --no-pager log --grep='no-push' --pretty=format:"%h" --no-merges HEAD ^master)
do
    echo "Drop commit $i"
    git rebase --drop $i   # <== this is what I want to do
done

有什么想法吗?

摘自here (thanks to jonrsharpe):

# identify and remove all commits which would be merged to master but should not
for i in $(git --no-pager log --grep='no-push' --pretty=format:"%h" --no-merges HEAD ^master)
do
    echo "Drop commit $i"
    git rebase -p --onto $i^ $i
done