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

Karl Bielefeldt
  • 47,314
  • 10
  • 60
  • 94
mokasin
  • 551
  • 1
  • 4
  • 6
  • Possible duplicate of [git - merge conflict when local is deleted but file exists in remote](https://stackoverflow.com/questions/4319486/git-merge-conflict-when-local-is-deleted-but-file-exists-in-remote) – IMSoP Jul 19 '19 at 09:30

4 Answers4

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.

Jack A.
  • 4,245
  • 1
  • 20
  • 34
shah1988
  • 2,584
  • 1
  • 19
  • 21
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.

workdreamer
  • 2,836
  • 1
  • 35
  • 37
  • 3
    That assumes you want to accept the deleted file, which is explicitly _not true_ in the question. – Jan Hudec Mar 26 '15 at 08:34
  • This means your local doesn't have the file, and git have it on is stash. The reason why you have the conflict- – workdreamer Mar 26 '15 at 09: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 Mar 26 '15 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. – workdreamer Mar 31 '15 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.

mokasin
  • 551
  • 1
  • 4
  • 6
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>

mGeek
  • 786
  • 8
  • 11