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

Particularly the .DS_Store file, I'm on a mac so I of course put this in my .gitignore. I don't care about the file at all. But for some reason it is trying to pull it from the bare repo I setup as a server.

   Updating 8e11f79..b315527
    error: Your local changes to the following files would be overwritten by merge:
        .DS_Store
        config/wi-fi.txt
        web/.DS_Store
    Please, commit your changes or stash them before you can merge.
    Aborting
git


Solution 1:[1]

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.

Solution 2:[2]

Not a silver bullet, but the following worked for me:

git restore --staged ../.DS_Store

Solution 3:[3]

Just remove .DS_Store :)

Worked for me.

Solution 4:[4]

First of all, it is necessary to check and delete .DS_Store file, if it is exists.

I also had this error when there is no such file.

Adding .DS_Store line into .gitignore helped to solve this problem.

Solution 5:[5]

find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch

Solution 6:[6]

After you committed and pushed your changes, can try

git reset --hard

Don't forget to add .DS_Store to .gitignore

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 cmaster - reinstate monica
Solution 2 guzmanoj
Solution 3 Valentin Yuryev
Solution 4 Vladislav Zaynchkovsky
Solution 5 Nikikiy SCC
Solution 6 Eduard Brokan