Skip to content

Instantly share code, notes, and snippets.

@simonwhitaker
Last active April 5, 2022 08:01
Show Gist options
  • Save simonwhitaker/6354592 to your computer and use it in GitHub Desktop.
Save simonwhitaker/6354592 to your computer and use it in GitHub Desktop.
A script to determine whether one git commit is the ancestor of another
#!/bin/bash
#
# git-is-ancestor, by Simon Whitaker
#
# Suggested usage
#
# Store this file somewhere, make it executable, then alias
# it to git is-ancestor by putting this in your $HOME/.gitconfig:
#
# [alias]
# is-ancestor = !/path/to/git-is-ancestor
#
# Then use thus:
#
# $ git is-ancestor rev1 rev2
script_name=$(basename $0)
usage()
{
cat << EOF >&2
usage: ${script_name} <REV1> <REV2>
EOF
}
if [ $# -ne 2 ]; then
usage
exit 2
fi
if $( git merge-base --is-ancestor $1 $2 ); then
echo "$1 is an ancestor of $2"
exit 0
elif $( git merge-base --is-ancestor $2 $1 ); then
echo "$2 is an ancestor of $1"
exit 0
else
echo "$1 and $2 are not related"
exit 1
fi
@Artoria2e5
Copy link

if $( git merge-base --is-ancestor $1 $2 ); then
if git merge-base --is-ancestor "$1" "$2"; then

You definitely don't want run the stdout of the command (if any, fortunately none for now.)

#!/bin/bash

Looks completely POSIX to me, so why not /bin/sh?

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