git merge: apply changes to code that moved to a different file

Viewed 60640

I am attempting a pretty beefy git merge maneuver right now. One problem that I am coming across is that I made some changes to some code in my branch, but my colleague moved that code to a new file in his branch. So when I did git merge my_branch his_branch, git did not notice that the code in the new file was the same as the old, and so none of my changes are there.

What's the easiest way to go about applying my changes again to the code in the new files. I won't have too many problems finding out which commits need to be reapplied (I can just use git log --stat). But as far as I can tell, there is no way to get git to reapply the changes into the new files. The easiest thing I am seeing right now is to manually reapply the changes, which is not looking like a good idea.

I know that git recognizes blobs, not files, so surely there must be a way to tell it, "apply this exact code change from this commit, except not where it was but where it now is in this new file".

4 Answers

My quick solution to this problem (in my case it was not a single file, but a whole directory structure) was:

  • move the file(s) in "my_branch" to the location where they are in "his_branch" (git rm / git add)
  • git commit -m "moved to original location for merging"
  • git merge his_branch (no conflicts this time!)
  • move the file(s) to my desired location (git rm / git add)
  • git commit -m "moved back to final location after merge"

You will have two additional commits in the history.

But since git tracks the moving of the files, git blame, git log etc will still work on these files, since the commits that moved the files did not change them. So I don't see any drawback to this method and it's very straightforward to understand.

Related