Skip to content

Instantly share code, notes, and snippets.

@dualcyclone
Created April 4, 2017 10:57
Show Gist options
  • Save dualcyclone/db66a335ebdf40dd07de9b9de5d9c9a2 to your computer and use it in GitHub Desktop.
Save dualcyclone/db66a335ebdf40dd07de9b9de5d9c9a2 to your computer and use it in GitHub Desktop.
A script to find the source branch for a branch in Git.
#!/bin/sh
# Script to find the parent branch for a given branch name
# If no branch name is specified, then it'll use the existing branch
# If you specify a branch to find the source of that doesn't exist, the script will exit with an error exit code
currentbranch=`git branch | grep '*' | sed 's/\* //'`
exists=`git show-ref refs/heads/$1`
if [ "$1" != "" ] && [ -z "$exists" ]; then
echo "You've specified a branch that doesn't exist"
exit 1
fi
if [ "$1" != "$currentbranch" ]; then
git checkout $1 &> /dev/null
fi
git show-branch | grep '\*' | grep -v "`git rev-parse --abbrev-ref HEAD`" | head -n1 | sed 's/.*\[\(.*\)\].*/\1/' | sed 's/[\^~].*//'
if [ "$1" != "$currentbranch" ]; then
git checkout $currentbranch &> /dev/null
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment