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

Posted by machineghost on Stack Overflow See other posts from Stack Overflow or by machineghost
Published on 2010-12-29T21:48:36Z Indexed on 2010/12/29 21:54 UTC
Read the original article Hit count: 253

Filed under:

In our repository we have a convention where every commit message starts with a certain pattern:

Redmine #555: SOME_MESSAGE

We also do a bit of rebasing to bring in the potential release branch's changes to a specific issue's branch. In other words, I might have branch "foo-555", but before I merge it in to branch "pre-release" I need to get any commits that pre-release has that foo-555 doesn't (so that foo-555 can fast-forward merge in to pre-release).

However, because pre-release sometimes changes, we sometimes wind up with situations where you bring in a commit from pre-release, but then that commit later gets removed from pre-release. It's easy to identify commits that came from pre-release, because the number from their commit message won't match the branch number; for instance, if I see "Redmine #123: ..." in my foo-555 branch, I know that its not a commit from my branch.

So now the question: I'd like to remove all of the commits that "don't belong" to a branch; in other words, any commit that:

  • Is in my foo-555 branch, but not in the pre-release branch (pre-release..foo-555)
  • Has a commit message that doesn't start with "Redmine #555"

but of course "555" will vary from branch to branch. Is there any way to use filter-branch (or any other tool) to accomplish this? Currently the only way I can see to do it is to do go an interactive rebase ("git rebase -i") and manually remove all the "bad" commits.

© Stack Overflow or respective owner

Related posts about git