退出 Vim 而不在 Git 中提交更改

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16595908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 08:32:31  来源:igfitidea点击:

Exit Vim without committing changes in Git

gitvimgit-commit

提问by j0fb

When I use git commit --amendor git rebase -i, vim opens up for me to make changes. If I then change my mind and exit vim without making any changes, a commit is still made which shows up in git reflog.

当我使用git commit --amendor 时git rebase -i,vim 会打开让我进行更改。如果我改变主意并退出 vim 而不做任何更改,仍然会进行提交,该提交显示在git reflog.

How do I exit the editor without committing anything?

如何退出编辑器而不提交任何内容?

采纳答案by FDinoff

To get git to not make a change when you are executing git commit --amendor git rebase -i.

为了让 git 在您执行git commit --amendgit rebase -i.

Just delete the message (and save). All git does is look for a non empty message to see if a valid commit happened. Since there is a commit message (because you commited something before) git thinks that its a valid commit or rebase.

只需删除消息(并保存)。git 所做的只是查找非空消息以查看是否发生了有效的提交。由于有一个提交消息(因为你之前提交了一些东西),git 认为它是一个有效的提交或变基。

回答by EDY ARMENDARIZ

:cq!

This will force an error to Vim and it will not save any changes. Linkto Vim manual.

这将强制 Vim 出错,并且不会保存任何更改。链接到 Vim 手册。

You may want to use cqwithout !if you want vimto exit with an error and without saving.

如果您想退出并出现错误并且不保存,您可能想要使用cqwithout 。!vim

回答by Ingo Karkat

When you haven't made changes and saved them, :q!could suffice (in a plain commit; when you're not amending), but if you are like me, chances are you've already (even unconsciously) persisted the edited message.

当您没有进行更改并保存它们时,:q!就足够了(在简单的提交中;当您没有修改时),但是如果您像我一样,很可能您已经(甚至无意识地)保留了编辑过的消息。

Git (and other such tools that use Vim to edit a message) will abort the entire process (and ignore any saved changes to the message) if the editor quits with a non-success exit status. You can do that in Vim with the :cq[uit]!command.

如果编辑器以非成功退出状态退出,则 Git(以及其他使用 Vim 编辑消息的此类工具)将中止整个过程(并忽略对消息的任何已保存更改)。你可以在 Vim 中使用:cq[uit]!命令来做到这一点。

You may want to use cqwithout !if you want vimto exit with an error and without saving.

如果您想退出并出现错误并且不保存,您可能想要使用cqwithout 。!vim

回答by Mirko Steiner

the git appication runs the editor application, and if the editor application returnes unsuccessfully (non-zero exitcode) the git application recognizes this and stops further processing.

git appication 运行编辑器应用程序,如果编辑器应用程序返回失败(非零退出代码),git 应用程序会识别出这一点并停止进一步处理。

in vim you can perform this with :cq!

在 vim 中,您可以执行此操作 :cq!

from the vim manual:

来自vim手册:

                                                        :cq :cquit 
:cq[uit][!]             Quit Vim with an error code, so that the compiler
                        will not compile the same file again.
                        WARNING: All changes in files are lost!  Also when the
                        [!] is not used.  It works like ":qall!" :qall,
                        except that Vim returns a non-zero exit code.

this works for svn, too! the difference AFAIK between svn and git is, that svn don't like empty commit messages and stops when you quit with :q!(even if the exitcode is 0) but for git this is ok. for both it is not ok, if the editor gives an non-zero exitcode.

这也适用于 svn!svn 和 git 之间的 AFAIK 区别在于,svn 不喜欢空提交消息并在您退出时停止:q!(即使退出代码为 0),但对于 git 来说这是可以的。如果编辑器给出非零退出代码,则两者都不行。

exitcodes are a very fundamental concept in unix/linux and and easy way to inform the caller application if everyhing was ok (exitcode 0) or something went wrong.

exitcodes 是 unix/linux 中的一个非常基本的概念,并且是通知调用者应用程序是否一切正常(exitcode 0)或出现问题的简单方法。