67

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?

3
  • Note that git reflog is local to that working copy. It's the chronological history of your HEAD rather than the logical history. Commented May 16, 2013 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!. Commented May 16, 2013 at 19:35
  • git replaces the previous commit(the hash tag changes) even if I exit without saving. Commented May 16, 2013 at 20:21

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

2 Comments

+++++++ amazing!
You should use cq with the !it forces vim to save whereas without ! it closes the editor with an error without saving.
34

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.

5 Comments

+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.
@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!
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.
@CharlesBailey Thank you for badgering me towards a better answer; I've incorporated your suggestion; hopefully, I've earned your +1 now :-)
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.
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.

2 Comments

do I \delete the comments also or just the non-comment part?
@Shanimal non comments is sufficent.
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.

1 Comment

You should use cq with the ! it forces vim to save whereas without ! it closes the editor with an error without saving.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.