Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 596
Jan Rüegg Avatar asked Dec 14 '15 15:12

Jan Rüegg


People also ask

How do you check if branch is merged or not?

You can use the git merge-base command to find the latest common commit between the two branches. If that commit is the same as your branch head, then the branch has been completely merged.

How do you list the branches that have been merged?

With git branch --merged <commit> , your local list of branches will be filtered by all the branches who have been merged into a given branch or commit. Similar to above, you could type git branch --no-merged <commit> and only the branches not merged into the named commit would be listed.

Do branches disappear after merge?

In a good workflow, the feature branch is deleted once its merged back into master. New branches should be created for each new feature(s) that you work on.


2 Answers

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
like image 195
Enrico Campidoglio Avatar answered Oct 04 '22 05:10

Enrico Campidoglio


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.

like image 33
VonC Avatar answered Oct 04 '22 04:10

VonC