Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get git to correctly merge moved content (not only files)

I am currently looking at a lot of the content tracking features of git. It is very nice to know that git allows me to figure out code which has been moved from one file to another, but I am wondering how this feature is usable when doing conflict resolution in merges.

Here is the scenario:

I have two files hello.cc and bye.cc created. I start a branch topic and move some code from hello.cc to bye.cc. If I now do a git blame -C bye.cc I can see that this code originally came from hello.cc which is nice to know. However now I switch to the original branch without the moved content and change some code within the section in hello.cc that has been moved in the other commit. If I now do a git merge topic I get a conflict for hello.cc. However unless I use a diff3 style (which I usally do though), I can only see that this method has been removed from hello.cc in the other branch, but not that it has been changed afterwards. What would be nice would be to also get a conflict on bye.cc because it would be necessary to check if those changes from the other branch will have to be reapplied to the code. Is this somehow possible?

I know I can manually figure out, that the code has been moved by doing a git blame --reverse -C topic.... However for one it took me quite a while to figure out this possibility and most other will probably not know about it. For second I am lazy and will probably just forget that the code might have been moved. Also I am not sure that this works when the code has been moved to more than one file.

What would be your way to keep this situation as safe as possible?

Edit:

I just found out that git blame --reverse -C hello.cc $(git merge-base HEAD topic)..topic also works to find out where the content moved. And if I do understand git correctly, this is probably faster, because it will not do a full search of the content in the full repository.

Edit:

I uploaded the repository I am using for playing around to github so you can try out the merge for yourself. The commit where I moved the function is in the topic branch. The commit where the same function get's changed in master at HEAD of branch merge_here. There is one additional commit in master in which I was playing around with some other merging techniques, which you should ignore for this question.

like image 996
LiKao Avatar asked Jan 12 '12 23:01

LiKao


1 Answers

I am afraid it is not possible for git to automatically recognize moved code in merge and produce conflicts etc, unless those are whole files renamed.

There were already some discussions on this topic here, like how does git handle merging code that was moved to a different file? and git merge: apply changes to code that moved to a different file.

like image 110
Krizz Avatar answered Nov 05 '22 07:11

Krizz