Skip to content

Instantly share code, notes, and snippets.

Created Jan 31, 2016
What would you like to do?
Find the "branching off" point between two commits, irrespective of how many times they've been merged into each other.
#!/bin/bash -e
# Filter out early commits that are not common ancestors
# Also helpfully throws an error if there are *no* common ancestors.
FRONTIER=$(git rev-list --boundary "$BRANCH_A"..."$BRANCH_B" | grep ^- | cut -d- -f2)
# Only happens if branch A == branch B exactly
if [ -z "$FRONTIER" ]; then
echo "$(git rev-parse "$BRANCH_A")"
exit 0
# Find topologically-first cut
while read rev; do
# Check if rev is a cut for both branches
[ "$(git rev-list --boundary "$BRANCH_A" "$BRANCH_B" "^$rev" | grep ^-)" == "-$rev" ] || continue
# Found it!
echo "$rev"
exit 0
done < <(git rev-list --topo-order $FRONTIER)
# Unreachable
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment