Skip to content

Instantly share code, notes, and snippets.

@rubensa
Forked from joechrysler/who_is_my_mummy.sh
Created September 19, 2023 06:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubensa/b7dbaafcad6d23b57aac6411ce6a8535 to your computer and use it in GitHub Desktop.
Save rubensa/b7dbaafcad6d23b57aac6411ce6a8535 to your computer and use it in GitHub Desktop.
Find the nearest parent branch of the current git branch
#!/usr/bin/env zsh
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.
@rubensa
Copy link
Author

rubensa commented Sep 19, 2023

Here's another version: git log --pretty=format:'%D' HEAD^ | grep 'origin/' | head -n1 | sed 's@origin/@@' | sed 's@,.*@@'

The seds at the end are useful to transform something like origin/develop, origin/HEAD, develop into simply develop.

see: https://gist.github.com/joechrysler/6073741?permalink_comment_id=3108387#gistcomment-3108387

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment