Exit Vim without committing changes in Git

GitVimGit Commit

Git Problem Overview


When I use git commit --amend or 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.

How do I exit the editor without committing anything?

Git Solutions


Solution 1 - Git

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

Solution 2 - Git

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.

Solution 3 - Git

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.

Solution 4 - 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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionj0fbView Question on Stackoverflow
Solution 1 - GitEDY ARMENDARIZView Answer on Stackoverflow
Solution 2 - GitIngo KarkatView Answer on Stackoverflow
Solution 3 - GitFDinoffView Answer on Stackoverflow
Solution 4 - GitMirko SteinerView Answer on Stackoverflow