user482745
user482745

Reputation: 1214

Git merging moved and renamed file

In one branch I renamed file. In other branch I moved file. I merged master branch with both branches. Now I have two files in master branch and no conflict during merge. Is Git supposed to behave like that? I would expect at least warning.

Edit: Console gives me waring. So it is egit issue. (Egit is eclipse plugin)

Upvotes: 2

Views: 1614

Answers (1)

CharlesB
CharlesB

Reputation: 90496

Behaviour is normal, because you renamed the file without git mv; git correctly detected file rename, but did not commit deletion of old file, so you have both files now after merge.

Using git mv, or mv followed by git rm oldfile it correctly leads to a merge conflict.

Initialized empty Git repository 
$ echo "hello" > hello.txt
$ git add hello.txt && git commit -m "first"
[master (root-commit) 708ec5f] first
 1 file changed, 1 insertion(+)
 create mode 100644 hello.txt
$ git branch mybranch
$ git mv hello.txt hello2.txt
$ git commit -m "renamed"
[master 00c68ed] renamed
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename hello.txt => hello2.txt (100%)
$ 
$ git checkout mybranch
Switched to branch 'mybranch'
$ mkdir test
$ git mv hello.txt test
$ git commit -m "moved"
[mybranch 044e091] moved
 1 file changed, 0 insertions(+), 0 deletions(-)
 rename hello.txt => test/hello.txt (100%)

$ git checkout master
Switched to branch 'master'
$ git merge mybranch
CONFLICT (rename/rename): Rename "hello.txt"->"hello2.txt" in branch "HEAD" rename "hello.txt"->"test/hello.txt" in "mybranch"
Automatic merge failed; fix conflicts and then commit the result.

Upvotes: 2

Related Questions