62

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?

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
j0fb
  • 723
  • 1
  • 5
  • 4
  • Note that `git reflog` is local to that working copy. It's the chronological history of your HEAD rather than the logical history. – Ben Jackson May 16 '13 at 19:35
  • 2
    git commits when it detects the temp file has changed (been saved). The more simple question here is "How do I exit vim without saving?" The answer, as gpojd has shown, is `:q!`. – Jonathon Reinhart May 16 '13 at 19:35
  • git replaces the previous commit(the hash tag changes) even if I exit without saving. – j0fb May 16 '13 at 20:21
  • 1
    Possible duplicate of [How do I stop a Git commit when VI is on the screen waiting for a commit message?](http://stackoverflow.com/questions/4323440/how-do-i-stop-a-git-commit-when-vi-is-on-the-screen-waiting-for-a-commit-message) – 203 Nov 22 '16 at 15:46

4 Answers4

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

belka
  • 1,480
  • 1
  • 18
  • 31
EDY ARMENDARIZ
  • 901
  • 1
  • 6
  • 3
32

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.

belka
  • 1,480
  • 1
  • 18
  • 31
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • 18
    +1 for `:cq`, but -1 for `:q!` - this isn't correct, it just keeps the pre-amended commit message and applies any actual staged changes. – CB Bailey May 17 '13 at 07:47
  • 1
    @CharlesBailey For amend, you're right, but for plain commits, the empty message will prevent Git from committing. I intended my first sentence as a lead-in towards the actual answer; the discussion in gpjod's answer already showed its problems. But thanks for highlighting this once more! – Ingo Karkat May 17 '13 at 08:30
  • 1
    OK, but the whole question is about the 'amend' case only, so I think it's a little misleading to mention it at all; at least without saying something like "if you're **not** amending..." first. – CB Bailey May 17 '13 at 08:32
  • @CharlesBailey Thank you for badgering me towards a better answer; I've incorporated your suggestion; hopefully, I've earned your +1 now :-) – Ingo Karkat May 17 '13 at 09:13
  • This baffles me that the usual `:q!` doesn't work here, as it does in the case of a normal commit!? I just mangled my repo amending uncommitted added files I wasn't aware of, because of that. – macieksk Mar 12 '20 at 15:40
25

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.

FDinoff
  • 30,689
  • 5
  • 75
  • 96
4

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.

Mirko Steiner
  • 354
  • 1
  • 5
  • You should use `cq` with the `!` it forces vim to save whereas without `!` it closes the editor with an error without saving. – belka Dec 09 '19 at 10:30