Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of an exclamation mark in a Git commit message via the command line

Tags:

git

bash

People also ask

What does exclamation mean in git?

That means the Git status is normal. As soon as you start editing a file, the status changes to modified and the icon overlay then changes to a red exclamation mark. That way you can easily see which files were changed since you last updated your working tree and need to be committed.

Which command is used for writing a commit message in git?

The easiest way to create a Git commit with a message is to execute “git commit” with the “-m” option followed by your commit message.

What is the command to commit with the message?

To add a Git commit message to your commit, you will use the git commit command followed by the -m flag and then your message in quotes.

What is the use of commit message in git?

What is a commit message? The commit command is used to save changes to a local repository after staging in Git. However, before you can save changes in Git, you have to tell Git which changes you want to save as you might have made tons of edits.


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


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.)


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.)


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 :)


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.