0

Related to my previous question Git merge conflict upon rebasing adjacent deletion and insertion?

By default, git's merge algorithm can give dangerous/unhelpful suggestions.

For instance, I have sometimes situations, where I want to push some code changes of my local development branch to the master branch, but keep other changes purely local (e.g. debug code that I still need, or unfinished changes that got mangled into the same branch).

Say, there is a commit sequence:

! --- devel~2 = master
program main
end program main

! --- devel~1
program main
    write *, "Some debug output"
end program main

! --- devel = HEAD
program main
    write *, "Some debug output"
    write *, "Some release mode output"
end program main

Now I use git rebase -i master and want to reverse the order of the two changes. Since it affects adjacent hunks, it will create a merge conflict:

program main
<<<<<<< HEAD
=======
program main
    write *, "Some debug output"
    write *, "Some release mode output"
>>>>>>> b6b1ecc... Add debug message
end program main

It does make sense, as the second line could very well be invalid without the first. However, if the two additions are independent, both suggestions made by git are incorrect.

At this point, the best thing I can do is do look at

git log -n 1 -U b6b1ecc

which will show me what part of the change is really wanted. But in real-world scenarios, it typically isn't even obvious, that the suggestion made by git is incorrect and I've ended up pushing an unfinished change to the master branch because of it.

Is there any way to make git give more appropriate merge suggestions? Especially in such a way, that the final state of the file in the devel branch remains unchanged by the rebase.

kdb
  • 4,098
  • 26
  • 49
  • 2
    You're looking for the `diff3` style, say `git checkout -m --conflict=diff3 that.file`, and there's a config item to make that the default, say `git help config`. What reporting style best suits a conflict varies, seems it's as hard to know what needs showing for a particular conflict as it is to know what the result should be. – jthill Mar 30 '22 at 16:31
  • Note that Git's existing (built in) merge drivers work on the basis of lines. For Fortran programs that's probably appropriate. Git has no intelligence at all, it's just following simple line-oriented text combining rules. – torek Mar 30 '22 at 22:31
  • @jthill `diff3` definitely provides some improvement, by providing more context. Still results in a high risk of accidentally dropping changes though. I wonder if there is some way to automate "change order of commits without changing end result" better :/ – kdb Apr 04 '22 at 06:31
  • The hard part isn't automating a method that solves your or any particular case better, the hard part is identifying those cases, distinguishing them from the cases where that method would (now silently) produce worse, much worse results. – jthill Apr 04 '22 at 17:20

0 Answers0