如何处理'git commit -a'命令中的交换文件错误?

时间:2017-03-12 13:51:14

标签: git bash vim git-commit

我目前正在开发git hub,我正处于初级阶段,练习如何控制文件。

以下是我目前的状态:

On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   index.html

no changes added to commit (use "git add" and/or "git commit -a")

现在我正在尝试使用git commit -a命令进行修改。

但是只要我输入git commit -a,bash shell就会显示如下内容:

E325: ATTENTION
Found a swap file by the name "~/Desktop/ComputerScience/GitHub/.git/.COMMIT_EDITMSG.swp"
      owned by: choehyeong-gyu   dated: Sun Mar 12 22:31:25 2017
     file name: ~choehyeonggyu/Desktop/ComputerScience/GitHub/.git/COMMIT_EDITMSG
      modified: YES
     user name: choehyeong-gyu   host name: choehyeong-gyuuiMacBookPro.local
    process ID: 6651
While opening file "/Users/choehyeonggyu/Desktop/ComputerScience/GitHub/.git/COMMIT_EDITMSG"
         dated: Sun Mar 12 22:41:36 2017
  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/choehyeong-gyu/Desktop/ComputerScience/GitHub/.git/COMMIT_EDITMSG"
to recover the changes (see ":help recovery").
If you did this already, delete the swap file "/Users/choehyeong-gyu/Desktop/ComputerScience/GitHub/.git/.COMMIT_EDITMSG.swp"
to avoid this message.

Swap file "~/Desktop/ComputerScience/GitHub/.git/.COMMIT_EDITMSG.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort:

我对此毫无准备,只是进入'E'。然后,外壳喊出来,

E325: ATTENTION
Found a swap file by the name     "~/Desktop/ComputerScience/GitHub/.git/.COMMIT_EDITMSG.swp"
      owned by: choehyeong-gyu   dated: Sun Mar 12 22:31:25 2017

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
#       modified:   index.html
#
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"~/Desktop/ComputerScience/GitHub/.git/COMMIT_EDITMSG" 7L, 209C

现在没有任何工作,我不知道如何采取下一步措施。

1 个答案:

答案 0 :(得分:0)

这是Vim,而不是Git

这不是Git的问题,而是由Vim编辑器引起的。它抱怨的文件被称为“交换文件”。

什么是交换文件?

Vim使用交换文件用于各种目的,这些文件遵循格式

.{original_filename}.swp

因此,默认情况下,如果您正在编辑文件COMMIT_EDITMSG,则会创建名为.COMMIT_EDITMSG.swp的交换文件。这本身就是正常的。通常当Vim退出时,它会清除这些文件。

但是如果Vim加载一个文件并发现,当它尝试创建一个交换文件时,它已经存在,它会给你这个警告并询问如何继续。这是为了保护用户免受攻击--Vim假设在同一个文件上打开了两个编辑器,这可能导致一个人覆盖另一个人的更改,并失去工作。

有几种方法可以实现。

  1. 您有多个编辑者打开编辑同一个文件。
  2. 其他人也在编辑文件。
  3. 上一个vim实例在编辑此文件时崩溃,并且没有自行清理交换文件。
  4. 该提交消息是什么?

    已经加载到提交消息中的文本只是Vim抱怨的副作用。只需删除该文本并编写自己的提交消息。

    如何解决此问题

    如果您有理由确定您或其他人未在另一个会话中合法地编辑该文件,那么解决此问题的最简单方法是删除交换文件,它应解决问题。

    rm -f ~/Desktop/ComputerScience/GitHub/.git/.COMMIT_EDITMSG.swp