272

Using Windows XP I accidentally typed git commit -a instead of git commit -am "My commit message", and now I'm viewing my CMD prompt filled with the file version of my commit message ("Please enter the commit message for your..."). I've added my message to the top, but now I can't figure out how to save and leave. I tried to press Ctrl + W + Q, but it doesn't do anything, but add ^ where the cursor is.

I also tried Esc first, and then Ctrl + W + Q, but it says No write since last change (add ! to override).

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
orokusaki
  • 48,267
  • 47
  • 159
  • 244

7 Answers7

530

Press ESC to make sure you are out of the edit mode and then type:

:wq
orokusaki
  • 48,267
  • 47
  • 159
  • 244
nunopolonia
  • 13,237
  • 3
  • 24
  • 28
  • 6
    @User789 for anything specific or in general? If you're looking for generic commands try this website: http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html – nunopolonia Nov 07 '13 at 10:35
  • 1
    They really could have added this as a comment so thousands of us weren't sent out of the terminal for three characters!! But I think nunopolonia is glad they didn't =) – Casey Murray Feb 27 '16 at 16:51
  • Keep in mind that if you have ***multiple keyboard layouts*** and you forget you are ***not on english keyboard layout***, you might get stuck and thinking this doesn't work. Hope I helped someone not waste time like I did. – Djuka Oct 07 '16 at 09:52
  • 3
    Thank god I found this. Seriously, it wouldn't have hurt to just add that one piece of direction to the enter commit message screen?? – voidzero Oct 31 '16 at 18:00
52

Instead of telling you how you could execute a certain command (Esc:wq), I can provide you two links that may help you with VIM:

However, the best way to learn Vim is not only using it for Git commits, but as a regular editor for your everyday work.

If you're not going to switch to Vim, it's nonsense to keep its commands in mind. In that case, go and set up your favourite editor to use with Git.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
eckes
  • 56,506
  • 25
  • 151
  • 189
35

Esc to make sure you exit insert mode, then :wq (colon w q) or ZZ (shift-Z shift-Z).

ephemient
  • 180,829
  • 34
  • 259
  • 378
  • 2
    Anyone else noticed how much easier this `Shift-Z` `Shift-Z` is than the other one? And by the way, does anything ever get less intuitive? Oh yes, I'm a citizen of Windows! – Ifedi Okonkwo Apr 22 '15 at 17:28
32
  • Press i or a to get into insert mode, and type the message of choice

  • Press ESC several times to get out of insert mode, or any other mode you might have run into by accident

    • to save, :wq, :x or ZZ

    • to exit without saving, :q! or ZQ

To reload a file and undo all changes you have made...:

Press several times ESC and then enter :e!.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
sjas
  • 15,508
  • 11
  • 75
  • 80
10

A faster way to

  • Save
  • and quit

would be

:x

If you have opened multiple files you may need to do a

:xa
Fabian Schuh
  • 227
  • 1
  • 7
5

:q! will force an unconditional no-save exit

Marc B
  • 340,537
  • 37
  • 382
  • 468
  • 6
    This should be a comment. It's highly relevant but does not answer the question. On contrary it talks about a command that does the opposite of what is asked for. Relevant but not an answer – Rune FS Apr 03 '13 at 09:27
0

Use:

:wq!

The exclamation mark is used for overriding read-only mode.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Hedego
  • 240
  • 1
  • 10