Is there a way to check if two different git commits are equal in content?

Viewed 4176

I know that git tracks content and generates a sha based partially on the content. However, the sha is also based upon the parent commit. When I rebase a branch, because my commits now have a different ancestor, all of my commits have different shas.

But what I'm wondering, is there a way to compare two commits (or commit ranges) to see if content-wise, they are the same? This should also be able to tell if a binary change is the same as well.

I'm thinking if there was some way to get the sha for the content without the ancestor information incorporated, that might do it.

Thanks for any and all help,

4 Answers

@solstice333's answer addresses the problem behind the question but the output still requires a lot of parsing. If you want a quick way to confirm similarities after a rebase, an extension of that solution should help reduce a majority of the clutter.

Solution

hash1=123456 \
hash2=098765 \
sed_command='/^\(diff\|index\|@@\).*$/ d' \
diff <(git show --oneline -U0 $hash1 | sed "$sed_command") <(git show --oneline -U0 $hash2 | sed "$sed_command")

(If you aren't using bash, you can just use the bottom line and replace the variables)

Breaking it down

This command finds the changes from each commit, cleans up the output, and finds the differences in the outputs. The outputs are trimmed down via:

  • --one-line - simplifies commit message output
  • -U0 - shows only the changed lines/removes all surrounding lines; shorthand for --unified=0;
  • sed - Using this command, we reduce clutter in the output by removing any line that starts with certain characters based on the regex (/^\(diff\|index\|@@\).*$/). Some conditions here can be removed for better specificity but generally do not matter due to the nature of the differences in a rebase.

I suppose you could also extend this to use git diff in place of the inner git show commands, to encapsulate all changes in a commit range.

Related