177

How do I enter an exclamation point into a Git commit message from the command line?

It is possible to escape the exclamation point with a backslash, but then the backslash ends up in the commit message as well.

I want something like this:

git commit -am "Nailed it!"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Daniel X Moore
  • 14,637
  • 17
  • 80
  • 92
  • 9
    For an explanation of why this problem exists in the first place: http://askubuntu.com/questions/434832/why-does-exclamation-mark-within-double-quotes-cause-a-bash-error – Mark Amery Jan 06 '16 at 19:21
  • *It is possible to escape the exclamation point with a backslash, but then the backslash ends up in the commit message as well.* I've noticed that behaviour as well when using `grep`. I can't make sense of it! – AJM Apr 04 '23 at 12:54

6 Answers6

257

Use single quotes instead of double quotes

git commit -am 'Nailed it!'

Alternatively, if you need to use double quotes for whatever reason but still want a literal ! then turn off history expansion at the top of your script via set +H

SiegeX
  • 135,741
  • 24
  • 144
  • 154
51

Another way to solve that is to add a space after ! like:

git commit -am "Nailed it! "

Note the space between ! and the last ".

(The space won't be included in the commit message - Git trims trailing whitespace from commit messages automatically.)

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
nicky_zs
  • 3,633
  • 1
  • 18
  • 26
  • 3
    This seems the simplest solution to me, and allows double-quotes with variable interpolation: `git commit -m "$(sillyname) $(buzzphrase). $(superb)! "` :) – Alex Hall Nov 29 '17 at 01:34
  • 4
    I'm so upset with myself for not googling for an answer to this ages ago. I had no idea it would be such a simple solution. My commit messages deserved the excitement I was trying to bring to them! – ctrlplusb Aug 27 '20 at 14:20
8

If you need to use double quotes and the ! is the last character in the message, just keep the ! outside of the quotes, since we're only using quotes so that the spaces get included in the message.

git commit -m "Reverting last commit because I don't like it"!

If you need to include ! mid string, you can use single quotes but if you need to use a literal single quote, you'll need to close your quote, then put the ' outside of the string by escaping it. So, let's say your message is I don't like it! Reverting again!, this can be composed with 'I don' + \' + 't like it! Reverting again!'

git commit -m 'I don'\''t like it! Reverting again!'

Anything more complicated than this, you're probably better off with git commit and letting git invoke your default text editor :)

Ryan H.
  • 7,374
  • 4
  • 39
  • 46
5

No need to remember what quotes or escapes to use, instead

  1. Type your command (or part of it)
  2. In your command line, press Ctr X E » will open your "external editor".
  3. Edit your command, and close your editor.

… the command will be executed & any special characters will show up correctly in the Git commit message!

(NB many other uses: typing complex commands, adding line breaks in your commit message etc.)

PDK
  • 1,476
  • 1
  • 14
  • 25
1

You can also type:

git commit -am "Nailed it
!
"

So just hit enter and place the exclamation mark on a new line, then close off the commit message. Found that one out by accident one day.

cg22
  • 347
  • 5
  • 16
  • 8
    This will insert the literal newline prior to the exclamation mark into the commit message, which is ugly. -1. – Mark Amery Jan 05 '16 at 16:42
-3

Hmm. Escaping it with a slash doesn't show up in commit message for me.

git commit -m "Nailed it\!"
Steve Geluso
  • 139
  • 1
  • 2
  • 7