0

I have a the following piece of code in my git repo (using git version 2.33.0.windows.2):

int foo = 0;
// foo is set in some manner
if (foo == 1) {
    DoA();
    DoB();
    DoC();
    DoD();
}
if (foo == 2) {
    DoA();
    DoC();
    DoE();
}
// etc

Now if I change this code by switching up the order of the if statements to become like this:

int foo = 0;
// foo is set in some manner
if (foo == 2) {
    DoA();
    DoC();
    DoE();
}
if (foo == 1) {
    DoA();
    DoB();
    DoC();
    DoD();
}
// etc
    

If I now do a git diff the output is this:

@@ -1,14 +1,14 @@
 int foo = 0;
 // foo is set in some manner
-if (foo == 1) {
+if (foo == 2) {
     DoA();
-    DoB();
     DoC();
-    DoD();
+    DoE();
 }
-if (foo == 2) {
+if (foo == 1) {
     DoA();
+    DoB();
     DoC();

Is there a way to make git 'recognize' that the if statements where swapped instead of some lines that were edited? Of course it is correct, but it would make it easier to review that nothing changed, except the order of the if statements.

5
  • What is your git configuration? Commented Aug 6 at 7:33
  • 2
    What software are you using to review changes? Does it have a choice of diffing algorithms? Commented Aug 6 at 7:34
  • Please check the related question for the default diff in git. Check the gitattributes manpage how you can hint the language/type of the file (programing language), some of the differs then give better overview.. e.g. in which function a change is in. Commented Aug 6 at 7:39
  • git's diff uses a default context of 3 lines. If you play with the context window, you can get a larger delete and create statements but at the cost of being verbose. Still won't recognize any large reasons of copy/paste lines (swapping) Commented Aug 6 at 7:41
  • 1
    stackoverflow.com/questions/21771313/git-diff-ignore-reorder This question may be relevant too Commented Aug 6 at 7:44

1 Answer 1

5

Try git -c diff.algorithm=histogram diff and if you like what you see you can configure that as the default, git config diff.algorithm histogram with or without --global. I suspect the cost of the extra analysis isn't even measurable these days.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, that seems to work. If I change the algorithm to either patience or histogram it does what I hope it would.
@Frank: This may shed some more insights about patience and historgram: stackoverflow.com/q/32365271/367456

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.