Check if a branch has been merged to master and hasn't been reverted out

I would like to create a tool that will list out the names of feature branches that have been merged into a branch (e.g. master), but excluding branches whose merges have since been reverted.

For example:

  • Unable to push file to git repository
  • Why does GitHub think my code has differences?
  • Git bash tab completion for .sh files
  • git deploy subfolder of repository
  • Using Git with your CakePHP Project
  • Why is git apply of a patch with binary files not creating the binary files?
  •    /--*--*--*--\                           feature
      /             \
    ------*--*-------*--*--*--*-------------*  master
                     ^     ^
                     |     | git revert -m1 abc1 (#abc2)
                     | git merge --no-ff feature (#abc1)

    I understand that git branch --contains feature will return master, and I understand why, but I would like to know if there is a simple-enough way to exclude branches that have since been reverted out.

    If not, what would be optimal solution? Comparing diffs?

  • How to handle git bisect with external code causing failure
  • Push a tag to a remote repository using Git?
  • Git installation issue in Mountain Lion (OS X 10.8)
  • gitx How do I get my 'Detached HEAD' commits back into master
  • How can I fetch git objects using the smart protocol (raw) over http?
  • bundle install rails “sh.exe has stopped working” on windows 7
  • 3 Solutions collect form web for “Check if a branch has been merged to master and hasn't been reverted out”

    First, from “How can I know in git if a branch has been already merged into master?”, you can check if your feature branch is part of git branch --merged or check git merge-base to find the merged commit.

    Second, the question “Telling if a Git commit is a Merge/Revert commit” does warn us that a revert commit can be hard to find, and only advocate for the revert message boilerplate in the commit message.

    Do the simple search which is

    git log --grep="This reverts commit ${merge_sha}"

    If they hand edited the commit message then you are stuck with something awful like

    matchId=$(git diff  ${merge_sha}^! | grep -v -e "^diff --git" -e "^--- " -e "^+++ " -e "^index " | sha1sum
    for sha in $(git revlist ${merge_sha}..HEAD)
      shaId=$(git diff  -R ${sha}^! | grep -v -e "^diff --git" -e "^--- " -e "^+++ " -e "^index " | sha1sum)
      if [ ${matchId} == ${shaId} ]
          echo "${sha} might be a ninja revert of ${merge_sha}"

    This could obviously be improved a lot to reverse the fields of some of the lines that I am excluding with grep currently. You’d basically be re-implementing git patch-id but allowing it to have the a/b files all reversed (maybe there is an option to allow that already, but I don’t know what it is)

    An easy way to do this would be to simply grep through the log:

    git log | grep "^commit " | grep <commit-id>
    • git log — shows the log of all the commits
    • grep "^commit " — filters by commit ID (since they all start with “commit”)
    • grep <commit-id>— filter by just the commit you care about

    NOTE—This may not work if git changes the commit ID.

    Git Baby is a git and github fan, let's start git clone.