tags:

views:

36

answers:

1

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".

+1  A: 

You can always use git diff (or git format-patch) to generate the patch, then go manually edit the filenames in the patch, and apply it with git apply (or git am).

Short of this, the only way it's going to work automatically is if git's rename detection can figure out that the old and new files are the same thing - which it sounds like they aren't really in your case, just a chunk of them. It's true that git uses blobs, not files, but a blob is just the contents of an entire file, without the filename and metadata attached. So if you have a chunk of code moved between two files, they aren't really the same blob - the rest of the blob's content is different, just the chunk in common.

Jefromi
Well, it's the best answer so far. I couldn't figure out how to make `git format-patch` work for a commit. If I do `git format-patch SHA1`, it generates a whole bunch of patch files for the whole history. But I guess `git show SHA1 > diff.patch` will work just as well.
asmeurer
@asmeurer: use the `-1` option. The normal mode of operation for format-patch is a revision range, like `origin/master..master`, so you can easily prepare a patch series.
Jefromi
Actually, another note. `git apply` and `git am` are too picky, because they want the same line numbers. But I am currently being successful with the UNIX `patch` command.
asmeurer