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

65,138

Solution 1

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).

Solution 2

you need to abort the current commit by

git merge --abort

then you need to delete the merge message file.

go to the project directory and remove file by the following command

rm .git/.MERGE_MSG.swp

You can also do it with the folowing single command

git merge --abort && rm .git/.MERGE_MSG.swp

Solution 3

In my case, there was a problem closing the VI editor ...

  • Restarting the terminal doesn't work.
  • Restarting my mac solved the issue.
Share:
65,138
Tigerotic
Author by

Tigerotic

Updated on July 09, 2022

Comments

  • Tigerotic
    Tigerotic almost 2 years

    When I pull:

    E325: ATTENTION
    Found a swap file by the name "~/Documents/Sites/recipegenerator/.git/.MERGE_MSG.swp"
              owned by: username   dated: Wed Dec 14 12:28:45 2016
             file name: ~username/Documents/Sites/recipegenerator/.git/MERGE_MSG
              modified: YES
             user name: username   host name: Users-MacBook-Pro.local
            process ID: 33747
    While opening file "/Users/larsvanurk/Documents/Sites/recipegenerator/.git/MERGE_MSG"
                 dated: Thu Dec 22 14:06:17 2016
          NEWER than swap file!
    
    (1) Another program may be editing the same file.
        If this is the case, be careful not to end up with two
        different instances of the same file when making changes.
        Quit, or continue with caution.
    
    (2) An edit session for this file crashed.
        If this is the case, use ":recover" or "vim -r /Users/username/Documents/Sites/recipegenerator/.git/MERGE_MSG"
        to recover the changes (see ":help recovery").
        If you did this already, delete the swap file "/Users/username/Documents/Sites/recipegenerator/.git/.MERGE_MSG.swp"
        to avoid this message.
    
    Swap file "~/Documents/Sites/recipegenerator/.git/.MERGE_MSG.swp" already exists!
    

    When I push:

    To  https://github.com/nickname/recipegenerator.git
     ! [rejected]        master -> master (fetch first)
    error: failed to push some refs to 'https://github.com/nickname/recipegenerator.git'
    hint: Updates were rejected because the remote contains work that you do
    hint: not have locally. This is usually caused by another repository pushing
    hint: to the same ref. You may want to first integrate the remote changes
    hint: (e.g., 'git pull ...') before pushing again.
    hint: See the 'Note about fast-forwards' in 'git push --help' for details.
    

    Please help :C Idk what to do. I can't push or pull. I tried pretty much everything I could think of. I also tried: git merge --abort. The thing is when I do that, I can't seem to find my conflict that I should resolve.

  • Steeve Favre
    Steeve Favre over 3 years
    Same here I closed my terminal and relaunched it and it was solved
  • quetzalcoatl
    quetzalcoatl over 3 years
    How does that differ from my 4-year-old answer? Nevertheless, it's definitely more concise than mine :)
  • quetzalcoatl
    quetzalcoatl over 3 years
    "Restarting my mac solved the issue." - It should not have made any difference. If it had, then apparently you've had another VIM process running. When you restarted your mac, all applications were closed and that rogue VIM session cleaned up its trash. Next time, try running PS (or whatever it is on mac) and see if there are any VIM processes you don't expect.
  • grantespo
    grantespo about 3 years
    @quetzalcoatl, it made a difference for me. While I was rebasing, my computer crashed, so I force shut it down. Thus when i turned my computer back on, I was prompted if I wanted to continue my session. I selected, "Yes", then when i tried to continue my rebase, i got the error that OP mentioned in the question. Aborting the rebase didn't help. But restarting my mac did. I bet if Answered the prompt "No" to restart a new state, this error wouldn't have occured.