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?
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.
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.
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.
The equivalent of running gitk --all
in SourceTree would be to choose All Branches from the drop-down list in the upper left corner:
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
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:
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:
And will get you list of branches it has been merged into:
It is a workaround, but at least you don't leave SourceTree.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With