6

In my branchA:

I copy a file from folder04/config.xml to folder05/config.xml, then git add and commit.

In my branchB (B is branch out from A):

I already have one file named folder05/config.xml.

Now I merge branchA into branchB, it appears a weird conflict like this.

Untracked files:   (use "git add <file>..." to include in what will be
committed)

    folder05/config.xml~HEAD

It generates a file named config.xml~HEAD, what does it means?

I can't understand why, and what should I do in this situation?

kowsky
  • 12,647
  • 2
  • 28
  • 41
Corey
  • 1,217
  • 3
  • 22
  • 39
  • I cannot reproduce this; when I try there is a merge conflict in `config.xml` upon merging `branchA` into `branchB`. Can you provide details about how this happened? When was `folder05/config.xml` added to `branchB`? When did `branchB` branch off from `branchA`? Did `folder05` already exist then? – kowsky Jan 11 '18 at 10:27
  • branchB added folder05/config.xml after it branch out from branchA, so before I commit folder05/config.xml in branchA, branchB already have that file. – Corey Jan 11 '18 at 10:35
  • 1
    I still can't reproduce this, but I would suspect that the untracked file `folder05/config.xml~HEAD` is the `branchB`-version of the file, while the real file (`folder05/config.xml`) is the one from `branchA`. Is this the case? – kowsky Jan 11 '18 at 10:38
  • branchA version has more characters. – Corey Jan 11 '18 at 10:43
  • folder05/config.xml~HEAD is the branchB-version of the file <-- that make sense – Corey Jan 11 '18 at 10:45
  • 1
    @kowsky: it's a bit tricky to produce the problem (and it would have been helpful for Kuanlin Chen to have quoted the `CONFLICT (rename/add)` or whatever line), but see [Edward Thomson's answer](https://stackoverflow.com/a/48208268/1256452). – torek Jan 11 '18 at 15:55

1 Answers1

12

It generates a file named config.xml~HEAD, what does it means?

This file was generated automatically by git because you had a filename collision conflict - you had two items that were unrelated in the two branches trying to occupy the name config.xml.

This could be caused by a variety of situations:

Most likely, you created file named config.xml in your branch. In the branch you're merging in, a different file was renamed to config.xml. Since these two files lack any common ancestor, they can't be merged. They should both be kept in the working directory, but as a conflict. So they will both be kept, but with unique names.

The config.xml in the branch being merged will be checked out on disk as config.xml. The config.xml in your branch will be checked out on disk as config.xml~HEAD.

Here's an example, where we add a file named newname.txt in our master branch, and rename file.txt into newname.txt in the branch that we're merging in. In this case, a conflict will be raised and our branch's contents will be written as newname.txt~HEAD in the working folder:

% git checkout -bbranch
Switched to a new branch 'branch'

% git mv file.txt newname.txt

% git commit -m"rename file -> newname"
[branch d37e379] rename file -> newname
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename file.txt => newname.txt (100%)

% git checkout master
Switched to branch 'master'

% echo "new file" > newname.txt

% git add newname.txt

% git commit -m"added newname in master"
[master f7bd593] added newname in master
 1 file changed, 1 insertion(+)
 create mode 100644 newname.txt

% git merge branch
CONFLICT (rename/add): Rename file.txt->newname.txt in branch. newname.txt added in HEAD
Adding as newname.txt~HEAD instead
Automatic merge failed; fix conflicts and then commit the result.

Once you have resolved the conflict to your satisfaction and committed the merge, you can simply delete the ~HEAD file from your working directory.

(Finally, remember that git does not track that renames occurred, it uses heuristics to detect renames. So this conflict - in practice - may not really be a conflict because the files may not have really been renamed.)

Edward Thomson
  • 74,857
  • 14
  • 158
  • 187