Diff between 2 git commits except contained in 3rd commit

129 views Asked by At

Is there a way to get meld + git to display changes that happened between 2 git commits, but exclude changes introduced in a 3rd commit? The use case is to see how much current commit differs from some commit done before a 3-way merge.

1

There are 1 answers

0
Dmitriusan On BEST ANSWER

I've got a hacky way to do that.

Warning: Before using single liner below, better commit/backup all your uncommitted changes. This single liner tries to stash and restore your changes. But it still may introduce some clutter (new files) at repo or may contain some bug

The code below shows a diff between FIRST_COMMIT and SECOND_COMMIT except changes to other files introduced by another branch in MERGE_COMMIT. You should set env variables FIRST_COMMIT, SECOND_COMMIT and MERGE_COMMIT to appropriate values.

FIRST_COMMIT=866dfa2a7
MERGE_COMMIT=94b195989
SECOND_COMMIT=0fc856fd9

git stash ; \
git checkout $FIRST_COMMIT ; \
git ls  $MERGE_COMMIT..$SECOND_COMMIT | sort | \
uniq | \
xargs -L 1 git checkout $SECOND_COMMIT -- ; \
git difftool -d $FIRST_COMMIT ; \
git stash && git stash drop ; \
git stash apply

What this single-liner does:

  • puts local uncommitted changes to git stash
  • checks out code state at FIRST_COMMIT
  • takes all files changed at MERGE_COMMIT and SECOND_COMMIT and all revisions between them
  • every file from the previous step is reset to its state at a time of the SECOND_COMMIT
  • shows diff using diff tool you configured for git. The diff would be between FIRST_COMMIT and SECOND_COMMIT except changes to other files introduced by another branch in MERGE_COMMIT. The previous sentence means that if some file was modified both in SECOND_COMMIT and in MERGE_COMMIT, all changes will be present at diff. It's hard to avoid that.
  • After you exited your diff tool, git will remove temporary changes, and check out the branch you have been working on.
  • After that, changes from git stash will be applied.

Depending on your prefered git workflow, you may decide to remove git stash calls from single liner above