Web lists-archives.com

Re: why does "git revert" commit even if i try to bail with ":q!"?




On Tue, Mar 12, 2019 at 10:23 AM Robert P. J. Day <rpjday@xxxxxxxxxxxxxx> wrote:
>
>   never noticed this before ... when i do a regular "git commit" and
> enter my "vi" edit session and change my mind, i can bail with ":q!",
> regardless of what i've set up as a commit message, and i'll see:
>
>   Aborting commit due to empty commit message.
>
> however, i was just playing with "git revert" and, after i ran:
>
>   $ git revert <commit SHA>
>
>   again, simulating that i changed my mind, i just typed ":q!", but
> the revert went ahead, anyway. i tried again, this time completely
> deleting all the lines from the commit msg (as the template
> suggested), but the revert *still* completed after typing ":q!".
>
>   it was only after deleting all the lines and using ":wq" that the
> revert was cancelled:
>
>   Aborting commit due to empty commit message.
>
> that seems ... inconsistent. am i misunderstanding something?

When you're doing a fresh commit, the .git/COMMIT_MSG is "empty". It
has whitespace and comments, but no _usable_ lines. So when you :q!,
the commit is aborted because nothing Git can use as a commit message
was saved to the file and so it's still "empty".

When you use git revert, though, it writes a valid, usable message to
the file ("Revert <subject>\n\nThis reverts commit <sha>"). When you
:q!, that's still in the file. Since the file isn't empty, the commit
moves ahead.

Git doesn't actually _know_ you quit vi with :q!. All it knows is the
editor process completed with 0 exit code, and the message file isn't
"empty". If :q! made vi exit non-zero then you'd see that the commit
(or revert) got canceled because the editor failed (I don't know the
exact message off the top of my head).

An easy way to confirm this behavior would be to run your "git
revert", and, in the editor, delete all the contents, then :w and :q!.
You'll see that the revert is aborted due to an empty message. Or you
could do a normal "git commit", type a message, then :w and :q!.
You'll see that the commit still runs. So the difference between the
two is that one starts with no usable commit message, and the other
one does.

Hope this helps,
Bryan