I am looking at a git diff, most of it is the same code moved from one file to another plus whitespace. How could I see the real differences? I tried git diff -b -C -C
to no avail.
Asked
Active
Viewed 998 times
3

chx
- 11,270
- 7
- 55
- 129
-
2IMO this is a workflow issue: you should keep renames and content changes in separate commits. Your best bet might be renaming them back to their old names, examining the changes, and committing them if suitable. Then make another commit that just does the renames. Otherwise you pass the problem you are currently encountering on to anyone who views the logs later. – kampu May 23 '13 at 01:07
-
You can try comparing the two files using an external diff viewing program, like DiffMerge, TortoiseDiff, BeyondCompare, etc. – May 23 '13 at 05:10
-
I find your description hard to understand. I hardly read one block/function moved from `file-a` to `file-b` (both were existing and are existing). Could you please provide some short example diff output? Please elaborate a bit more in your sentence too. – try-catch-finally Jan 13 '17 at 06:40
-
Does this answer your question? [Using Git diff to detect code movement + How to use diff options](https://stackoverflow.com/questions/12590947/using-git-diff-to-detect-code-movement-how-to-use-diff-options) – Glenn 'devalias' Grant Nov 11 '19 at 06:08
3 Answers
5
I came across this via a comment in another SO post, but it seems that since git v2.15 there is the ability to use git diff --color-moved
to better differentiate between moved and changed code.
There is a nice example + screenshot showing it's usage on the GitHub git 2.17 release blog post too.
You can find the help/usage for it at https://git-scm.com/docs/git-diff#Documentation/git-diff.txt---color-movedltmodegt
Related options include:
--color-moved[=<mode>]
(modes:no
,default
,plain
,blocks
,zebra
,dimmed-zebra
)--no-color-moved
--color-moved-ws[=<modes>]
(modes:no
,ignore-space-at-eol
,ignore-space-change
,ignore-all-space
,allow-indentation-change
)--no-color-moved-ws

Glenn 'devalias' Grant
- 1,928
- 1
- 21
- 33
-
The `modes` argument to the option `--color-moved-ws` seems to be mandatory. – ob-ivan Aug 31 '23 at 17:06
1
Use the -M
option of git diff:
-M[<n>], --find-renames[=<n>]
Detect renames. If n is specified, it is a threshold on the
similarity index (i.e. amount of addition/deletions compared
to the file’s size). For example, -M90% means git should
consider a delete/add pair to be a rename if more than 90%
of the file hasn’t changed.
and the -w
:
-w, --ignore-all-space
Ignore whitespace when comparing lines. This ignores
differences even if one line has whitespace where the
other line has none.

rcomblen
- 4,579
- 1
- 27
- 32
-
1This doesn’t seem to help where a chunk of code was moved from one file to another, the case to which I believe the OP was referring. – Aryeh Leib Taurog Nov 22 '17 at 13:51
1
Based on kampu's useful comment the only working solution so far is to move the code back to the original place and diff there.

chx
- 11,270
- 7
- 55
- 129