Jan Rüegg
Jan Rüegg

Reputation: 10045

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

Upvotes: 9

Views: 4890

Answers (2)

VonC
VonC

Reputation: 1324258

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.

Upvotes: 8

Enrico Campidoglio
Enrico Campidoglio

Reputation: 59923

The equivalent of running gitk --all in SourceTree would be to choose All Branches from the drop-down list in the upper left corner:

View All Branches in SourceTree

The graph shows which branches have been merged where, just like gitk does.

However, finding out exactly which branches have been merged in the current one – that is the branches whose tips are reachable from HEAD – is more easily done from the command line, as you can simply say:

git branch --merged

If you want, you can also include remote branches in the list by adding the --all option:

git branch --all --merged

Finding out which branches have not been merged in the current one is just as easy:

git branch --no-merged

Upvotes: 10

Related Questions