git commit --amend
(correctly spelled) is actually more dangerous than git commit -a -m "mend"
. The former will rewrite an existing commit, replacing it by a new commit, so this is more or less a destructive command. The latter however will just create a commit that you didn’t intend to do. This is a constructive command that does not rewrite any existing history but just adds to it.
Adding a commit that you didn’t intend to add is maybe bothersome but not really problematic at all. You can simply undo that commit (without losing any content) using git reset --soft HEAD~1
. This will reset the branch pointer to the commit before without affecting your working directory.
Of course, you can also undo the amendment of a commit but this is more a destructive command with which you need to be a bit careful.
So I personally wouldn’t bother with Git interpreting it the wrong way. Yes, it’s annoying, but that’s all there is to it. Just undo it when you notice it.
What I would personally recommend, especially if you find yourself amending commits more often, to create an alias for git commit --amend
. I personally use git amend
. You can set this up using the following ocmmand:
git config --global alias.amend "commit --amend"
You can also add -C HEAD
if you don’t want to trigger an edit but just want to keep the original commit message (I do this a lot, so this is the alias I am using):
git config --global alias.amend "commit --amend -C HEAD"
git commit -ammend
are not as disastrous as you may think. The original commit is the parent of the one you just created by mistake; it didn't go anywhere. – Turnbullgit config --global alias.amend 'commit --amend'
. So every time you need to amend a commit, you just need to usegit amend
. – Catfish