How do I handle the Git error "Your local changes to the following files would be overwritten by merge"

Since you got the error due to a pull, the damage has already been done: Someone has already committed a .DS_Store file to git. This should never happen, of course, but it has happened.

You really don't want such files to be under version control, because

  1. git will modify them when it checks out a commit, or, even worse, it will actively merge changes from someone else into a local .DS_Store when you merge a branch possibly creating a broken .DS_Store file. I have no idea what your OS does with the contents of these files, but it's not expecting you to change them in any way.

  2. You will get an error whenever you try to checkout a revision containing a .DS_Store file when your OS has modified it.

  3. Non-Apple users won't like the .DS_Store files littering your repository.

I would advise you to try to hunt down the origin of the file, because that information is central for how you can fix the issue. The command

git log --all -- .DS_Store

gives you a list of all the commits that have touched the .DS_Store file. These are the commits that you need to rewrite to remove the .DS_Store from your history.

Rewriting the commits can, of course, be done with the big gun git filter-branch, but as long as it's only recent commits that have not been merged yet, you can get away with doing a git rebase -i.

And, of course, when you are done, put .DS_Store in your .gitignore and check it in. This should stop people from (accidentally) adding .DS_Store files in the future.

Tags:

Git