-1

Everytime I add a property to a class with XML comments git thinks I added space below and a summary opening tag below instead of the of the one I wrote. For example, I added the property FirstName (line 16 to 20), but the git diff is (18 to 22). At first, I thought it was a normal behaviour. I think I've seen others git diff that does not have this behaviour.

    12 /// <summary>
    13 /// Date.
    14 /// </summary>
    15 public DateTime Date { get; set; }
    16
    17 /// <summary>
    18 /// FirstName.
    19 /// </summary>
    20 public string FirstName { get; set; }
    21
    22 /// <summary>
    23 /// LastName.
    24 /// </summary>
    25 public string LastName { get; set; }

Here is a picture for visual representation

enter image description here

It's not that bad, it just makes merge conflicts on properties more time consuming because I need to rewrite the summary tag.

Honestly, I don't even know what to do. I tried to write my code in different manners but can't get rid of this. I've search on internet but can't find a post about this problem.

  • 1
    You are not showing enough context (line 17 is not even present in your screen shot, whereas what we need to see is everything from, say, line 10 to line 30 in order to make a judgment) — and please show _text_, not a picture of text. Thanks! – matt Aug 09 '23 at 19:55

2 Answers2

1

Git is showing you the difference. You added the new lines 16-20, but the first line that Git finds different is line 18. Lines 16 and 17 are unchanged from the previous version.

If you need lines 16 and 17 to be reported as different, they must differ from the previous version. Or, you need to understand that differences are not the same as edits.

eskwayrd
  • 3,691
  • 18
  • 23
  • It might be it, simple as that. At first, I thought the same, but I think I've seen a colleague's git diff that doesn't have the same behaviour. Were you experiencing the same thing when you tried on your end? If yes, I'll close this question. – Julien Turcotte Aug 10 '23 at 01:15
1

If you just walk down the file one line at a time you can see that Git is right about where the difference starts. This is easiest to see if you put the old file and the new file side by side:

12 /// <summary>                         12 /// <summary>
13 /// Date.                             13 /// Date.
14 /// </summary>                        14 /// </summary>
15 public DateTime Date { get; set; }    15 public DateTime Date { get; set; }
16                                       16
17 /// <summary>                         17 /// <summary>
18 /// LastName.                         18 /// FirstName.
19 /// </summary>                        19 /// </summary>
20 public string LastName { get; set; }  20 public string FirstName { get; set; }
                                         21
                                         22 /// <summary>
                                         23 /// LastName.
                                         24 /// </summary>
                                         25 public string LastName { get; set; }

The first line that differs is 18. Okay, then 19 is the same in both, but then 20 differs, and you can't have a one-line diff, so 18-20 are a new "hunk". Okay, but we already dealt with 16 and 17, so the next match is old 18 and new 23. Thus the diff is 18-22.

(I may be off by one but I think I've capture the general gist.)

I think the underlying issue here is that you are imagining that Git is going to tell you what happened. "Hey, these lines were moved down to make room for these other lines which were inserted." That is not how Git thinks. It is just playing "spot the difference"; it doesn't know or care about the mechanics of what you did from a human point of view.

matt
  • 515,959
  • 87
  • 875
  • 1,141