Search code examples
gitgit-mergeatlassian-sourcetreesourcetree

Sourcetree: Find out if a branch is merged


How can I know if a branch (or commit) is merged in SourceTree?

When Using gitk --all, it will show for a commit (or branch) foo all other branches, where foo is already merged into.

To clarify what I mean a screenshot: The encircled (red) area shows all the branches where the current commit is part of. Can this be displayed in SourceTree as well?

Screenshot of gitk


Solution

  • But maybe it is easier after all to not use SourceTree for that feature

    You can use an custom action defined in SourceTree and listing those merged branches.
    That is not as integrated as gitk, but at least, you don't have to switch tool.

    First define an custom action, using $SHA for getting the selected commit:

    Custom action

    It should call a script in your %PATH% called git-bm (see this answer as an example)

    #!/bin/sh
    for branch in $(git for-each-ref --format="%(refname:short)" refs/heads/); do
      if [ "${branch}" != "$1" ]; then
        t=$(git for-each-ref --format="%(refname:short)" --merged "${branch}" refs/heads/|grep -v "${branch}"|grep "$1")
        if [ "${t}" != "" ]; then
          echo "${branch}"
        else
          t=$(git branch --contains "$1" | grep "${branch}")
          if [ "${t}" != "" ]; then
            echo "${branch}"    
          fi
        fi
      fi
    done
    

    That will list all branches from which you can access the current SHA1 (which is to say "all branches in which the current branch has been merged")

    (Note: the syntax git for-each-ref --merged has been introduced in git 2.7.0 only - 4th of January, 2016. See for instance "Is it possible to filter out merged branches in git for-each-ref?")

    Then invoke it on the commit you want:

    invoke custom action

    And will get you list of branches it has been merged into:

    list

    It is a workaround, but at least you don't leave SourceTree.