What can I do, when the compare tool compares the wrong lines?

78 Views Asked by At

I had the following code. (I added line numbers for clarification)

(1) if (foo1)
(2) {
(3)     some code 1
(4) }

I changed the if condition to if (foo1 || bar1). I also added similar code above, what results in

(1) if (foo0 || bar0)
(2) {
(3)     some code 0
(4) }
(5) 
(6) if (foo1 || bar1)
(7) {
(8)     some code 1
(9) }

I use git. The Visual Studio compare tool now thinks that I replaced if (foo1) with if (foo0 || bar0) and that I added the rest, including if (foo1 || bar1), as new code.

(1) if (foo1)           (1) if (foo0 || bar0)
(-)                     (2) {
(-)                     (3)     some code 0
(-)                     (4) }
(-)                     (5)
(-)                     (6) if (foo1 || bar1)

This is a problem, because I want to separate the changes into 2 commits. But I cannot stage only the change of the if condition, because the compare tool does not see, that it is a change. What could I do? Is it e. g. possible to manually link line 1 with line 6?

2

There are 2 best solutions below

0
yoav lax On

It's not possible to link line 1 to 6.

I would suggest you separate them into two commits like the following:

  1. Perform the change of: if (foo1 || bar1)
  2. Git commit
  3. Perform the change of: if (foo0 || bar0)
  4. Git commit

Alternatively, instead of the first commit you can perform git add to stage it and this will give you the ability to compare too.

0
Adrijan Bjedov On

You can use patch commits:

  1. Run git commit --patch, this will run an interactive commit
  2. Tell git to split into smaller hunks (s)
  3. Stage the first hunk (y) and don't stage the second (n)
  4. Write a message for the first commit
  5. Run git add . and git commit for the second commit

If you run ? during the patch you can see what all the options do, like this picture