Let\'s say I have the following local repository with a commit tree like this:
master --> a
\\
\\
develop c --> d
Another way to phrase the question is "What is the nearest commit that resides on a branch other than the current branch, and which branch is that?"
You can find it with a little bit of command line magic
git show-branch \
| sed "s/].*//" \
| grep "\*" \
| grep -v "$(git rev-parse --abbrev-ref HEAD)" \
| head -n1 \
| sed "s/^.*\[//"
With awk:
git show-branch -a \
| grep '\*' \
| grep -v `git rev-parse --abbrev-ref HEAD` \
| head -n1 \
| sed 's/[^\[]*//' \
| awk 'match($0, /\[[a-zA-Z0-9\/-]+\]/) { print substr( $0, RSTART+1, RLENGTH-2 )}'
Running the above code on
A---B---D <-master
\
\
C---E---I <-develop
\
\
F---G---H <-topic
Will give you develop if you run it from H and master if you run it from I.
The code is available as a gist