Using git filter-branch to remove commits by their commit message

后端 未结 2 468
情歌与酒
情歌与酒 2020-12-15 10:57

Simple Version:

If I have a branch \"foo-555\", with a bunch of commits with messages like:

  • foo 555: blah
  • foo 123: blah blah<
相关标签:
2条回答
  • 2020-12-15 11:44

    Write a script to remove lines with Redmine #555:

    #!/bin/sh
    
    mv $1 $1.$$
    grep -v 'Redmine #555' < $1.$$ > $1
    rm -f $1.$$
    

    Of course you can do that however you want (eg echo a script of commands to ed).

    Then launch your rebase with EDITOR set to your script:

    EDITOR=/path/to/script git rebase -i REVISION
    

    Of course it still won't be guaranteed to complete -- there may be errors during the rebase caused by leaving out revisions. You can still fix them and git rebase --continue manually.

    0 讨论(0)
  • 2020-12-15 11:51

    Here's a fast solution that uses filter-branch instead of rebasing. There's no interactivity or needing to resolve conflicts.

    git filter-branch --commit-filter '
        if [ `git rev-list --all --grep "<log-pattern>" | grep -c "$GIT_COMMIT"` -gt 0 ]
        then
            skip_commit "$@";
        else
            git commit-tree "$@";
        fi'  HEAD
    

    You'll probably want to then clean up with:

    git reflog expire --expire=now
    git gc --prune=now
    
    0 讨论(0)
提交回复
热议问题