Skip to content

Instantly share code, notes, and snippets.

@psema4
Forked from intel352/git_branch_parent.sh
Last active May 30, 2016 13:49
Show Gist options
  • Save psema4/22a5bd624dea5d353531 to your computer and use it in GitHub Desktop.
Save psema4/22a5bd624dea5d353531 to your computer and use it in GitHub Desktop.
Determine "parent" of the active git branch (Ubuntu)
#!/usr/bin/env bash
# via http://stackoverflow.com/a/17843908/773209
#
# Ubuntu variant
#
# if ack-grep isn't found:
# $ sudo apt-get install ack-grep
branch=`git rev-parse --abbrev-ref HEAD`
git show-branch 2>&1 | ack-grep '\*' | ack-grep -v "$branch" | head -n1 | sed 's/.*\[\(.*\)\].*/\1/' | sed 's/[\^~].*//' | grep -v 'more than 25'
# How it works:
# 1| Get a textual history of all commits.
# 1A| Redirect git's STDERR to STDOUT.
# 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.
# 7| Strip excessive refs warnings (originally sent to
# STDERR) before rendering the output.
@psema4
Copy link
Author

psema4 commented Apr 23, 2015

Note: If you need this script, it's worth reading the StackOverflow thread ;)

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