|git show-branch -a \|
|| grep '\*' \|
|| grep -v `git rev-parse --abbrev-ref HEAD` \|
|| head -n1 \|
|| sed 's/.*\[\(.*\)\].*/\1/' \|
|| sed 's/[\^~].*//'|
|# How it works:|
|# 1| Display a textual history of all commits.|
|# 2| Ancestors of the current commit are indicated|
|# by a star. Filter out everything else.|
|# 3| Ignore all the commits in the current branch.|
|# 4| The first result will be the nearest ancestor branch.|
|# Ignore the other results.|
|# 5| Branch names are displayed [in brackets]. Ignore|
|# everything outside the brackets, and the brackets.|
|# 6| Sometimes the branch name will include a ~2 or ^1 to|
|# indicate how many commits are between the referenced|
|# commit and the branch tip. We don't care. Ignore them.|
the ack commands can be seamlessly replaced with grep, making usage (at least on macs) easier. You also seem to have an extra backtick at the end of line 5.
branch=`git rev-parse --abbrev-ref HEAD` git show-branch -a 2>/dev/null | grep '\*' | grep -v "$branch" | head -n1 | sed 's/.*\[\(.*\)\].*/\1/' | sed 's/[\^~].*//'
I suspect the parent branch's last commit is a merge, making the column show
Obviously you can combine this into a single line if you wish.
This doesn't give the correct answer for me. I think it's necessary to verify the answer by trying an interactive rebase on the answer it gives you, and if it gives you a load of commits that aren't in your branch, it's the wrong answer and you have to guess another branch and try an interactive rebase again.
This has an issue if there are square brackets in the comments here is my alternative
git show-branch -a 2>/dev/null \ | grep '\*' \ | grep -v `git rev-parse --abbrev-ref HEAD` \ | head -n1 \ | perl -ple 's/\[[A-Za-z]+-\d+\][^\]]+$//; s/^.*\[([^~^\]]+).*$/$1/'
(I couldn't get the sed equivalent regexp)
This script does not work. E.g. I merged a branch1 to master and then created branch2 from master. Ideally parent of branch2 is master, but this script always return branch1 as the parent which is not true. It is doing so because branches are all pointers and branch2 is pointing to the merge commit (from branch1 to master). even though that merge commit has 2 parents, only branch1 is returned.
It's not working for me, it is returning only one branch name as parent branch for all 800 branches in my repo I don't know why..?
This did not work for me when I had done something like
The following did give the correct commit hash for me