Skip to content

Instantly share code, notes, and snippets.

@mikerpost
Forked from eddiemoya/git-find-merge-base.sh
Created June 20, 2014 22:17
Show Gist options
  • Save mikerpost/0776af11e5cc8285f3ef to your computer and use it in GitHub Desktop.
Save mikerpost/0776af11e5cc8285f3ef to your computer and use it in GitHub Desktop.
##
# 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