You can tell if one commit (one of the two you want to revert) is an ancestor of another
git merge-base --is-ancestor <maybe-ancestor-commit> <descendant-commit>
So list every commits of every branch and:
- if none of your 2 commits are ancestor of a branch HEAD, discard the branch
- if they are, start enumerating commit from that branch
You might consider soft resetting that branch to before those commits: the index would be at the desired state: you can add and commit that index as one new commit which would reverse anything done since those commits (ancestors of a given branch)