When a file is renamed with git mv
, the commit will show rename from and rename to, and in pull request shows the same, which is good. But when a file is git mv
ed then changed, it looks there is a certain threshold that when number of lines changed exceeded it, it will no longer show as renaming, instead it is show as old file deleted and new file added. So my question is is this threshold a well defined number? and is there any other way to make it better, mainly because in pull request diff, when the two files are no considered renamed, the diff won't show side by side, which makes the review difficult.

- 13,238
- 8
- 62
- 100
1 Answers
It is based on the diff similarity index
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.Without a
%
sign, the number is to be read as a fraction, with a decimal point before it. I.e.,-M5
becomes0.5
, and is thus the same as-M50%
.
Similarly,-M05
is the same as-M5%
.To limit detection to exact renames, use
-M100%
.The default similarity index is 50%.
More generally, it is better to mv/rename first a file, commit, then do some modifications.
You can do both is said modifications are minor compared to the rest of the file (typical case: refactoring where just the name of the package changes)

- 1,262,500
- 529
- 4,410
- 5,250
-
Yeah, mv in a commit, change in another seems a good choice. Thanks. – fluter Jan 11 '19 at 09:48
-
Is it possible during the pull request workflow to break the move detection even with an initial git mv commit followed by a modify commit(s)? Basically if one always does git mv followed by a modification commit the file rename will always be picked up correctly regardless of how many modifications were made to that file. Is that accurate? – jxramos Jun 04 '22 at 01:19
-
1@jxramos I believe it is. Separating `mv` from modification is safer (but one rarely thinks to split their changes that way) – VonC Jun 04 '22 at 08:15