3

When I merge changes from a remote repository which deletes a file, the local copy will be unlinked (if it doesn't have any changes).

How can I teach git to simply leave them on disk?

EDITED:

I can un-cache the deleted files before merging, thus

git diff --name-only -z --diff-filter=D ..origin/master | \
    xargs -0 git rm --cached && \
    git commit -m 'un-cache deleted files'
git merge origin/master

so that's not the problem. My users are (as usual ;-) ).

Thus: Is it possible to do this without the additional commit? Is it possible to block a merge which deletes a file?

Matthias Urlichs
  • 2,301
  • 19
  • 29

3 Answers3

1

This worked for me:

git reset -- any_deleted_files
git reset any_deleted_directories
git checkout -- .
git commit -am "resolved merge"

This removes git's knowledge of the deletion of the files entirely.

0

Git has to choose one parent's version of the story. In this case, it is choosing to delete the file which is the version of what happened in the remote repository. Playing the devil's advocate, suppose that you deleted a file in your branch and then pushed that to the remote, to be merged into the remote branch. You would probably expect that Git would delete the file from the remote also. So, there may not be a silver bullet solution here. What you view as undesirable and unexpected behavior in one case could be the opposite in another case.

If you follow the link below, you will find concise instructions for how to add back a deleted file.

Reference: git merge: Removing files I want to keep!

Community
  • 1
  • 1
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

One way is to go ahead and commit those files on a new branch.

Those files are useful to you (and someone else who already committed them). What you may really want to do is combine your file with theirs. To do that, commit the files locally on a new branch, then merge that branch after you git pull.

willoller
  • 7,106
  • 1
  • 35
  • 63
  • Well, sometimes you don't want to combine anything. One example is using git for a home directory which has a bunch of volatile machine-local config files. I want to remove them from git but *not* from the other systems I pull this repository from. – Matthias Urlichs Aug 27 '17 at 10:04