9

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

Jan Rüegg
  • 9,587
  • 8
  • 63
  • 105
  • So you know a command-line way of finding it, but want to find a way to do it in Sourcetree specifically? I don't think it has that feature. – tom Dec 21 '15 at 16:31
  • Yes! (Well, gitk is actually the built-in git gui, but it seems inferior to sourcetree except in this aspect...) – Jan Rüegg Dec 21 '15 at 16:33

2 Answers2

10

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
Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
  • Thanks for your help! I clarified the question with a screenshot, to show which part of gitk I'd like to "find" in SourceTree. But maybe it is easier after all to not use sourcetree for that feature, as you described... – Jan Rüegg Dec 22 '15 at 08:37
  • As mentioned in the comment for the other answer, this does not work, either! I want to know which branches the current branch has been merged into, not the other way around! – Jan Rüegg Jan 07 '16 at 11:59
8

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.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Unfortunately, I found out that this does not work after all. I want to show all branches this branch is merged into, not the other way around. (For example, if I merge the current branch into master, with your solution it will not show "master" in the list). Gitk does this correctly... – Jan Rüegg Jan 07 '16 at 11:57
  • @JanRüegg so something like (pseudo-syntax): `for each branches: (git branch --merged abranch|grep currentBranch)` – VonC Jan 07 '16 at 12:04
  • @JanRüegg I have edited the answer with a script which should list "all branches this branch is merged into". Can you test it? It works on my side. – VonC Jan 07 '16 at 13:05
  • Wow, that was fast, thank you very much! I manage to get the script to run through sourcetree, but then I get many errors like this: "error: unknown option `merged' usage: git for-each-ref [options] [] ...". I'm still trying to figure out what's the problem... – Jan Rüegg Jan 07 '16 at 13:13
  • @JanRüegg please make sure you have git 2.7.0 installed. – VonC Jan 07 '16 at 13:14
  • Actually, the script only seems to work for local branches, and not remote branches... any idea what the problem could be? – Jan Rüegg Jan 12 '16 at 10:29
  • @JanRüegg Because I used `refs/heads`: you need to do the same for `refs/remotes` – VonC Jan 12 '16 at 10:34