184

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

5
  • 2
    Not exactly the same, but here is a similar question with good anwsers that might apply: stackoverflow.com/questions/2701790/… Commented Jan 23, 2015 at 16:35
  • 6
    None of the answers describe why git isn't able to perform merges like this automatically. I thought it was supposed to be smart enough to detect renames and perform the appropriate merge automatically?
    – John
    Commented Mar 30, 2016 at 21:29
  • Perhaps this wasn't true when the question was asked, but modern versions of git (I'm using 1.9.5) can merge changes across renamed and moved files. There is even a --rename-threshold option to tweak the amount of similarity that is required.
    – Todd Owen
    Commented Feb 27, 2017 at 6:26
  • 1
    @ToddOwen that will work if you are doing a vanilla rebase off of an upstream branch, but you'll still run into trouble if you're cherry picking or back-porting a range of changes that may not include the commit that renames the files.
    – GuyPaddock
    Commented Apr 19, 2017 at 14:33
  • Is/was this a problem at all if you do a rebase first?
    – hbogert
    Commented Jul 27, 2019 at 13:09

4 Answers 4

177

I had a similar issue, and I resolved it by rebasing my work to match the target file organization. This works because git keep tracks of files content, so by rebasing on top of a rename, the changes are applied as necessary.

More precisely, let's say that you modified original.txt on your branch (the local branch), but on the master branch, original.txt has been copied to another one, say copy.txt. This copy has been done in a commit that we name commit CP.

You want to apply all your local changes, commits A and B below, that were made on original.txt, to the new file copy.txt.

 ---- X -----CP------ (master)
       \ 
        `--A---B--- (local)
 

Create a throwaway branch move at the starting point of your changes with git branch move X. That is to say, put the move branch at commit X, the one before the commits you want to merge; most likely, this is the commit from which you branched out to implement your changes. As user @digory doo wrote below, you can do git merge-base master local to find X.

 ---- X (move)-----CP----- (master)
       \ 
        `--A---B--- (local)
 

On this branch, issue the following renaming command:

git mv original.txt copy.txt

This renames the file. Note that copy.txt did not yet exist in your tree at this point.
Commit your change (we name this commit MV).

        ,--MV (move)
       /
 ---- X -----CP----- (master)
       \ 
        `--A---B--- (local)
 

You can now rebase your work on top of move:

git rebase move local

This should work without problem, and your changes are applied to copy.txt in your local branch.

        ,--MV (move)---A'---B'--- (local)
       /
 ---- X -----CP----- (master)
 

Now, you do not necessarily want or need to have commit MV in your main branch's history, because the move operation may lead to a conflict with the copy operation at commit CP in the main branch.

You only have to rebase your work again, discarding the move operation, as follows:

git rebase move local --onto CP

... where CP is the commit where copy.txt was introduced in the other branch. This rebases all the changes on copy.txt on top of the CP commit. Now, your local branch is exactly as if you always modified copy.txt and not original.txt, and you can continue merging with others.

                ,--A''---B''-- (local)
               /
 -----X-------CP----- (master)
 

It is important that the changes are applied on CP or otherwise copy.txt would not exist and the changes would be applied back on original.txt.

Hope this is clear. This answer comes late, but this may be useful to someone else.

14
  • 3
    That's a lot of work, but I think it should work in principle. I think you'll have better luck with merge than rebase, though.
    – asmeurer
    Commented Feb 28, 2013 at 19:34
  • 11
    This solution only involves basic git commands, unlike editing patches or using patch (which involves also potentially a lot of work), and that's why I thought it could be interesting to show it. Also, note that I took more time to write the answer than to actually apply the changes, in my case. At which step would you recommand using a merge? and why? The only difference I see is that by rebasing, I make a temporary commit that is later discarded (commit MV), which is not possible with merges only.
    – coredump
    Commented Mar 1, 2013 at 8:46
  • 3
    With rebase, you have a higher chance of dealing with merge conflicts, because you deal with each commit, whereas with a merge you deal with everything at once, meaning some changes that might have happened with a rebase would be nonexistent with a merge. What I would do is merge, then manually move the merged file. Maybe I misunderstood the idea of your answer, though.
    – asmeurer
    Commented Mar 1, 2013 at 17:49
  • 6
    I added some ASCII trees to clarify the approach. Regaring merge vs. rebase in that case: what I want to do is take all the changes on 'original.txt' in my branch and apply them to 'copy.txt' in the master branch, because for some reason, 'original.txt' was copied (and not moved) into 'copy.txt' at some point. After that copy, 'original.txt' may also have evolved on the master branch. If I merged directly, my local changes on original.txt would be applied to the modified original.txt in master branch, which would be difficult to merge. Regards.
    – coredump
    Commented Mar 4, 2013 at 9:32
  • 2
    I see. I would recommend just doing git merge local from move, then manually copying over the file to your original local branch. This has git doing less of the work for you (you are manually copying the file), but it will end up being easier because merge conflicts are simpler when merging than when rebasing.
    – asmeurer
    Commented Mar 8, 2013 at 22:54
38

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.

3
  • 2
    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
    Commented Aug 16, 2010 at 17:29
  • 1
    @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.
    – Cascabel
    Commented Aug 16, 2010 at 17:31
  • 1
    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
    Commented Aug 16, 2010 at 17:36
33

Here is a merge solution of encountering a merge conflict with rename and edit and resolving it with mergetool recognizing the correct 3 merge source files.

  • After a merge fails because of 'deleted file' that you realize was renamed and edited:
  1. You abort the merge.
  2. Commit renamed files on your branch.
  3. And merge again.

Walk-through:

Create a file.txt:

$ git init
Initialized empty Git repository in /tmp/git-rename-and-modify-test/.git/

$ echo "A file." > file.txt
$ git add file.txt
$ git commit -am "file.txt added."
 [master (root-commit) 401b10d] file.txt added.
 1 file changed, 1 insertion(+)
 create mode 100644 file.txt

Create a branch where you will edit later:

$ git branch branch-with-edits
 Branch branch-with-edits set up to track local branch master. 

Create the rename and edit on master:

$ git mv file.txt renamed-and-edited.txt
$ echo "edits on master" >> renamed-and-edited.txt 
$ git commit -am "file.txt + edits -> renamed-and-edited.txt."
 [master def790f] file.txt + edits -> renamed-and-edited.txt.
 2 files changed, 2 insertions(+), 1 deletion(-)
 delete mode 100644 file.txt
 create mode 100644 renamed-and-edited.txt

Swap to branch, and edit there too:

$ git checkout branch-with-edits 
 Switched to branch 'branch-with-edits'
 Your branch is behind 'master' by 1 commit, and can be fast-forwarded.
 (use "git pull" to update your local branch)
$ 
$ echo "edits on branch" >> file.txt 
$ git commit -am "file.txt edited on branch."
 [branch-with-edits 2c4760e] file.txt edited on branch.
 1 file changed, 1 insertion(+) 

Attempt to merge master:

$ git merge master
 CONFLICT (modify/delete): file.txt deleted in master and modified in HEAD. Version HEAD of file.txt left in tree.
 Automatic merge failed; fix conflicts and then commit the result. 

Notice the conflict is hard to resolve - and that files were renamed. Abort, mimic the rename:

$ git merge --abort
$ git mv file.txt renamed-and-edited.txt
$ git commit -am "Preparing for merge; Human noticed renames files were edited."
 [branch-with-edits ca506da] Preparing for merge; Human noticed renames files were edited.
 1 file changed, 0 insertions(+), 0 deletions(-)
rename file.txt => renamed-and-edited.txt (100%) 

Try merge again:

$ git merge master
 Auto-merging renamed-and-edited.txt
 CONFLICT (add/add): Merge conflict in renamed-and-edited.txt
 Recorded preimage for 'renamed-and-edited.txt'
 Automatic merge failed; fix conflicts and then commit the result. 

Great! Merge results in a 'normal' conflict that can be resolved with mergetool:

$ git mergetool
 Merging:
 renamed-and-edited.txt

 Normal merge conflict for 'renamed-and-edited.txt':
 {local}: created file
 {remote}: created file
$ git commit 
 Recorded resolution for 'renamed-and-edited.txt'.
 [branch-with-edits 2264483] Merge branch 'master' into branch-with-edits 
3
  • 2
    In that solution, it seems to me that the first step, the merge that is aborted, is only useful to find out which files were renamed and edited remotely. If you know them in advance. You could skip that step and, basically, the solution is simply to rename the files manually locally and then merge and resolve the conflicts as usual.
    – user2066805
    Commented Mar 31, 2016 at 3:07
  • 1
    Thank you. My situation was that I moved B.txt -> C.txt and A.txt -> B.txt with git mv, and git couldn't automatically match the merge conflicts correctly (was getting merge conflicts between old B.txt and new B.txt). By using this method, the merge conflicts are now between the correct files.
    – cib
    Commented Jan 10, 2018 at 13:17
  • This only works when the whole file is moved, but git should generally detect that situation automatically. The tricky situation is when only part of a file is moved. Commented Sep 5, 2019 at 10:37
3

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.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.