How to abort 'git commit --amend'?
GitGit Problem Overview
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?
Git Solutions
Solution 1 - Git
That is correct. Saving the file with no contents will abort the amend.
Solution 2 - Git
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.
Solution 3 - Git
delete the message. an empty message will abort any commit (amend is just a 'special' commit)
Solution 4 - Git
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.
Solution 5 - Git
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.
Solution 6 - Git
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.
Solution 7 - Git
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