Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't git attempt to merge changes to renamed files?

Tags:

git

merge

Let's say I have a file that's

  1. Modified in master
  2. Modified in a feature branch
  3. Renamed in a feature branch

When I try to merge up from master to the feature branch, merge fails with

CONFLICT (modify/delete): X deleted in HEAD and modified in origin/master. Version origin/master of X left in tree.

I understand that there is a conflict, but why doesn't it even try to merge changes and place conflict markers in the file? Previous answers seem to imply that it should. All I get is two different versions of the file, where I have to figure out the difference manually and port changes line by line from master version to my version.

Steps to reproduce:

git init
touch a
git add a
git commit -m 'initial import'

git checkout -b feature1
echo feature1 > a
git add a
git commit -m feature1
git mv a b
git commit -m feature1

git checkout master
echo bugfix > a
git add a
git commit -m bugfix

git checkout feature1 
git merge master 
like image 417
Alex B Avatar asked Jun 20 '12 06:06

Alex B


People also ask

What happens when you rename a file in git?

We use the git mv command in git to rename and move files. We only use the command for convenience. It does not rename or move the actual file, but rather deletes the existing file and creates a new file with a new name or in another folder.

How does git detect renamed files?

Detect renames. If n is specified, it is a threshold on the similarity index (i.e. amount of addition/deletions compared to the file's size). For example, -M90% means Git should consider a delete/add pair to be a rename if more than 90% of the file hasn't changed.

Why git merge not working already up to date?

Why git merge not working already up-to-date? The message “Already up-to-date” means that all the changes from the branch you're trying to merge have already been merged to the branch you're currently on. More specifically it means that the branch you're trying to merge is a parent of your current branch.


1 Answers

Because there is actually no concept of first-class rename operation in git, it just "detects" renames using a threshold for file differences. Your files are probably too different.

Try merging with: git merge master -s recursive -X rename-threshold=5%

like image 118
akent Avatar answered Oct 11 '22 13:10

akent