Skip to content

Instantly share code, notes, and snippets.

@smola
Created January 18, 2017 09:23
Show Gist options
  • Save smola/d53d4ad23c3363576fba9492b60bce15 to your computer and use it in GitHub Desktop.
Save smola/d53d4ad23c3363576fba9492b60bce15 to your computer and use it in GitHub Desktop.
Find all initial commits and the commits where they were merged
#!/bin/bash
#
# git-find-roots finds every commit with no parent (initial commits)
# and the commit where they were merged.
#
# Usage:
# git-find-roots [commit-ish]
#
# Example:
# git-find-roots master
#
BRANCH=${1:-master}
find_roots() {
branch=$1
git rev-list --max-parents=0 $branch
}
find_merge() {
commit=$1
branch=$2
perl -ne 'print if ($seen{$_} .= @ARGV) =~ /10$/' <(git rev-list --ancestry-path $commit..$branch) <(git rev-list --first-parent $commit..$branch) | tail -n 1
}
for root in $(find_roots $BRANCH) ; do
merge=$(find_merge $root $BRANCH)
echo "root $root was merged in $merge"
done
@oskarb
Copy link

oskarb commented Feb 18, 2018

To clarify, it finds all root commits of the specified branch, which is not necessarily ALL root commits in the repository.

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