如何解决 git 合并错误 "Swap file .MERGE_MSG.swp already exists"

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

当我拉动时:

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!

当我推送时:

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.

请帮忙 :C 不知道该怎么做。我不能推也不能拉。我几乎尝试了所有我能想到的方法。我也试过:git merge --abort。问题是当我这样做时,我似乎找不到我应该解决的冲突。

这是一条来自 VIM 的消息,显然您在 git 中将其用作文本编辑器。您是否尝试阅读并遵循这两 (1) (2) 点?其中一个可能是真的,会让你解决这个问题。

首先,检查 MERGE_MSG 文件(不是 MERGE_MSG.swp),看看它是否存在以及里面有什么。它很可能是垃圾或可以安全删除的临时文件。从名字看,应该是作为合并提交信息的临时文本编辑区的文件名

然后,由于您使用 VIM,当 VIM 启动时,它会尝试创建一个交换文件以满足其内部需要。错误消息说它是 ~/Documents/Sites/recipegenerator/.git/.MERGE_MSG.swp。通常,您可以简单地删除此类交换文件,尤其是当它们是旧的或意外的时。但是,如果最近某个合并提交消息编辑会话崩溃了,并且如果您有很多不想丢失的创意文本 - 那么请不要删除它并 恢复 而是交换,如错误消息中的 (2) 中所述。

但是,由于您不知道发生了什么,也没有说丢失您写的一些文本,而且它可能只是自动生成的 MERGE_MSG,所以我假设你可以:

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

然后再次尝试您正在做的事情。

另外,最好查看报错信息中(1)中提到的提示。检查 ps 或其他任何打开的 VIM 会话,这些会话当前可能正在编辑 MERGE_MSG。如果你发现任何东西,那么,好吧,找到它们并完成编辑,或者让它们退出(转义,:q!,输入)(vim 将在退出时清理交换),或者终止它们(杀死它们,但随后您需要手动删除交换文件)。

我的终端也有同样的问题,我退出了终端,它对我有用!!!

有同样的问题并修复它退出终端并再次提交。

您需要通过

中止当前提交
git merge --abort

那么您需要删除合并消息文件。

转到项目目录并通过以下命令删除文件

rm .git/.MERGE_MSG.swp

您也可以使用以下单个命令来完成

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

就我而言,关闭 VI 编辑器时出现问题 ...

  • 重启终端无效。
  • 重新启动我的 mac 解决了问题。

通常,当您从两个不同的地方执行 GIT 操作时,您会收到此错误消息。就像我在使用 Git Bash(命令行)和 Visual Studio 一起执行 Git 操作时遇到这个问题一样。

原因:

当在 GIT 的两个实例之一中进行提交或合并会话时,会出现此问题。即 Git Bash & Visual Studio Git。 两个实例都尝试编辑同一个文件,我们收到此错误消息。

解法:

在两个打开的实例之一中完成提交或合并会话。 之后删除 .swp 文件(如果它在提交后仍然存在(来自任何一个实例)。

参考 -