80

I am trying to take this one step further. How could this work in a standard Bash shell?

git commit -m 'cracked enigma's code'

Could this simply be done with backslash-escaping like the following?

git commit -m 'cracked enigma\'s code'

Further, how could double-quotes be used? Also by backslash-escaping? Would that be the best way? Are there any good alternative ways?

git commit -m 'cracked the "real" enigma's code'
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nutty about natty
  • 1,267
  • 2
  • 10
  • 17
  • 3
    *"Could this simply be done with backslash escaping like so"*: You could just try yourself and find out! – Felix Kling Apr 16 '13 at 09:19
  • 1
    @FelixKling true, but I'm a git-beginner and don't feel to courageous just yet. – nutty about natty Apr 16 '13 at 09:23
  • 1
    This doesn't really have anything to do with courageousness. You can always create an empty git repository, create one file, create the commit and then see what the outcome is. The question itself is OK of course. – Felix Kling Apr 16 '13 at 09:26
  • (for reference) http://www.gnu.org/software/bash/manual/bashref.html#Double-Quotes & http://www.gnu.org/software/bash/manual/bashref.html#Quoting – nutty about natty Apr 16 '13 at 20:32

2 Answers2

126

Use double quotes:

git commit -m "cracked enigma's code"

Or, if your message contains other special characters, use double quotes or backslash only for the single quote:

git commit -m 'cracked $enigma'"'"'s code'
git commit -m 'cracked $enigma'\''s code'
choroba
  • 231,213
  • 25
  • 204
  • 289
  • 4
    what's the `$`-sign doing there? isn't that sth to define a variable? (sorry if this is too bash-basic...) – nutty about natty Apr 16 '13 at 09:28
  • 4
    It's there to show why simple double quotes do not work. You can try with `echo` instead of `git commit -m` – choroba Apr 16 '13 at 09:30
  • 13
    Basically it’s because inside double quotes, variables are expanded; and `$enigma` would be a variable reference. So if you want your message to contain an actual dollar sign, you cannot use double quotes. – poke Apr 16 '13 at 09:41
  • 2
    when trying to git commit with single quoted comments, one will get a "error: pathspec '_____' did not match any file(s) known to git." – Akin Hwan Dec 28 '17 at 23:03
  • Good answer. The concatenation (`'"'"'`) is really the way to go. I use it all the time. It feels strange but works very well. – Karl Wilbur Jan 06 '18 at 12:47
21

There is no need to escape the ' character if your commit is double quoted.

git commit -m "cracked enigma's code"

EDIT: Anyway, when you have some special characters to add in the commit message I prefer to edit in a editor (like nano or vim), commiting without the -m option.

git commit

And then put the message and exit. It's more confortable instead of thinking how you have to escape all those quotes and double quotes.

Pigueiras
  • 18,778
  • 10
  • 64
  • 87
  • This is not true for `git commit -m "pattern match wasn't exhaustive, scalac!"` on OSX, git 2.15.1. Works if I leave out the exclamation. Weird stuff. – Viktor Hedefalk Feb 15 '18 at 15:37