Search code examples
bashgitshellgit-rebase

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


I know how to interactively modify a git history and drop a specific commit but I didn't find a way to do that fully automatically.

I'd like to identify specific commits (e.g. containing a magic string) and have them deleted by a script.

Something like this:

# 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

Any ideas?


Solution

  • Taken from 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