1

I committed a java file using

git commit -a 

I forgot to add a file to said commit so I tried to amend the previous one using

git commit -a --amend 

When the editor opened up, I changed my mind and closed the editor using :q!.

I then did a git log and my unintentional commit was successful.

Anyone know why git went ahead and committed my 'aborted' commit?

1 Answer 1

1

When you amend a commit, irrespective of whether you forcefully quit the editor or anything, the amend will be successful as long as the commit message was not empty when the .git/COMMIT_MESSAGE file was last saved.

So, with git commit --amend, if you previously had a commit message; you'd need to remove the entire message and then save the buffer. When you quit the edit, the amend step will be aborted.

Sign up to request clarification or add additional context in comments.

9 Comments

What is the reasoning behind git commit --amend going ahead and amending the commit?
@TheCoder The commit hook does not understand your --amend flag.
So my error was combining 2 commands: a commit all + amend my last commit; and git ignored the first one?
Amending commit and commit all are both same from the perspective of commit behaviour. The underlying principle is, amend will load the previous commit message in buffer, and recompute the SHA, thereby generating a new commit (which is what git commit does anyway). --amend only prepopulates certain values for SHA compute based on previous commit (and removes the same from history).
Is a commit basically, deleting/ignoring the previous commit, generating a new SHA and giving you the chance to amend the commit message? Or is there something else it does?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.