29

When I write git commit --amend I get some kind of editor, where I can change the name of this commit. How to confirm and save my changes using keyboard?

Martin G
  • 17,357
  • 9
  • 82
  • 98
Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
  • 14
    Quit your editor! You're probably using a vi clone (probably vim). Try typing ":wq" and hit return. The colon takes you to command mode, then w to write the buffer, and then q to quit. Don't ever let someone tell you that ctrl-x ctrl-c is more intuitive than "wq" for "write-quit". – William Pursell Jan 27 '15 at 17:58
  • 2
    @WilliamPursell even though I am late for the party, you should type up your comment as an answer. As of now your comment is the only thing on this question actually addressing the problem stated in the question. – dot_Sp0T Dec 05 '17 at 08:59
  • so you can write :wq you need to press ctrl+C to exit editor. If someone else face this problem – Valary o Oct 04 '20 at 11:29
  • 2
    Possible duplicate of [Git - How to close commit editor?](https://stackoverflow.com/questions/13239368/git-how-to-close-commit-editor) – pkamb Oct 06 '20 at 00:01
  • This question is not a duplicate as suggested. The answer is `git commit --amend --no-edit` which amends a commit without changing its commit message. – rbento Mar 19 '21 at 05:46

5 Answers5

36

Expanding on what William Pursell said, you probably ended up in vim. Save your changes and exit the editor by typing : to enter a command, followed by wq, then press enter. To exit vim without saving your changes do :q! instead.

To change this default to something you're more familiar with, you can set the EDITOR variable to something of your choice (try nano).

Just put export EDITOR=nano at the end of ~/.bash_profile (create the file if you don't already have it) to get this behaviour for every new terminal session.

Also, you could do git commit --amend -m 'Your message here' without a need for an editor at all.

Travis
  • 2,579
  • 18
  • 19
  • 2
    While this answer brings up good alternatives it doesn't address the actual problem stated in the question. – dot_Sp0T Dec 05 '17 at 08:57
8

Configure the editor like this (gedit as an example):

git config --global core.editor "gedit"

You can read the current configuration like this:

git config core.editor

You can also add the commit message from the command line.

git commit --amend -m "blablabla"

and the editor will not be opened at all.

Martin G
  • 17,357
  • 9
  • 82
  • 98
  • 2
    While this answer brings up good alternatives it doesn't address the actual problem stated in the question. – dot_Sp0T Dec 05 '17 at 08:58
7

It seems like this thread answers your question: Git - How to close commit editor?

Save the file in the editor. If it's Emacs: CTRLX CTRLS to save then CTRLX CTRLC > to quit or if it's vi: :wq

Press esc first to get out from editing. (in windows/vi)

Community
  • 1
  • 1
6

If you use git bash command prompt, and you have not pushed yet. You can use

git commit --amend -m "new message"

If you have already pushed, you use rebase

git rebase -i HEAD~1

where 'i' means interface and '1' means the last one. If you want last two, you put '2'.

Rebase will take you into a very awkward 'VI' editor. Make sure your keyboard is in "INSERT" mode by insert key.

Then you overwrite the yellow word "pick" to "reword". Then you overwrite the old message after the funny id with your new command. Then you escape your INSERT mode by pressing "Esc" on your keyboard. Now you have to get out 'VI' and save.

Type :wq then hit "ENTER" to save your edit. If you see they give you more questions, hit another "ENTER"

Once you got back to the '$' mode, you do

git push --force

Good luck.

Jenna Leaf
  • 2,255
  • 21
  • 29
3

Just follow the instructions on the lower part of the screen. I remember it said that, press ^O to write out, and then if you have changed anything, it will let's you enter the file name to write to. Just press enter now.

NOTE THAT, ^O means Ctrl+O, and M-D means Esc+D on Ubuntu(as far as I know). This is what confuses us.

You can always use ^G to read the help info.

Scott Yang
  • 339
  • 3
  • 6