Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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
like image 386
cade galt Avatar asked Oct 17 '15 12:10

cade galt


People also ask

How do you fix error your local changes to the following files would be overwritten by checkout?

The Git “Your local changes to the following files would be overwritten by checkout” error occurs when you make changes on two branches without committing or stashing those changes and try to navigate between the branches. You can fix this issue by either stashing your changes for later or adding them to a commit.

How do I get my local changes back in git?

If you have committed changes to a file (i.e. you have run both git add and git commit ), and want to undo those changes, then you can use git reset HEAD~ to undo your commit.


3 Answers

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

git restore --staged ../.DS_Store
like image 187
guzmanoj Avatar answered Oct 04 '22 02:10

guzmanoj


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.

like image 35
cmaster - reinstate monica Avatar answered Oct 04 '22 00:10

cmaster - reinstate monica


Just remove .DS_Store :)

Worked for me.

like image 39
Valentin Yuryev Avatar answered Oct 04 '22 00:10

Valentin Yuryev