126

I accidentally used git commit --amend. My text editor is open and waiting for input. I know, that when I close it now (not changing the existing commit message) the commit will be amended. What can I do to abort the process?

This blog article says that I can simply delete the commit message and the commit will then not be valid and ignored. Is that correct? Are there alternatives?

Flip
  • 6,233
  • 7
  • 46
  • 75
  • This answer helped me https://stackoverflow.com/questions/1459150/how-to-undo-git-commit-amend-done-instead-of-git-commit/1459264 – igauravsehrawat Aug 13 '19 at 09:39

8 Answers8

134

That is correct. Saving the file with no contents will abort the amend.

djb
  • 5,591
  • 5
  • 41
  • 47
  • 9
    I'd say it's less work to add `#` (comment line) to the commit message instead of deleting all the lines. Anyway, same result – Pau May 03 '18 at 12:47
  • 4
    depends, I find "D, shift+G" (or ctrl+A, del) to be pretty quick and way more reassuring ... – Romain Vincent Nov 14 '18 at 21:20
  • 2
    did not work for me (using nano inside intellij integrated terminal), the commit was amended regardless. – Louis Jan 17 '19 at 10:34
  • 1
    Thanks - so simple once you hear the solution. Specifically (for those still lost), `ggdG` (deleting all content) and `:wq` (saving empty commit) worked perfectly as far as backing out of my accidental amendment. – J.M. Janzen Oct 08 '19 at 17:15
107

Adding another answer to this, you can also do

:cq

to abort the amend in Vim which tells it to quit with an error code, causing Git to abort with:

error: There was a problem with the editor 'Vim'.
Please supply the message using either -m or -F option.

From Vim's documentation:

: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.
Dave S
  • 788
  • 1
  • 11
  • 30
aug
  • 11,138
  • 9
  • 72
  • 93
  • 3
    This has the advantage of working in repositories with commit hooks that automatically add to the message (such as adding a Gerrit change ID). – Harry Cutts Apr 30 '19 at 01:22
  • 4
    Great trick! Now the hard part is to remember it. – Gur Nov 08 '21 at 12:11
11

If you delete the commit message (no need to delete the ones starting with #) you will abort the git commit --amend command. You will get output like this:

Aborting commit due to empty commit message.
Alan
  • 1,582
  • 1
  • 13
  • 11
7

delete the message. an empty message will abort any commit (amend is just a 'special' commit)

pedrorijo91
  • 7,635
  • 9
  • 44
  • 82
3

Yes, delete message and quit with :wq. The line begin with #, you can leave it.

the final editor showing


# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Sun Sep 5 17:09:09 2021 +0800
#
# On branch main
#
# Initial commit
#
# Changes to be committed:
#       new file:   b
#
# Changes not staged for commit:
#       modified:   b
#

output after quit

Aborting commit due to empty commit message.
fan123199
  • 49
  • 5
3

This will go back to the commit before, but we don’t lose the changes to the code or files:

git reset HEAD@{1}
nhaht
  • 1,005
  • 12
  • 21
2

Point to note:
Deleting the content and doing :q! still commits. I don't understand ViM totally, so, can't say why.

You have to write the final empty/remaining content to file as well, so to speak. So, :wq it is.

  • 2
    `:q!` will force quit VIM, even if there are unsaved changes. This means the original content of the file, our existing commit message in this case, will remain in the file, so the commit is written. – tannerli Dec 13 '21 at 15:00
0

Agree that saving the contents of the file as empty will abort the commit, but I want to add to delete all lines in vi/vim run:

:1,$d

then do

:wq

Finn
  • 61
  • 1
  • 2