parsley72
parsley72

Reputation: 9087

Why does git merge delete files rather than moving them?

I have an old branch that I want to bring up to date. I merged from master but there are three files that have been moved and Git is saying they've been "deleted by them".

I first tried moving the files to the new location in my branch then merging but git just used the branch files and didn't attempt to merge with master.

I then tried setting:

git config merge.renameLimit 999999

but I got the same result.

Is there anything else I can do to force Git to recognise that these files have been moved rather than deleted? The filenames are the same.

Upvotes: 3

Views: 202

Answers (1)

jthill
jthill

Reputation: 60393

You've hit a (really, the) downside of Git's method of recording only snaphots and ancestry: ordinarily, Git can identify renamed files because they still look similar enough, but if you drastically change a file and also move it, there's nothing left to distinguish the result from a deleted old file and a new other file in this new location.

git checkout master^0                         # make a scratch commit to help Git
git mv new/path/to/file1 old/path/to/file1    # move the drastic-rewrite stuff back
git commit -m \
    'put drastically-changed-and-renamed files back where automerge can find them'

git checkout oldstuff                         # merge the helper commit to oldstuff
git merge @{1}                                # ...
git mv old/path/to/file1 new/path/to/file1    # put things in their new location
git commit --amend

Git's merge looks at the tips and the base. Since these files at the master tip look nothing at all like what they did at the merge base with oldstuff and have also picked up and moved since, merge doesn't see them as the same files any more. So put them back to help Git's automerge, run the merge, move them back to their new home.

To answer your title question directly, why Git does its snapshots-and-ancestry-only histories, it's an engineering tradeoff. There's downsides no matter what you do, you've hit the downside of Git's choice.

Upvotes: 4

Related Questions