Skip to content

Instantly share code, notes, and snippets.

@evilbuck
Created October 6, 2023 15:48
Show Gist options
  • Select an option

  • Save evilbuck/5079af0b60653e9fb16fff47d2c6ca2d to your computer and use it in GitHub Desktop.

Select an option

Save evilbuck/5079af0b60653e9fb16fff47d2c6ca2d to your computer and use it in GitHub Desktop.
Git: Find the nearest ancestor branch
# 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?"
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 )}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment