Exit Vim without committing changes in Git

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.

in vim you can perform this with :cq!

from the vim manual:

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

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.


:cq!

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

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


To get git to not make a change when you are executing git commit --amend or git 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.


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.

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.

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