How to solve git merge error “Swap file .MERGE_MSG.swp already exists”

It’s a message from VIM which apparently you are using as the text editor in git. Have you tried reading and following these two (1) (2) points? One of them will be probably true, and will let you solve this issue.

First of all, check that MERGE_MSG file (not MERGE_MSG.swp), and see if it exists and what’s inside. Most likely it’s trash or a temporary file that can be safely deleted. Judging from the name, it’s probably the file name used as a temporary text editing area for merge commit messages.

Then, since you use VIM, when VIM starts, it tries to create a swap file for its own internal needs. The error message says it’s ~/Documents/Sites/recipegenerator/.git/.MERGE_MSG.swp. Often, you can simply delete such swap files, especially if they are old or unexpected. However, if recently some merge-commit-message-editing session has crashed and if you had a lot of creative text you don’t want to lose – then don’t delete it and recover that swap instead, as described in (2) in the error message.

However, since you don’t know what is going on and you haven’t said anything about losing some text you wrote, and since it’s probably just a MERGE_MSG that was auto-generated anyways, I suppose you can:

git merge --abort
rm ~/Documents/Sites/recipegenerator/.git/.MERGE_MSG.swp

and try what you were doing once again.

Also, it’s good to check the hint mentioned in (1) in error message. Check with ps or whatever else for any open VIM sessions that could be currently editing that MERGE_MSG. If you spot any, then, well, get to them and either finish editing, or make them quit (escape, :q!, enter) (vim will cleanup swaps on quitting), or terminate them (kill them, but then you need to remove swap files manually).

Leave a Comment