Skip to content

Instantly share code, notes, and snippets.

@chrisandreae
Last active June 26, 2024 13:37
Show Gist options
  • Save chrisandreae/4a2d3c2ecc8237c3fa884667e3080ebd to your computer and use it in GitHub Desktop.
Save chrisandreae/4a2d3c2ecc8237c3fa884667e3080ebd to your computer and use it in GitHub Desktop.
Compare Rebase Script Fragment
compareRebase () {
local newbranch oldbranch newcommit oldcommit ancestor
local opts=()
if [ "$1" = "-w" ]
then
opts+=("-w")
shift
fi
newbranch=${1:-$(git rev-parse --abbrev-ref HEAD)}
oldbranch=${2:-"${newbranch}@{u}"}
ancestor=${3:-origin/master}
oldancestor=${4:-"$ancestor"}
oldcommit=$(git commit-tree -p "$(git merge-base "${oldancestor}" "${oldbranch}")" "${oldbranch}^{tree}" -m "before rebase")
newcommit=$(git commit-tree -p "$(git merge-base "${ancestor}" "${newbranch}")" "${newbranch}^{tree}" -m "after rebase")
git range-diff "${opts[@]}" "${oldcommit}^..${oldcommit}" "${newcommit}^..${newcommit}"
}
@thefloweringash
Copy link

For comparison, I use this

#!/usr/bin/env bash

master=$1
: "${master:=master}"

colordiff -u \
          <(git diff "$(git merge-base "origin/$master" "HEAD@{u}")" "HEAD@{u}") \
          <(git diff "$(git merge-base "origin/$master" HEAD)" HEAD)

@chrisandreae
Copy link
Author

chrisandreae commented Feb 8, 2022

Utility for comparing the outcome of a rebase by comparing their diffs from a common ancestor (e.g. the master branch). Assumes that the branch's upstream is the "before" state to compare with.

The advantage of using git range-diff is that it gives you a proper colorized "diff-of-diffs", which can be very hard to read otherwise.

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