How to "git show" a merge commit with combined diff output even when every changed file agrees with one of the parents?

GitMerge

Git Problem Overview


After doing a "simple" merge (one without conflicts), git show usually only shows something like

commit 0e1329e551a5700614a2a34d8101e92fd9f2cad6 (HEAD, master)
Merge: fc17405 ee2de56
Author: Tilman Vogel <email@email>
Date:   Tue Feb 22 00:27:17 2011 +0100

Merge branch 'testing' into master

This is because, for merges, git show uses the combined diff format which omits files that agree with either of the parent versions.

Is there a way to force git to still show all differences in combined diff mode?

Doing git show -m will show the differences (using pairwise diffs between the new and all parent versions respectively) but I would prefer to have that with the differnces marked by +/- in the respective columns like in combined mode.

Git Solutions


Solution 1 - Git

Look at the commit message:

commit 0e1329e551a5700614a2a34d8101e92fd9f2cad6 (HEAD, master)
Merge: fc17405 ee2de56
Author: Tilman Vogel <email@email>
Date:   Tue Feb 22 00:27:17 2011 +0100

Merge branch 'testing' into master

notice the line:

Merge: fc17405 ee2de56

take those two commit ids and reverse them. so in order get the diff that you want, you would do:

git diff ee2de56...fc17405

to show just the names of the changed files:

git diff --name-only ee2de56..fc17405

and to extract them, you can add this to your gitconfig:

exportfiles = !sh -c 'git diff $0 --name-only | "while read files; do mkdir -p \"$1/$(dirname $files)\"; cp -vf $files $1/$(dirname $files); done"'

then use it by doing:

git exportfiles ee2de56..fc17405 /c/temp/myproject

Solution 2 - Git

A better solution (mentioned by @KrisNuttycombe):

git diff fc17405...ee2de56

for the merge commit:

commit 0e1329e551a5700614a2a34d8101e92fd9f2cad6 (HEAD, master)
Merge: fc17405 ee2de56
Author: Tilman Vogel <email@email>
Date:   Tue Feb 22 00:27:17 2011 +0100

to show all of the changes on ee2de56 that are reachable from commits on fc17405. Note the order of the commit hashes - it's the same as shown in the merge info: Merge: fc17405 ee2de56

Also note the 3 dots ... instead of two!

For a list of changed files, you can use:

git diff fc17405...ee2de56 --name-only

Solution 3 - Git

You can create branch with HEAD set to one commit before merge. Then, you can do:

git merge --squash testing

This will merge, but not commit. Then:

git diff

Solution 4 - Git

Seems like answered here: https://public-inbox.org/git/[email protected]/

> So in a similar way, running > > $ git diff --cc $M $M^1 $M^2 $(git merge-base $M^1 $M^2) > > should show a combined patch that explains the state at $M relative to the > states recorded in its parents and the merge base.

Solution 5 - Git

If you are sitting at the merge commit then this shows the diffs:

git diff HEAD~1..HEAD

If you're not at the merge commit then just replace HEAD with the merge commit. This method seems like the simplest and most intuitive.

Solution 6 - Git

I think you just need 'git show -c $ref'. Trying this on the git repository on a8e4a59 shows a combined diff (plus/minus chars in one of 2 columns). As the git-show manual mentions, it pretty much delegates to 'git diff-tree' so those options look useful.

Solution 7 - Git

If your merge commit is commit 0e1329e5, as above, you can get the diff that was contained in this merge by:

git diff 0e1329e5^..0e1329e5

I hope this helps!

Solution 8 - Git

in your case you just need to

git diff HEAD^ HEAD^2

or just hash for you commit:

git diff 0e1329e55^ 0e1329e55^2

Solution 9 - Git

You can use the diff-tree command with the -c flag. This command shows you what files have changed in the merge commit.

git diff-tree -c {merged_commit_sha}

I got the -c flag's description from Git-Scm:

> This flag changes the way a merge commit is displayed (which means it > is useful only when the command is given one , or --stdin). > It shows the differences from each of the parents to the merge result > simultaneously instead of showing pairwise diff between a parent and > the result one at a time (which is what the -m option does). > Furthermore, it lists only files which were modified from all parents.

Solution 10 - Git

I built a general-purpose approach to doing various operations on a merge's commits.

Step One: Add an alias to git by editing ~/.gitconfig:

[alias]
  range = "!. ~/.githelpers && run_on_merge_range"

Step Two: In ~/.githelpers, define a bash function:

run_on_merge_range() {
  cmd=$1; shift
  commit=$1; shift
  range=$(git show $commit | grep Merge: | awk '{print $2 "..." $3}')
  echo "git $cmd $range $@"
  if [ -z $range ]; then
    echo "No merge detected"
    exit 1
  fi
  git $cmd $range $@
}

Step Three: Profit!

git range log <merge SHA> --oneline
git range diff <merge SHA> --reverse -p
git range diff <merge SHA> --name-only

There is probably a LOT of room for improvement here, I just whipped this together to get past an annoying situation. Feel free to mock my bash syntax and/or logic.

Solution 11 - Git

No, there is no way to do this with git show. But it would certainly be nice sometimes, and it would probably be relatively easy to implement in the git source code (after all, you just have to tell it to not trim out what it thinks is extraneous output), so the patch to do so would probably be accepted by the git maintainers.

Be careful what you wish for, though; merging a branch with a one-line change that was forked three months ago will still have a huge diff versus the mainline, and so such a full diff would be almost completely unhelpful. That's why git doesn't show it.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTilman VogelView Question on Stackoverflow
Solution 1 - Gitrip747View Answer on Stackoverflow
Solution 2 - GitCodeManXView Answer on Stackoverflow
Solution 3 - Gitside2kView Answer on Stackoverflow
Solution 4 - Gitmax630View Answer on Stackoverflow
Solution 5 - GitBruce DawsonView Answer on Stackoverflow
Solution 6 - GitpatthoytsView Answer on Stackoverflow
Solution 7 - Githesham_EEView Answer on Stackoverflow
Solution 8 - GitgurugrayView Answer on Stackoverflow
Solution 9 - GitEhsan MirsaeediView Answer on Stackoverflow
Solution 10 - GitNerdmasterView Answer on Stackoverflow
Solution 11 - GitapenwarrView Answer on Stackoverflow