Let\'s say I have the following local repository with a commit tree like this:
master --> a
\\
\\
develop c --> d
The solution based on git show-branch did not quite work for me (see below), so I've combined it with the one based on git log and ended up with this:
git log --decorate --simplify-by-decoration --oneline \ # selects only commits with a branch or tag
| grep -v "(HEAD" \ # removes current head (and branch)
| head -n1 \ # selects only the closest decoration
| sed 's/.* (\(.*\)) .*/\1/' \ # filters out everything but decorations
| sed 's/\(.*\), .*/\1/' \ # picks only the first decoration
| sed 's/origin\///' # strips "origin/" from the decoration
log commandmaster and develop results (mostly) in Initial commit A---B---D---E---F <-origin/master, master
\ \
\ \
\ G---H---I <- origin/hotfix, hotfix
\
\
J---K---L <-origin/develop, develop
\
\
M---N---O <-origin/feature/a, feature/a
\ \
\ \
\ P---Q---R <-origin/feature/b, feature/b
\
\
S---T---U <-origin/feature/c, feature/c
Despite local branch existence (e.g. only origin/topic is present since the commit O was checked-out by directly by its SHA), the script should print as follows:
G, H, I (branch hotfix) → masterM, N, O (branch feature/a) → developS, T, U (branch feature/c) → developP, Q, R (branch feature/b) → feature/aJ, K, L (branch develop) → Initial commit *B, D, E, F (branch master) → Initial commit * - or master if develop's commits were on top of master's HEAD (~ the master would be fast-forwardable to develop)
The solution based on git show-branch proved unreliable for me in the following situations:
grep '\*' \ for `grep '!' \ – and that is just the beginning of all the troublesmaster and develop results in develop and `` respectivelymaster branch (hotfix/ branches) end up with the develop as a parent since their closest master branch parent was marked with ! instead of * for a reason.