如何处理来自 'git commit -a' 命令的交换文件错误?
How to deal with a swap file error from the 'git commit -a' command?
我目前正在 git 集线器上工作,我正处于练习如何控制文件的初级阶段。
以下是我目前的状态:
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'。然后,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
# 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
现在没有任何效果,我不知道下一步该怎么做。
这是Vim,不是Git
这不是 Git 的问题,而是由 Vim 编辑器引起的。它抱怨的文件称为 "swap file".
什么是交换文件?
Vim 出于各种目的使用交换文件,这些文件遵循格式
.{original_filename}.swp
因此默认情况下,如果您正在编辑文件 COMMIT_EDITMSG
,则会创建一个名为 .COMMIT_EDITMSG.swp
的交换文件。这本身就是正常的。通常当 Vim 退出时,它会清理这些文件。
但是如果 Vim 加载一个文件并发现,当它试图创建一个交换文件时,该文件已经存在,它会给你这个警告并询问如何继续。这是为了保护用户免受他们自己的伤害 - Vim 假设有两个编辑器打开同一个文件,这可能会导致一个覆盖另一个的更改,并丢失工作。
有几种方法可以实现。
- 您有多个编辑者打开编辑同一个文件。
- 其他人也在编辑文件。
- vim 的前一个实例在编辑此文件时崩溃,并且没有自行清理交换文件。
那个提交信息是怎么回事?
已加载到您的提交消息中的文本只是 Vim 抱怨的副作用。只需删除该文本并编写您自己的提交消息。
如何解决这个问题
如果您有理由确定您或其他人没有在另一个会话中合法地编辑该文件,那么解决此问题的最简单方法是删除交换文件,它应该可以解决问题。
rm -f ~/Desktop/ComputerScience/GitHub/.git/.COMMIT_EDITMSG.swp
我目前正在 git 集线器上工作,我正处于练习如何控制文件的初级阶段。
以下是我目前的状态:
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'。然后,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
# 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
现在没有任何效果,我不知道下一步该怎么做。
这是Vim,不是Git
这不是 Git 的问题,而是由 Vim 编辑器引起的。它抱怨的文件称为 "swap file".
什么是交换文件?
Vim 出于各种目的使用交换文件,这些文件遵循格式
.{original_filename}.swp
因此默认情况下,如果您正在编辑文件 COMMIT_EDITMSG
,则会创建一个名为 .COMMIT_EDITMSG.swp
的交换文件。这本身就是正常的。通常当 Vim 退出时,它会清理这些文件。
但是如果 Vim 加载一个文件并发现,当它试图创建一个交换文件时,该文件已经存在,它会给你这个警告并询问如何继续。这是为了保护用户免受他们自己的伤害 - Vim 假设有两个编辑器打开同一个文件,这可能会导致一个覆盖另一个的更改,并丢失工作。
有几种方法可以实现。
- 您有多个编辑者打开编辑同一个文件。
- 其他人也在编辑文件。
- vim 的前一个实例在编辑此文件时崩溃,并且没有自行清理交换文件。
那个提交信息是怎么回事?
已加载到您的提交消息中的文本只是 Vim 抱怨的副作用。只需删除该文本并编写您自己的提交消息。
如何解决这个问题
如果您有理由确定您或其他人没有在另一个会话中合法地编辑该文件,那么解决此问题的最简单方法是删除交换文件,它应该可以解决问题。
rm -f ~/Desktop/ComputerScience/GitHub/.git/.COMMIT_EDITMSG.swp