Skip to content

Instantly share code, notes, and snippets.

@eddiemoya
Last active March 30, 2021 09:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save eddiemoya/6541519 to your computer and use it in GitHub Desktop.
Save eddiemoya/6541519 to your computer and use it in GitHub Desktop.
Finds the most recent common ancestor (the "merge-base") of two given revisions using 'git merge-base', and shows it highlighted in a list of every commit since then using 'git log' and 'git rev-list'.
##
# Get the first 7 characters of the SHA1 of the most common ancestor of the given revs
##
BASE_FULL_SHA1=$(git merge-base "$1" "$2");
BASE=$(cut -c 1-7 <<< $BASE_FULL_SHA1);
##
# Get (roughly) the number of commits between the given revs.
# Add one for safety padding so the actual commit in question is included.
##
REVS_SINCE=$(($(git rev-list "$1"..."$2" | wc -l | tr -d " " )+1));
##
# Get a formatted log outout, of everything since BASE by passing REV_SINCE to log.
# Use pass BASE to egrep to highlight the most common ancestor (merge-base)
##
git log --pretty=format:"%C(yellow)[%ad]%C(reset) %C(green)[%h]%C(reset) | %C(red)%s %C(bold red){{%an}}%C(reset) %C(blue)%d%C(reset)" --graph --date=short
$1...$2 | egrep --color=always "$BASE|$";
##
# For usability, echo out the full merge-base SHA1
# as well as the number of commits since the merge-base
##
echo "Merge Base: $BASE_FULL_SHA1";
echo "$REVS_SINCE commits between";
###
## Usage: git find-merge-base <rev> <rev>
## Example: git find-merge-base mybranc master
## git config --global alias.find-merge-base '!BASE_FULL_SHA1=$(git merge-base "$1" "$2");BASE=$(cut -c 1-7 <<< $BASE_FULL_SHA1);REVS_SINCE=$(($(git rev-list "$1"..."$2" | wc -l | tr -d " " ))); git log --pretty=format:"%C(yellow)[%ad]%C(reset) %C(green)[%h]%C(reset) | %C(red)%s %C(bold red){{%an}}%C(reset) %C(blue)%d%C(reset)" --graph --date=short $1...$2^ | egrep --color=always "$BASE|$"; echo "Merge Base: $BASE_FULL_SHA1"; echo "$REVS_SINCE commits between"'
##
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment