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 log --stat
I had a similar issue, and I resolved it by rebasing my work to match the target file organization.
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
This copy has been done in a commit that we name commit
You want to apply all your local changes, commits
B below, that were made on
original.txt, to the new file
---- X -----CP------ (master) \ \--A---B--- (local)
Create a throwaway branch
move at the starting point of your changes.
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).
---- 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 (move) / ---- X -----CP----- (master) \ \--A---B--- (local)
You can now rebase your work on top of
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
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
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
Hope this is clear. This answer comes late, but this may be useful for someone else.