Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git commit --amend is dangerous

I sometimes accidentally type git commit -amend when I really wanted to git commit --amend. Git detects that and asks me

$ git commit -amend
error: did you mean `--amend` (with two dashes ?)

which is great.

However, sometimes I write git commit -ammend and then git will just accept that and treat my spelling mistake as git -a -m "mend", so it justs commits with the log message "mend".

I am looking for a way to stop git doing so. I've tried to define an alias for git commit -ammend but failed for now. Do you have any suggestions how to deal with this issue?

like image 350
codingdave Avatar asked Dec 22 '16 16:12

codingdave


1 Answers

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"
like image 152
poke Avatar answered Oct 27 '22 19:10

poke