42

In one branch in one branch A a file is changed and the change commited. Now in another branch B the very same file is edited and renamed.

When merging B into A git recognises the conflict properly (CONFLICT (delete/modify)) and both files are in the working directory.

If I know want to have both changes in one file, how do I do this best?

There is git merge-file that is - if I'm right - expecting both files and a common ancestor. But how to give latter? How can I say "use $path from $commit" or something like that?

Example:

mkdir git-rename-repo
cd git-rename-repo
git init

echo "First line" > afile

git add .
git commit -m "First commit in master"
git checkout -b mybranch

echo "Second line in mybranch" >> afile
git mv afile bfile

git commit -a -m "change and rename in mybranch"
git checkout master

echo "Changed first line in master" > afile

git commit -a -m "changed afile"
git merge mybranch

I now want a file named 'bfile' with both changes:

Changed first line in master Second line in mybranch

Thanks

1

4 Answers 4

19

I also had the scenario

**CONFLICT (modify/delete):***FileName deleted in HEAD and modified in 6efb2a94ba0601aa1ec5050ab222a0359ee8379a. Version 6efb2a94ba0601aa1ec5050ab222a0359ee8379a of FileName left in tree.*

I was also equally confused and reached this post. But on typing git status, all doubts are vanished. git status, stated the following about the conflicted files:

Unmerged paths:
(use "git add/rm ..." as appropriate to mark resolution)

So I just did git rm FileName, and after that the CONFLICT got resolved.

0
15

On my case, it was a deleted file not present on the repository

CONFLICT (delete/modify): ERD.pdf deleted in HEAD and ... 

I just need to do: git rm ERD.pdf

Hope that helps.

4
  • 3
    That assumes you want to accept the deleted file, which is explicitly not true in the question.
    – Jan Hudec
    Commented Mar 26, 2015 at 8:34
  • This means your local doesn't have the file, and git have it on is stash. The reason why you have the conflict- Commented Mar 26, 2015 at 9:39
  • The original poster (it was not me) DOES HAVE THE FILE in the local revision. It is however NAMED DIFFERENTLY. Therefore suggesting to take the deleted version is wrong. The changes must be merged to the file under it's current, changed, name.
    – Jan Hudec
    Commented Mar 26, 2015 at 10:27
  • Look, my answer it's explicit: I say: "On my case [...] Hope that helps.", the idea it's to give simple solutions to help the most newbie people and more expert. Commented Mar 31, 2015 at 17:36
3

UPDATE Okay, git's recursive merge algorithm does this just fine by itself. I just used too small files to test it so the relative similiarity was beneath the trigger of the rename detection.

If I change one line of a file with two small lines the relative change is very big.


Of course I could do something like

git show HEAD^:afile > afile_ancestor
kdiff3 -m afile_ancestor afile bfile

P.S.: Sorry for the broken formatting above. I didn't had switched on JavaScript, so I couldn't see a preview.

3

git status will help you here.

If you want to add files to current branch do git add <file-name> If you want to remove/delete files from the current branch do git rm <file-name>

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.