2

I have 2 branches A and B

In branch A I renamed Thing.java to ThingImpl.java, and rewrote Thing.java so that it's now an interface. I also added quite a bit to ThingImpl.java.

In branch B I edited the class in Thing.java

Now I'm trying to merge my changes from branch A into branch B, and it's trying to merge my changes into the interface Thing.java instead of applying the diff to ThingImpl.java, which is what it should do. Is there a way to tell git to do this?

ario
  • 1,727
  • 4
  • 19
  • 28

3 Answers3

1

It depends on the content of those files and how much they have changed.

You can instruct git merge to detect harder renames, with:

git merge --no-ff -Xrename-threshold=15 -Xpatience -Xignore-space-change A

The -Xrename-threshold=15 would be in order to control that a similarity of 15% is already enough to consider two files rename candidates.

But that won't cover every use case, as shown in "git fails to detect renaming", and in "git merge with renamed files".

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • It didn't work for me. I guess Thing.java has been edited too much in both branches. – ario Dec 10 '16 at 01:05
  • Exactly: if the content is too different, git won't detect the rename. – VonC Dec 10 '16 at 01:08
  • 1
    I think it does not work because there is now a file Thing.java in both branches, and rename detection does not even take place. – j6t Dec 10 '16 at 15:25
1

When you merge branch A into branch B, the expected result is that there is a file Thing.java, which is identical to the version in branch A, and a file ThingImpl.java, that basically the same as in branch A, but with the changes that were made on branch B integrated.

The simplest is this:

git checkout branch_B
git mv Thing.java ThingImpl.java
git commit -m "A technical change to make the upcoming merge possible."
git merge branch_A

Now you do not depend on that rename detection kicks in.

If your build fails after the auxiliary commit and before the merge, you could modify your build procedure to take the renamed file into account.

j6t
  • 9,150
  • 1
  • 15
  • 35
0

I guess there's no way to tell Git exactly what the file history graph looks like, since it relies on its own diff: Getting Git to follow renamed and edited files

I probably should have committed the rename of ThingImpl.java separately from making changes to ThingImpl.java in branch A in order to avoid this issue.

As it is I had to merge manually.

Community
  • 1
  • 1
ario
  • 1,727
  • 4
  • 19
  • 28