Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with xxx~HEAD after git merge?

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?

like image 887
Corey Avatar asked Jan 11 '18 09:01

Corey


People also ask

What should I do after fixing merge conflicts?

When you've successfully solved all conflicts, you need to do two more things: (1) Mark each conflicted file as solved. A simple "git add <filepath>" does this for you. (2) Commit the resolution just as you would commit any other change with the "git commit" command.


1 Answers

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.)

like image 162
Edward Thomson Avatar answered Sep 28 '22 01:09

Edward Thomson